Local Debug/Breakpoint a Typescript Lambda in AWS CDK

Viewed 640

I have an AWS CDK app that creates a typescript Lambda function using lambdaNJS.NodejsFunction. The Lambda function runs in the AWS cloud and I can also run it locally. but how do I configure VSCode to hit a breakpoint for local debugging? I'm on Windows 10.

CDK (create Lambda):

const presignupLambda = new lambdaNJS.NodejsFunction(this, 'PresignupLambda', {
    functionName: 'presignup',
    entry: 'lambdas/presignup/main.ts',
    handler: 'handler',
    environment: {
        NODE_OPTIONS: '--enable-source-maps'
    },
    bundling: { sourceMap: true, minify: true }
});

Lambda main.ts:

import { PreSignUpTriggerEvent } from 'aws-lambda';
exports.handler = async (event: PreSignUpTriggerEvent) => {
    console.log('test!');
}

Note that the npm aws-lambda module is in the root package.json. The lambda does not have a local package.json -- only a main.ts file. The CDK is clever enough to bundle any required files for the Lambda.

CDK tsconfig.json (default created by the CDK app plus outDir so typescript .js and .d.ts files don't clutter the project).

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": ["es2018"],
    "declaration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": ["./node_modules/@types"],
    "outDir": "tsc.out"
  },
  "exclude": ["cdk.out", "**/tsc.out"]
}

Custom PowerShell script to run a cdk synth and generate a template.yaml in utf8.

.\scripts\pareto_cmd.ps1 sb2 cdk synth CognitoStack --no-staging > template.yaml
Get-Content template.yaml | Set-Content -Encoding utf8 template-utf8.yaml
Copy-Item template-utf8.yaml -Destination template.yaml
Remove-Item template-utf8.yaml

template.yaml contains this entry for the Lambda function. Note the aws:asset:path property that contains the bundled Lambda code.

PresignupLambda947F13DA:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1
        S3Key: 56ac0e4abb421da5514ff908c13eaa81b0f0dc733bfc918556e6be5f31fdfbf9.zip
      Role:
        Fn::GetAtt:
          - PresignupLambdaServiceRole32A83F70
          - Arn
      Environment:
        Variables:
          NODE_OPTIONS: --enable-source-maps
          AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
      FunctionName: cognito-presignup
      Handler: index.handler
      Runtime: nodejs14.x
    DependsOn:
      - PresignupLambdaServiceRole32A83F70
    Metadata:
      aws:cdk:path: CognitoStack/PresignupLambda/Resource
      aws:asset:path: C:\aws-cdk\cdk.out\asset.56ac0e4abb421da5514ff908c13eaa81b0f0dc733bfc918556e6be5f31fdfbf9
      aws:asset:property: Code

The C:\aws-cdk\cdk.out\asset.56ac0e4abb421da5514ff908c13eaa81b0f0dc733bfc918556e6be5f31fdfbf9 folder contains the Lambda bundled code:

index.js
index.js.map

Running the following SAM CLI command is successful:

sam local invoke PresignupLambda947F13DA --event events/presignup.json --debug-port 5858

The VSCode launch.json contains the following:

{
    "name": "Attach to SAM CLI",
    "type": "node",
    "request": "attach",
    "address": "localhost",
    "port": 5858,
    "localRoot": "${workspaceFolder}/cdk.out/",
    "remoteRoot": "/var/task",
    "protocol": "inspector",
    "stopOnEntry": false,
    "outFiles": [
        "${workspaceFolder}/cdk.out/**/*.js"
    ]
}

In VSCode, Run > Start Debugging (or hit F5 key) runs the Lambda function locally successfully but does not stop on breakpoints in main.ts. However, setting a breakpoint in the bundled js here does work: C:\aws-cdk\cdk.out\asset.56ac0e4abb421da5514ff908c13eaa81b0f0dc733bfc918556e6be5f31fdfbf9\index.js

0 Answers
Related