Search code examples
amazon-web-servicesdebuggingaws-lambdaserverless-application-model

Is there any command to run Lambda function in the 'Debug' mode locally using the AWS SAM CLI?


I'm using the VS Code to write a Lambda function with AWS SAM (Serverless Application Model) CLI configured locally. I had started this project using this command sam init --runtime nodejs12.x --name

After executing this command, I got a sample hello-world project scaffold and I can see options like Run Locally| Debug Locally | Configure like in this screenshot:-

enter image description here

I believe this options comes using the 'Code Lens' plugin installed in VS Code editor. I was able to write my own code & debug using this option locally but after a while this option disappeared like below:- enter image description here

Now, I could not run my Lambda code in the Debug mode anymore. By executing this command:- sam local invoke --event sam local invoke --no-event

The Lambda is running locally as normal but not in Debug mode. When I create new AWS SAM projects, the options are coming but not anymore with my code. Is it something to do with Code Lens or SAM Serverless Template?

Please assist to help me:- Option A) To enable this options back Option B) Equivalent command in AWS SAM to run the Lambda in Debug mode

Thank you,


Solution

  • I was able to find the solution to my problem here. This is out of box provided by AWS SAM CLI. We need to configure the launch.json of VS Code first like below to a debugger port 9999 or anything:-

    {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Attach to SAM CLI",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 9999,
            "localRoot": "${workspaceRoot}/hello-function",
            "remoteRoot": "/var/task",
            "protocol": "inspector",
            "stopOnEntry": false
          }
        ]
      }
    

    And then execute the below command :-

    sam local invoke -d 9999 --no-event <function name>