Unable to attach to and debug AWS Lambda functions started using SAM CLI

I'd like to debug a set of lambda functions started using the following SAM CLI command:

`sam local start-lambda -t <TEMPLATE_FILE>`

This starts a bunch of lambda functions and prints to the console:

Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
Running on http://127.0.0.1:3001/

It's also possible to start the container in debug mode using the `-d` flag: `sam local start-lambda -d 5678 -t <TEMPLATE_FILE>`. But when I use "Attach to Node.js" in Run/Debug Configuration to debug, I can't connect to localhost:5678.

Has anyone managed to start the lambda functions with the command above and then attach to the container to debug them? I can debug a particular lambda locally using AWS Toolkit plugin, which invokes (as opposed to starting) the lambda. But it doesn't seem possible to start lambdas using AWS Toolkit.

I'm on MacOS (Apple Silicon).

P. S.: Unfortunately, this discussion didn't help much.

1
3 comments

what can you see if you open the http://localhost:5678/json  URL in browser?

BTW, did you try using AWS Toolkit for WebStorm for running/debugging AWS Lambda functions as suggested in https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html? Also, the https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html#serverless-sam-cli-running-locally page suggests using the 

sam local invoke -d 5858 <function logical id>

command to start a function locally in debug mode, can you attach the debugger when using this command?

0

Elena,

I see nothing when I browse http://localhost:5678/json. No connection.

I can debug a single lambda function by invoking it using AWS Toolkit. Under the hood, the plugin does this:

- compile TypeScript
- sam build
- sam local invoke -d 57482

Then I see on the debugger console:

Debugger listening on ws://0.0.0.0:57482/327f961e-35b0-4199-9ef8-ef63fc8b6473
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

So the plugin does the attaching just fine. But when I try it without the help of the plugin, same problem.

The problem is this only invokes the Lambda function, and then dies after a single invocation. What I would like is for it to stay alive for further  invocations. Normally, without debugging, I do this using

sam local start-lambda -t <TemplateFile>

This gets up all of the lambdas defined in the template. And you could then interact with them, and they stay alive. What I would like to know is if it's possible to somehow attach WebStorm's debugger to this. Here's what I tried:

- sam local start-lambda -t <TemplateFile> -d 5678
- Attach to Node.js/Chrome with 5678
- Invoke a particular lambda

When I invoke the lambda, I can even see in the console:

Debugger listening on ws://0.0.0.0:5678/456a44e4-bdb2-4936-b60f-5b171a5a03a7
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

When I put the breakpoint in the TypeScript file, debugging doesn't work.

 

0

>I see nothing when I browse http://localhost:5678/json. No connection.

in this case, attaching via Attach to Node.js won't work as well, nothing is listening on the port:(

I'd suggest contacting the plugin authors for assistance, I have no experience with AWS stuff as it's not officially supported by JetBrains

0

Please sign in to leave a comment.