Description:
I'm trying to debug a lambda with sam cli. It starts up the docker container, and then waits for me to attach a debugger. When I try to attach, vscode pops up a warning:
Unable to open 'index.js': File not found (file:///c:/Users/esty.heller/Documents/Dev/benmanagesoftware/lambda/runtime/node_modules/awslambda/index.js).
And then it just stops and waits, can't do anything except disconnect and let the function run.
Just running the function with sam local invoke works fine.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
Windows 10, Docker version 18.03.1-ce, build 9ee9f40 (using linux containers), latest vscode
Output of sam --version:
SAM CLI, version 0.4.0
Optional Debug logs:
$ sam local invoke -e event-create.json -d 5858 CreateUrl
2018-07-15 11:17:47 Invoking create-url.handler (nodejs8.10)
2018-07-15 11:17:47 Found credentials in shared credentials file: ~/.aws/credentials
Fetching lambci/lambda:nodejs8.10 Docker container image......
2018-07-15 11:17:49 Mounting C:\Users\esty.heller\Documents\Dev\benmanagesoftware\lambda\url-shortener as /var/task:ro inside runtime container
Debugger listening on ws://0.0.0.0:5858/3e08373e-2480-470f-9eed-d7fe2651cae0
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
At this point, I try to attach using my launch config:
{
"name": "Attach to Sam Local",
"type": "node",
"request": "attach",
"port": 5858,
"protocol": "inspector",
"address": "localhost",
"localRoot": "${workspaceRoot}/",
"remoteRoot": "/var/task/"
}
And I get a popup warning Unable to open 'index.js': File not found (file:///c:/Users/esty.heller/Documents/Dev/benmanagesoftware/lambda/runtime/node_modules/awslambda/index.js). and it won't attach.
Here is my template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 300
Runtime: nodejs8.10
MemorySize: 512
Resources:
CreateUrl:
Type: AWS::Serverless::Function
Properties:
Handler: create-url.handler
Role: arn:aws:iam::933503113720:role/benmanage-lambda-api-dev
FunctionName: url-shortener-create-url-dev
Here is the create-url.js
exports.handler = async event => {
console.log(JSON.stringify(process.env, null, '\t'));
return event;
};
and here is event-create.json
{
"url": "https://hashids.org",
"metaData": {
"email": "[email protected]",
"phone": "+13145291389"
}
}
Add --debug flag to command you are running
$ sam local invoke -e event-create.json -d 5858 CreateUrl --debug
2018-07-15 11:40:49 local invoke command is called
2018-07-15 11:40:49 1 resources found in the template
2018-07-15 11:40:49 Found Serverless function with name='CreateUrl' and CodeUri='.'
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 http://localhost:None "GET /v1.35/_ping HTTP/1.1" 200 2
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 Found one Lambda function with name 'CreateUrl'
2018-07-15 11:40:49 Invoking create-url.handler (nodejs8.10)
2018-07-15 11:40:49 Environment variables overrides data is standard format
2018-07-15 11:40:49 Loading AWS credentials from session with profile 'None'
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable config_file from defaults.
2018-07-15 11:40:49 Loading variable credentials_file from defaults.
2018-07-15 11:40:49 Loading variable data_path from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable credentials_file from defaults.
2018-07-15 11:40:49 Loading variable config_file from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable metadata_service_timeout from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable metadata_service_num_attempts from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Looking for credentials via: env
2018-07-15 11:40:49 Looking for credentials via: assume-role
2018-07-15 11:40:49 Looking for credentials via: shared-credentials-file
2018-07-15 11:40:49 Found credentials in shared credentials file: ~/.aws/credentials
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable region from config file with value 'us-east-1'.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable region from config file with value 'us-east-1'.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable profile from defaults.
2018-07-15 11:40:49 Loading variable region from config file with value 'us-east-1'.
2018-07-15 11:40:49 Resolving code path. Cwd=C:\Users\esty.heller\Documents\Dev\benmanagesoftware\lambda\url-shortener, CodeUri=.
2018-07-15 11:40:49 Resolved absolute path to code is C:\Users\esty.heller\Documents\Dev\benmanagesoftware\lambda\url-shortener
2018-07-15 11:40:49 Code C:\Users\esty.heller\Documents\Dev\benmanagesoftware\lambda\url-shortener is not a zip/jar file
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 http://localhost:None "GET /v1.35/images/lambci/lambda:nodejs8.10/json HTTP/1.1" 200 None
2018-07-15 11:40:49 Looking for auth config
2018-07-15 11:40:49 No auth config in memory - loading from filesystem
2018-07-15 11:40:49 Trying paths: ['C:\\Users\\esty.heller\\.docker\\config.json', 'C:\\Users\\esty.heller\\.dockercfg']
2018-07-15 11:40:49 No config file found
2018-07-15 11:40:49 Looking for auth entry for 'docker.io'
2018-07-15 11:40:49 No entry found
2018-07-15 11:40:49 No auth config found
2018-07-15 11:40:52 http://localhost:None "POST /v1.35/images/create?tag=nodejs8.10&fromImage=lambci%2Flambda HTTP/1.1" 200 None
Fetching lambci/lambda:nodejs8.10 Docker container image......
2018-07-15 11:40:52 Mounting C:\Users\esty.heller\Documents\Dev\benmanagesoftware\lambda\url-shortener as /var/task:ro inside runtime container
2018-07-15 11:40:52 http://localhost:None "POST /v1.35/containers/create HTTP/1.1" 201 90
2018-07-15 11:40:52 http://localhost:None "GET /v1.35/containers/79c711637027c81c8f952b4f9b255a2e5ab5378468607ec784c331e6306c5d20/json HTTP/1.1" 200 None
2018-07-15 11:40:52 http://localhost:None "GET /v1.35/containers/79c711637027c81c8f952b4f9b255a2e5ab5378468607ec784c331e6306c5d20/json HTTP/1.1" 200 None
2018-07-15 11:40:52 http://localhost:None "POST /v1.35/containers/79c711637027c81c8f952b4f9b255a2e5ab5378468607ec784c331e6306c5d20/start HTTP/1.1" 204 0
2018-07-15 11:40:52 Setting up SIGTERM interrupt handler
2018-07-15 11:40:52 http://localhost:None "GET /v1.35/containers/79c711637027c81c8f952b4f9b255a2e5ab5378468607ec784c331e6306c5d20/json HTTP/1.1" 200 None
2018-07-15 11:40:52 http://localhost:None "POST /containers/79c711637027c81c8f952b4f9b255a2e5ab5378468607ec784c331e6306c5d20/attach?stream=1&stdin=0&logs=1&stderr=1&stdout=1 HTTP/1.1" 101 0
Debugger listening on ws://0.0.0.0:5858/0d8e8c68-dc15-4b74-b038-e0b83ca1cf1c
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
I have the same problem using the latest version of vscode.
Linux Ubuntu
SAM CLI 0.4.0
vscode 1.25.1
nodejs 8.10.
I'm also experiencing this problem as of this morning.
Windows 10
Docker 18.03.1-ce build 93354b3
SAM CLI 0.2.8
VSCode 1.25.1
NodeJS 6.10
Is this only with the latest version of VSCode? Did this work in the previous 1.24 version?
Trying to narrow down the search field here but the comments suggest something in 1.25 changed.
yes, it is working in the previous version 1.24 so the problem only occurs in the latest version of VSCode.
So my recommendation is to stay a version behind (if you can) until we can track what VSCode did to break the CLI.
Same problem here on MacOS Sierra with 1.25.1.
Something similar happens for Node.js 6.10, but instead of failing it lets you debug a remote index.js file that looks pretty similar to this gist.
I don't know if my observation is related to this issue but here is it anyway:
=============== VS Code ===============
Version: 1.25.1
Commit: 1dfc5e557209371715f655691b1235b6b26a06be
Date: 2018-07-11T15:40:20.190Z
Electron: 1.7.12
Chrome: 58.0.3029.110
Node.js: 7.9.0
V8: 5.8.283.38
Architecture: x64
================== SAM CLI ======================
SAM CLI, version 0.5.0
================== OS ==========================
Linux bingstp 4.15.0-29-generic #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
=================== Observation =====================
I am working on an example project, and found if I just change the lambda runtime to nodejs8.10, the problem appears with message "Unable to open 'index.js': File not found (file:///home/bing/git/aws-serverless-roundtrip-option1/sam-app/runtime/node_modules/awslambda/index.js).", but if I change runtime to "nodejs6.10" there's no such problem.
I also would like to add a walk-around. If, in launch.json, both "localRoot" and "remoteRoot" is properly set, especially "localRoot" is set as the root of the source, corresponding to the "codeUri" property in template, you can just set a breakpoint in the code, ignore the error message (although a bit of annoying to see it there) and press F5 or click the Continue (play) button, and execution will pause at the breakpoint and you can trace your code. It's just you cannot trace what's inside the code from that "awslambda/index.js".
For example, in the example project I'm working on, "CodeUri: src/" is in "template.yaml" in the properties of the lambda function, and "localRoot": "${workspaceRoot}/src" is in the launch.json to match the codeUri.
Or, if breakpoint does not work, you can still use the "debugger" statement to break the execution.
@bingtimren workaround works for me
another workaround is use
chrome://inspect to attach and debug
I have upgraded to v1.26 and still same issue
I'm having this same problem with v.1.27
{
"version": "0.6.0",
"configurations": [
{
"name": "Attach to SAM CLI",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5859,
"localRoot": "${workspaceRoot}",
"sourceMapPathOverrides": {
"${workspaceRoot}/runtime": "${remoteRoot}",
},
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false
}
]
}
This was the setting, under which I was able to get it to work. VScode needs to be open at the sam-app level. I'll have a PR out for this soon.
We have updated the documentation to reflect the correct mechanism to launch vscode debugging.
We have updated the documentation to reflect the correct mechanism to launch vscode debugging.
could you provide updated document link?
Most helpful comment
I also would like to add a walk-around. If, in launch.json, both "localRoot" and "remoteRoot" is properly set, especially "localRoot" is set as the root of the source, corresponding to the "codeUri" property in template, you can just set a breakpoint in the code, ignore the error message (although a bit of annoying to see it there) and press F5 or click the Continue (play) button, and execution will pause at the breakpoint and you can trace your code. It's just you cannot trace what's inside the code from that "awslambda/index.js".
For example, in the example project I'm working on, "CodeUri: src/" is in "template.yaml" in the properties of the lambda function, and "localRoot": "${workspaceRoot}/src" is in the launch.json to match the codeUri.
Or, if breakpoint does not work, you can still use the "debugger" statement to break the execution.