Search code examples
node.jstypescriptvisual-studio-codeaws-lambdaaws-sam-cli

VSCode AWS Sam (node12.x image) TypeScript debugger not reaching breakpoints


Im trying to get vscode debugger to work with an application generated with AWS Sam, but with TypeScript.

So before I added TypeScript, the debugger worked fine, I could reach the breakpoints without issue. When I added TypeScript, I had to change to folder structure, by adding a src and dist folder, so currently my file structure is like this:

enter image description here

According to AWS documentation (page 58): https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/aws-tookit-vscode-ug.pdf I think it has to do with the pathMappings in the launch.json file, but I can't seem to figure out what would the correct path. This is my current launch.json file:

 {
    "configurations": [
        
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "puppeteer-pdfMerger:HelloWorldFunction",
            "invokeTarget": {
                "target": "template",
                "templatePath": "puppeteer-pdfMerger/template.yaml",
                "logicalId": "HelloWorldFunction"
            },
            "lambda": {
                "runtime": "nodejs12.x",
                "payload": {},
                "environmentVariables": {},
                "pathMappings": [{
                    "localRoot": "${workspaceFolder}/puppeteer-pdfMerger/hello-world/dist/HelloWorldFunction",
                    "remoteRoot": "/var/task/dist"
                }]
            }
        }
}

I will note that when running this configuration the containerized Lambda runs fine, it's just the breakpoints are not working.


Solution

  • I managed to get the breakpoints to work in the end.

    What ended up working for me was changing the Dockerfile to:

    FROM public.ecr.aws/lambda/nodejs:12
    
    COPY dist/*.js package.json ./
    
    RUN npm install
    
    # Command can be overwritten by providing a different command in the template directly.
    CMD ["app.lambdaHandler"]
    

    And having the launch.json config as follows:

    {
                "type": "aws-sam",
                "request": "direct-invoke",
                "name": "puppeteer-pdfMerger:PdfGeneratorAndMergerFunction TemplateTarget",
                "invokeTarget": {
                    "target": "template",
                    "templatePath": "puppeteer-pdfMerger/template.yaml",
                    "logicalId": "PdfGeneratorAndMergerFunction"
                },
                "lambda": {
                    "runtime": "nodejs12.x",
                    "payload": {},
                    "environmentVariables": {},
                    "pathMappings": [
                        {
                            "localRoot": "${workspaceRoot}/puppeteer-pdfMerger/hello-world/dist",
                            "remoteRoot": "/var/task"
                        }
                    ]
                }
            }