aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Get an error when trying the AWS tutorial for running a Lambda locally under SAM with vscode codelens

Open nickweavers opened this issue 6 years ago • 16 comments

Description

Using the vscode codelens "Debug locally" optiion to debug the simple hello-world node example gives an error.

Steps to reproduce

Follow the steps described in this short video tutorial exactly, but choose the nodejs12.x runtime. When you finally click the "Debug locally" CodeLense option above the exports.lambdaHandler = async (event, context) => { the error will be produced in the "Debug console".

The steps listed are:

  1. open a bash terminal in WSL
  2. cd to a directory you'd keep your projects in (for me /c/var/www/lambda)
  3. In vscode CTRL-SHIFT-P
  4. Select "AWS: Create new SAM application"
  5. Select node12.x as SAM Application Runtime
  6. Select ASW SAM Hello World as SAM Application Template
  7. Enter . as the workspace folder for the new project
  8. Click okay at the path confirmation /c/var/www/lambda/
  9. enter sam-app as the name of your new app
  10. In vscode, edit the sam-app/hello-world/app.js file
  11. Insert a new line after line 19 and add console.log("We got here!");
  12. Place a breakpoint on this new line
  13. Click Configure in the CodeLens options above the exports.lambdaHandler = async (event, context) => {
  14. Click on the TERMINAL link when placed into the editor fore template.json
  15. enter sam local generate-event apigateway aws-proxy | clip 6 select the {} in the "event": {}, in templates.json and paste in the clipboard
  16. ALT-SHIFT-F to auto format
  17. Now click the ``Debug Locally"" CodeLens option in the app.js file

Observed result

The debugger first stops at the line const RAPIDClient = require("./RAPIDClient.js"); in file index.js image After clicking the continue button in image it gets here image the clicking continue again gives the error below in the DEBUG CONSOLE

2020-05-10T16:33:02.723Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1153:14)","    at Object..js (internal/modules/cjs/loader.js:1176:10)","    at Module.load (internal/modules/cjs/loader.js:1000:32)","    at Function._load (internal/modules/cjs/loader.js:899:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}
START RequestId: b9a1ab35-1c23-10e5-fcfb-b89d7e57ac38 Version: $LATEST
END RequestId: b9a1ab35-1c23-10e5-fcfb-b89d7e57ac38
REPORT RequestId: b9a1ab35-1c23-10e5-fcfb-b89d7e57ac38	Init Duration: 20909.03 ms	Duration: 1.53 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 42 MB	

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}

Expected result

I expected the working result they show in the video, which was that the debugger stops at the breakpoint on the console.log("AM is the coolest"); statement.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows 10 Pro 1909 (Build 18363.815)
  2. sam --version: SAM CLI, version 0.48.0
  3. WSL bash: $ docker --version Docker version 19.03.8, build afacb8b7f0 (as client)
  4. Windows DockerDesktop version 2.2.0.5 (43884) Stable
  5. vscode: Version: 1.45.0 (system setup) Commit: d69a79b73808559a91206d73d7717ff5f798f23c Date: 2020-05-07T16:18:48.860Z Electron: 7.2.4 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.18363

Add --debug flag to command you are running

nickweavers avatar May 10 '20 17:05 nickweavers

@nickweavers One thing to check is your docker shared settings. If that is not enabled for where your app is on disk then docker will just fail to mount but still try to execute (why it doesn't just fail is beyond me). If that is already enabled, then I suggest toggling the setting. This usually will wake up docker and remind it to do what is was already suppose to be doing.

jfuss avatar May 11 '20 02:05 jfuss

Do you mean the file sharing setting here @jfuss ? image

I tried toggling that with a restart between but I'm afraid it didn't fix my issue. I still get

2020-05-11T08:57:24.249Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1153:14)","    at Object..js (internal/modules/cjs/loader.js:1176:10)","    at Module.load (internal/modules/cjs/loader.js:1000:32)","    at Function._load (internal/modules/cjs/loader.js:899:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}
START RequestId: 03e19bf6-3870-1ada-9e10-26ba4c6ff8b8 Version: $LATEST
END RequestId: 03e19bf6-3870-1ada-9e10-26ba4c6ff8b8
REPORT RequestId: 03e19bf6-3870-1ada-9e10-26ba4c6ff8b8	Init Duration: 9649.92 ms	Duration: 1.42 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 41 MB	

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}

nickweavers avatar May 11 '20 08:05 nickweavers

(And just in case anyone else ends up here for the same reasons I did... if you're using WSL 1 and making a project in your Linux home directory, you'll see errors like this, presumably because Docker doesn't know how to mount things there.)

birtles avatar May 11 '20 11:05 birtles

When I run Vscode local sam debug on myApp and the debugger stops on the first breakpoint, DockerDesktop shows shows that a container is running and clicking Inspect on DockerDesktop shows a mount point is being created on this directory /tmp/aws-toolkit-vscode/vsctkTI3eKg/output/awsToolkitSamLocalResource

image

Listing the contents of this directory in my wsl bash I see:

nickw@nicks-xps-15-9560:/tmp/aws-toolkit-vscode/vsctkTI3eKg/output/awsToolkitSamLocalResource $ ls -la total 16 drwxrwxrwx 1 nickw nickw 512 May 15 08:40 . drwxr-xr-x 1 nickw nickw 512 May 15 08:40 .. -rwxrwxrwx 1 nickw nickw 2101 Oct 26 1985 README.md -rwxrwxrwx 1 nickw nickw 978 Oct 26 1985 myApp.js drwxrwxrwx 1 nickw nickw 512 May 15 08:40 node_modules -rwxrwxrwx 1 nickw nickw 728 Oct 26 1985 package.json -rwxrwxrwx 1 nickw nickw 1668 Oct 26 1985 template.yaml

But the vscode debug OUTPUT pane shows:

Attaching debugger to SAM Application...
Debugger attached.
Debugger attached
2020-05-15T08:14:50.956Z undefined ERROR Uncaught Exception

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'myApp'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'myApp'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1153:14)"," at Object..js (internal/modules/cjs/loader.js:1176:10)"," at Module.load (internal/modules/cjs/loader.js:1000:32)"," at Function._load (internal/modules/cjs/loader.js:899:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)"," at internal/main/run_main_module.js:18:47"]}
START RequestId: 542fa657-ac24-1eca-333b-a4f92d27a9b5 Version: $LATEST
END RequestId: 542fa657-ac24-1eca-333b-a4f92d27a9b5
REPORT RequestId: 542fa657-ac24-1eca-333b-a4f92d27a9b5 Init Duration: 2042883.21 ms Duration: 2.44 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 41 MB

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'myApp'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}
Local invoke of SAM Application has ended.

Any ideas on why the error is saying that myApp can't be found when it is clearly in the directory that is being mounted and that directory is in the wsl file system?

nickweavers avatar May 15 '20 09:05 nickweavers

@nickweavers VSCode looks to be mounting /tmp/aws-toolkit-vscode/vsctkTI3eKg/output/awsToolkitSamLocalResource which might not be shared with Docker?

What happens if you just invoke through the SAM CLI instead of VSCode? If the command line works, I would suggest reaching out to VSCode as this would have to do with their implementation and might know or have seen this issue before in the Toolkit.

jfuss avatar May 15 '20 14:05 jfuss

Hi @jfuss, that does look like it may be the case because when I go into the cli for the container from DockerDesktop and look at what is in /var/task I see this:

sh-4.2$ ls -la /var/task
total 4
drwxr-xr-x 2 root root   40 May 15 14:26 .
drwxr-xr-x 1 root root 4096 May  7 02:12 ..
sh-4.2$

But the odd thing is, it seems the vscode extension is copying the code directory to the wsl /tmp folder to avoid those kinds of permission issues.

nickweavers avatar May 15 '20 15:05 nickweavers

@jfuss, using SAM CLI I get the same issue:

nickw@nicks-xps-15-9560:/var/www/lambda/sam-app
$ sam build
Building resource 'HelloWorldFunction'
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrc
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

nickw@nicks-xps-15-9560:/var/www/lambda/sam-app
$ sam local invoke
Invoking app.lambdaHandler (nodejs12.x)

Fetching lambci/lambda:nodejs12.x Docker container image......
Mounting /var/www/lambda/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
2020-05-15T15:26:24.560Z        undefined       ERROR   Uncaught Exception      {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1156:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)","    at Module.load (internal/modules/cjs/loader.js:1000:32)","    at Function.Module._load (internal/modules/cjs/loader.js:899:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}
START RequestId: b6b79197-b222-1060-73bd-3d890f9ea367 Version: $LATEST
END RequestId: b6b79197-b222-1060-73bd-3d890f9ea367
REPORT RequestId: b6b79197-b222-1060-73bd-3d890f9ea367  Init Duration: 117.63 ms        Duration: 1.68 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 24 MB

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}
nickw@nicks-xps-15-9560:/var/www/lambda/sam-app

I wonder if this is a Docker issue, since the files at /tmp/aws-toolkit-vscode/vsctkTI3eKg/output/awsToolkitSamLocalResource should be accessible via the mount at /var/task inside the container, but they are not.

nickweavers avatar May 15 '20 15:05 nickweavers

@nickweavers This smells of a docker sharing issue to me. I don't know or understand the interactions between docker and WSL. If I had to guess, that is having something to do with your issue here. I will see what I can read up on. If you do the same and figure it out, please post here. I would like to roll this up into a troubleshooting section on the doc page so others aren't hunting around for the same thing.

jfuss avatar May 15 '20 21:05 jfuss

Hi @jfuss, I have just posted this on an existing issue in docker/for-win/issues on github here https://github.com/docker/for-win/issues/4812#issuecomment-629618373. That ticket was specifically about this volume mounting issue on wsl 2, so not sure if I should have raised it as a new issue with a title that reflected the wsl1/wsl2 inconsistency issue. I guess a moderator may break it out if they think it is better to do so.

nickweavers avatar May 16 '20 10:05 nickweavers

Spotted this issue for Docker, looks suspiciously like it's related. Hasn't been closed yet so I guess it's not fixed. https://forums.docker.com/t/docker-desktop-windows-2-2-0-0-containers-cant-find-files/87587/16

nickweavers avatar May 16 '20 22:05 nickweavers

The other issue we have seen that did get mysteriously resolved in the past, is that only the current user directory gets shared with docker even though docker sharing utility says that the entire drive is shared.

sriram-mv avatar May 26 '20 16:05 sriram-mv

any idea why it always breaks in /var/runtime/index.js line 10?? const RAPIDClient = require("./RAPIDClient.js");

ikamal-pxl avatar Sep 19 '21 13:09 ikamal-pxl

any idea why it always breaks in /var/runtime/index.js line 10?? const RAPIDClient = require("./RAPIDClient.js");

I would LOVE to know why this is. I see no resolution to this, no matter what I search for. Super annoying!

vgjenks avatar Mar 10 '22 18:03 vgjenks

any idea why it always breaks in /var/runtime/index.js line 10?? const RAPIDClient = require("./RAPIDClient.js");

I would LOVE to know why this is. I see no resolution to this, no matter what I search for. Super annoying!

I didn't find any appropriate solution but as a workaround I created a main method to call the lambda handler to be able to debug on my machine easily. This approach is much better as it doesn't take time to build. You can find an example below


const x = require('./services/x');
const y = require('/opt/layerY');

require("dotenv").config();

exports.lambdaHandler = async (event, context) => {
    try {
       //Code Here
    } catch (err) {
       //Code Here
    }
};

async function main() {
    const fs = require('fs');
    let rawdata = fs.readFileSync('./events/input.json');
    let json = JSON.parse(rawdata);
    let result = await module.exports.lambdaHandler(json, null);
    fs.writeFileSync('./events/output.json', JSON.stringify(result))
    console.log(result);
}

if (require.main === module) {
    main();
}

ikamal-pxl avatar Mar 13 '22 08:03 ikamal-pxl

any idea why it always breaks in /var/runtime/index.js line 10?? const RAPIDClient = require("./RAPIDClient.js");

@ikamal-pxl @vgjenks Not sure why, but this seems to fix the issue:

Run the debugger. After it breaks, in the "CALL STACK" section, right click the second entry from the top and click "Exclude Caller".

sam-debug

rocky-m avatar Apr 08 '22 00:04 rocky-m

I just wasted 4 hours of my life trying to work around this issue. As it turns out, I had *.js in my .gitignore file (it had got in there because we're working in TypeScript and didn't want to commit the js). I'm not sure why, but this prevented all our js from being copied into the docker container (or into .aws-sam\build\Function when using a Jetbrains IDE).

Removing .js from .gitignore sorted it all out.

damiensawyer avatar Jun 16 '23 08:06 damiensawyer