aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.53k stars 1.17k forks source link

Debug not works with "Sam local start-api" with "--debug-port" and "--warm-containers" #3749

Open CeccoCQ opened 2 years ago

CeccoCQ commented 2 years ago

With this command: sam local start-api --template-file template.yml --region eu-west-1 --debug-port 5555 --warm-containers EAGER

and this launch.json

{
      "name": "Attach to SAM CLI",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 5555,
      "localRoot": "${fileDirname}",
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "stopOnEntry": false
    }

my Visual Studio Code doesn't attach to container, I have no error or log, simply after 5 seconds the debug's topbar disappear. The terminal logs doesn't show "Debugging listening on...." when I try lo execute an HTTP request.

All works fine removing --warm-containers param.

qingchm commented 2 years ago

Thanks for reporting this! Please allow us some time to start reproducing the issue and get investigate!

Cliftonz commented 2 years ago

Getting the same issue.

jaedenharris commented 2 years ago

I also cannot start the debugger mode with or without the --warm-containers option

sam local start-api --warm-containers LAZY -p 3001 -d 9999

achandoff commented 2 years ago

Am getting the same issue. any update ?

ibadsiddiqui commented 2 years ago

Hello Everyone, I am having the same issue, the debugger doesn't attach and the top bar disappears.

vfilter commented 2 years ago

Debug does not work at all for me. Using the built-in scaffolded hello world and executing sam local invoke -d 9999 -e events/event.json HelloWorldFunction will successfully invoke the Lambda, but won't wait for the debugger. The hello world function just gets executed and ends.

jonkipu commented 2 years ago

@CeccoCQ Regarding the original issue using start-api, I had to dive into the code a bit to find out the following:

If the template contains several functions (could be generated functions besides your original lambda, such as log retention related lambdas), a warning will be output: Warning: you supplied debugging options but you did not specify the --debug-function option. To specify which function you want to debug, please use the --debug-function <function-name>

What's non-intuitive about this code path is that it will disable setting of the debug port rendering debugging useless in this case. If you have one function and don't specifiy --debug-function AWS SAM will assume you meant to debug your one and only function, when using ---warm-containers the code path will disable debugging if the function being executed isn't the one specified by --debug-function, however the code uses different identifiers of said function to check, again rendering debugging useless, this honestly seems like a bug.

To make sure you can debug your function use --debug-function with your current flags, but specify the function name as specified in your template, e.g: MyFunction1A65A8F as opposed to just MyFunction

CeccoCQ commented 2 years ago

@jonkipu thanks for your answer. I've a lot of Lambda Functions and for my purposes I've to debug them concurrently, I mean more function at the time. With ---debug-function the debug will be restricted only to one function, I suppose that this is a bug or if it's a feature, it's a really strange feature (in my opinion) because if I've an API gateway at 99% I've several functions.

jonkipu commented 2 years ago

I agree, I was baffled by this myself, it seems that for whatever reason, when no --debug-function is passed, debugging is disabled. I have yet to try and change the code to see if it just works when enabling debugging and debugging multiple functions, if it does I'll update you and I guess we'll have to bring this issue into someone's attention

jonkipu commented 2 years ago

@CeccoCQ, played with the code a little and I think part of this is by design, since when opting to use --warm-containers, SAM will retain the containers between invocations of each function, since it uses a different container for each function under your API, it can only expose the debug options (namely the --debug-port) to the host OS for only one of those containers, that's why choosing --debug-function is required, since it controls for which of your functions those debug settings are applied. If you forego --warm-containers SAM will create a container per function but will destroy them after that function is finished

OrRosenblatt commented 1 year ago

I run into the same issue. When running either of the following:

The specific ENV variables related to debugging (NODE_OPTIONS , NODE_PATH etc.) weren't added to the Docker image, and the 5858 debug port wasn't exposed. I also see the warning message mentioned above:

Warning: you supplied debugging options but you did not specify the --debug-function option.  
To specify which function you want to debug, please use the --debug-function <function-name>.

However when running sam local start-api --template-file template.yml --debug-port 5858 the port and the related node options are setup correctly on the Docker image. Any update on this issue?

jonkipu commented 1 year ago

@OrRosenblatt not sure this is so easy to fix / will be fixed soon as mentioned in my comment. I've been using the --debug-function parameter when using "--warm-containers" since then and it's fine as a workaround (at least for me)

eydelrivero commented 9 months ago

Is there any solution to this? I'm having the same problem when executing:

sam local start-api --debug-port 5858 --debug-function MyFunction --warm-containers EAGER

I have several functions in my template. I can see one of the containers exposing port 5858 but when I click on Run And Debug, the process seems to start but stops after a few seconds. The only thing info I see is this error under the OUTPUT tab, when selecting Window in the dropdown to the right:

Error: Could not connect to debug target at http://localhost:5858: Could not find any debuggable target

image