Visual Studio Code Debugger Not Connecting to SAM Local
Asked Answered
B

3

5

Per the AWS documentation, I am starting SAM local like this:

$ sam local start-api -d 5858

I have the following in my launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to SAM Local",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 5858,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/var/task"
        }
    ]
}

But when I launch the visual studio debugger it says"cannot connect to runtime make sure that runtime is in 'legacy' debug mode"

It looks as though many people have this issue with Node.js 6 and Visual Studio Code but I can't seem to find an answer... I'm using version 1.18.1 of Visual Studio Code

I have tried adding "protocol": "Legacy" to launch.json config. I've also tried using --debug-port instead of -d. I'm on Windows 10. Not sure if the issue is windows-specific.

Bouzoun answered 20/11, 2017 at 22:17 Comment(1)
Have you tried aws-sam-vscode ( github.com/eduardolucioac/aws-sam-vscode ) ❓❤️🐧Judenberg
R
4

When running sam local either through a terminal or visual studio code's terminal, set your break points and select the name of your launch.json in the debug drop down as shown in the documentation.

https://github.com/awslabs/aws-sam-local#debugging-applications

After that, launch sam local start-api. Then when you hit an endpoint on the API you should see the terminal state something like:

2018/01/12 07:17:29 Invoking index.handler (nodejs6.10)

2018/01/12 07:17:29 Mounting /Users/24g/1725_ecpo_lambda as /var/task:ro inside runtime container Debugger listening on [::]:8000

Once you see that the debugger is listening. Click the play button on the the debugger. I've noticed that this doesn't usually pick up on its own, unless there is an exception I believe.

I'm using version 1.19.1 of visual studio code.

Have you tried using a different port? This is currently my configuration:

{
  "version": "0.2.0",
     "configurations": [
      {
        "name": "Attach to SAM Local",
        "type": "node",
        "request": "attach",
        "address": "localhost",
        "port": 8000,
        "localRoot": "${workspaceRoot}",
        "remoteRoot": "/var/task"
      }
   ]
}
Rinarinaldi answered 12/1, 2018 at 12:36 Comment(1)
Thank you Gino. I was able to make it work in WebStorm, which is actually my IDE of choice. Although, I did find it to be a bit hit or miss in that sometimes it took multiple attempts to get the debugger to connect. I actually ended up abandoning SAM Local all together, but I will refer to your notes in the future if I need to use it again with VSC.Bouzoun
E
3

To fix this and debug Node SAM Local Lambda functions I had to read the Node.js debug docs (https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_supported-nodelike-runtimes).

Specifically the portion that solved this problem on my side related to the "Legacy" vs "Inspector" debug protocols. For Node >8.0 Legacy Debug protocol needs to be used.

Since the Lambda Runtime relies on Node 6.10 "Legacy" Protocol had to be used on my end.

At first I didn't think this could possibly be the issue since the SAM Local node VS Code configuration actually in-correctly (at least for me) references the "Inspector" protocol in their docs, while correctly referencing the "Legacy" protocol in the Gif (see below) inside of those same docs.

VS Code needs "Legacy" node debug protocol to work with AWS SAM Local

For those who would like to try that out, here is what my WORKING launch.json needed to look like.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to SAM Local",
            "type": "node",
            "request": "attach",
            "address": "127.0.0.1",
            "port": 8000,
            "localRoot": "${workspaceRoot}/dist",
            "remoteRoot": "/var/task",
            "protocol": "legacy"
        }
    ]
}

The most important part of that is: "protocol": "legacy"

I assume that you can swap 127.0.0.1 back to localhost, and port back to 5858 (rather than 8000) since those are all fragments of my varied attempts to find a solution but I have not tried that just yet.

I will submit a pull request to the AWS SAM Local README.md on github (found here: https://github.com/awslabs/aws-sam-local#debugging-applications) as soon as I get a minute. Hope this helps others in the interim!

Compare the above to the docs which as of the time of this posting still list the following JSON as the correct settings for the VS Code launch.json file:

INCORRECT JSON that does not work but is referenced in the docs (DO NOT USE THIS ONE):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to SAM Local",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 5858,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/var/task",
            "protocol": "inspector"
        }
    ]
}
Englut answered 12/3, 2018 at 1:12 Comment(0)
T
1

Not sure who's going to see this, but I faced this problem when testing with sam local invoke, however, my issue was simpler.

I just forgot to add the port -d 5860 to the sam local invoke command

Torture answered 7/7 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.