Reputation: 237
I'm using VSCode (SAM CLI, version 1.40.1, aws-cli/2.4.16) and remote Docker to build and test Lambda applications. I did manage to invoke my Lambda locally using Docker running on separate from my development VirtualBox machine (VM1 Alpine Linux running Docker Docker version 19.03.12, VM2 Windows 10 VSCode, etc.).
The result of that invocation goes like this:
PS C:\dev\sam-app> sam local invoke --container-host-interface 0.0.0.0 --container-host 192.168.0.187
Invoking app.lambdaHandler (nodejs14.x) Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.40.1-x86_64. Mounting C:\dev\sam-app.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container END RequestId: 15582b26-8431-411e-bf16-f9f2b3091c0c REPORT RequestId: 15582b26-8431-411e-bf16-f9f2b3091c0c Init Duration: 1.68 ms Duration: 70.05 ms Billed Duration: 71 ms Memory Size: 128 MB
Max Memory Used: 128 MB {"statusCode":200,"body":"{"message":"hello world"}"} PS C:\dev\sam-app>
For the operation to be successful I have to manually copy the build folder to my remote machine (I'm using Putty's PSCP).
The problem happens when I want to debug the function in VSCode->Run->Start Debuging (F5) using Breakpoints. Sam builds project into User App Data folder adding also some randomly generated folder and trying to mount that folder to a container.
2022-04-04 12:03:37 [INFO]: Preparing to debug locally: Lambda "app.lambdaHandler" 2022-04-04 12:03:37 [INFO]: Building SAM application... 2022-04-04 12:03:37 [INFO]: Running command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd build --build-dir C:\Users\Karol\AppData\Local\Temp\aws-toolkit-vscode\vsctkujyih9\output --template C:/Users/Karol/AppData/Local/Temp/aws-toolkit-vscode/vsctkujyih9/app___vsctk___template.yaml --base-dir C:/dev/sam-app/hello-world] 2022-04-04 12:03:39 [INFO]: Building codeuri: C:/dev/sam-app/hello-world runtime: nodejs14.x metadata: {} architecture: x86_64 functions: ['helloworld']
2022-04-04 12:03:39 [INFO]: Running NodejsNpmBuilder:NpmPack
2022-04-04 12:03:40 [INFO]: Running NodejsNpmBuilder:CopyNpmrc
2022-04-04 12:03:40 [INFO]: Running NodejsNpmBuilder:CopySource
2022-04-04 12:03:40 [INFO]: Running NodejsNpmBuilder:NpmInstall
2022-04-04 12:03:44 [INFO]: Running NodejsNpmBuilder:CleanUpNpmrc
2022-04-04 12:03:44 [INFO]: Running NodejsNpmBuilder:LockfileCleanUp
2022-04-04 12:03:44 [INFO]: Build Succeeded
2022-04-04 12:03:44 [INFO]: Built Artifacts : ....\Temp\aws-toolkit-vscode\vsctkujyih9\output Built Template : ....\Temp\aws-toolkit-vscode\vsctkujyih9\output\template.yaml
Commands you can use next ========================= [] Invoke Function: sam local invoke -t ....\Temp\aws-toolkit-vscode\vsctkujyih9\output\template.yaml [] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch [*] Deploy: sam deploy --guided --template-file ....\Temp\aws-toolkit-vscode\vsctkujyih9\output\template.yaml
2022-04-04 12:03:45 [INFO]: Build complete. 2022-04-04 12:03:45 [INFO]: Starting SAM application locally 2022-04-04 12:03:45 [INFO]: AWS.running.command Invoking app.lambdaHandler (nodejs14.x) Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.40.1-x86_64.
Mounting C:\Users\Karol\AppData\Local\Temp\aws-toolkit-vscode\vsctkujyih9\output\helloworld as /var/task:ro,delegated inside runtime container No response from invoke container for helloworld
Command stopped: "sam local invoke" 2022-04-04 12:03:55 [INFO]: Waiting for SAM application to start...
Of course, that folder doesn't exist on my remote machine and I can't copy it during run as I don't know the name of randomly generated folder.
2022-04-04 11:37:39 [INFO]: Build complete. 2022-04-04 11:37:39 [INFO]: Starting SAM application locally 2022-04-04 11:37:40 [INFO]: AWS.running.command Invoking app.lambdaHandler (nodejs14.x) Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.40.1-x86_64.
Mounting C:\Users\Karol\AppData\Local\Temp\aws-toolkit-vscode\vsctkWIK37z\output\helloworld as /var/task:ro,delegated inside runtime container No response from invoke container for helloworld
Command stopped: "sam local invoke"
Lunch.json looks like this:
{
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "hello-world:app.lambdaHandler (nodejs14.x)",
"invokeTarget": {
"target": "code",
"projectRoot": "${workspaceFolder}/hello-world",
"lambdaHandler": "app.lambdaHandler"
},
"lambda": {
"runtime": "nodejs14.x",
"payload": {},
"environmentVariables": {}
}
}
]
}
Strangely when I build an application using > sam build, it's being built to my project folder. Where I can copy the files from and invoke my function locally to see if it works, but can't debug this way.
Is there anything I can do to resolve that problem?
Upvotes: 1
Views: 2100
Reputation: 237
The solution that worked for me was to swap the lunch.json file generated when hitting F5 (Start Debugging). When there is no lunch.json file added VSCode suggests a few options, some of them are related to AWS template.yml, node.js, and one for VSCode. That last one, when selected opens up blank and configuration has to be created from scratch - strange. I found it online. I think the key is you can specify the address and port which is very important when dealing with the remote Docker.
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to SAM CLI",
"type": "node",
"request": "attach",
"address": "192.168.0.187",
"port": 9999,
"localRoot": "${workspaceRoot}/hello-world",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false
}
]
}
With that lunch file, VSCode stops building the project in Users/Name/AppData/..., but instead is using the project Build folder created by instruction 'sam build'. Of course, prior to debugging, I have to copy it to the remote machine. It is all very limited and complicated. Probably much easier when using Docker desktop.
Upvotes: 0