How to use AWS CDK to compile and deploy a typescript api with dependencies to lambda?


I have an api in TypeScript that i would like to deploy to a Lambda function. The project has dependencies that needs to be compiled on the platform it's running on, so I need a build step that can do the build on Linux machine in aws before it deploys. How can I leverage the CDK, codepipeline (or codebuild?) to do this?

Following the workshop its super easy to get going with the pipeline that can deploy a "hello world" lambda, but I'm struggling to advance with this. How can I compile the TS code as a step in the pipeline, or how can set up a step in the pipeline that pulls code from another repo and then compiles and deploys it?

I've tried to use the aws-cdk-lib/aws-lambda-nodejs constructor that claims to compile automatically. But, so far the pipelines crashes on the 2. step (build step in the pipeline self mutate process) because it can't find the dependencies in the Lamba.


  • package-lock.json
  • package.json
  • graph.js:
import { ApolloServer, gql } from 'apollo-server-lambda'
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core'
const typeDefs = gql`
    type Query {
        hello: String
const resolvers = {
  Query: {
    hello: () => 'Hello world!',

const server = new ApolloServer({
  introspection: true,
  plugins: [ApolloServerPluginLandingPageGraphQLPlayground]


exports.handler = server.createHandler();


  • pipeline-stack
export class PipelineStack extends cdk.Stack {

  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const pipeline = new CodePipeline(this, '*ID', {
      pipelineName: '*NAME',
      synth: new CodeBuildStep('*ID', {
        input: CodePipelineSource.connection('*ghaccount/ghrepo', '*branch', {
          connectionArn: "*GHCONNECTION ARN"
        installCommands: ['npm install -g aws-cdk-lib'],
        commands: [
          'npm ci',
          'npm run build',
          'npx cdk synth'

    pipeline.addStage(new PipelineStage(this, 'Deploy', {
      env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }

  • pipeline-stage.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from "constructs";
import { LambdaStack } from './server-stack';

export class PipelineStage extends cdk.Stage {

  constructor(scope: Construct, id: string, props?: cdk.StageProps) {
    super(scope, id, props);

    // services / resources the app needs to run goes here ->>
    const lambdaStack = new LambdaStack(this, 'LambdaStack');
  • server-stack.ts
export class LambdaStack extends cdk.Stack {

  public readonly api: apiGateway.RestApi

  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const graphqlLambda = new NodeLambda.NodejsFunction(this, 'adeegso-api-lambda', {
      entry: path.join(__dirname, '/../lambda/graph.ts'),

      handler: 'graph.handler',
      depsLockFilePath: "lambda/package-lock.json",

    this.api = new apiGateway.LambdaRestApi(this, 'adeegso-api-endpoint', {
      handler: graphqlLambda,


Thanks in advance for any help provided!

asked 2 years ago3288 views
3 Answers

I think @aws-cdk/aws-lambda-nodejs is what you are looking for.

answered 2 years ago
  • Currently using aws-cdk-lib/aws-lambda-nodejs. Problem is that the self mutating pipeline sees the import statements for the lambda dependencies and crashes. For this to work i guess there would have to be some way of letting the cdk pipeline ignore the dependencies in the lambda, do the self mutation to set up the steps in the pipeline, and then load and compile the dependencies for the lambda code as a step in the pipeline. No idea how to do this, and haven't found anyone writing about it either.

  • @Magnus can you deploy your LambdaStack without using Pipelines cdk deploy LambdaStack ?

  • @Michael_K No, I think we need to compile as a step in the pipeline. Because of some dependencies we have included we need to compile the code on the same runtime as its running, and because the team uses different OS locally we need to compile it as a step in the deployment.


Have you taken a look at constructs? there are lots of good solutions in there.

profile pictureAWS
answered 2 years ago

Hi, I have encouter the exact issue, I am using @aws-cdk/aws-lambda-nodejs i but not matter how I try I also would enouter the import module is not found issue with the cdk pipeline ... did you find any way to fix it?

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions