Debug AWS SAM in Visual Studio Code - node.js

I am trying to debug my \event endpoint using AWS SAM in VSCode and I'm having issues with two of the configurations I've tried. The Direct Invoke Configuration I've used successfully in a different project, but in my current project it returns this error even though template.yaml and package.json are both in the EventsApi project directory.
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Direct Invoke Config",
"invokeTarget": {
"target": "api",
"templatePath": "template.yaml",
"logicalId": "AddEventFunction"
},
"api": {
"path": "/event",
"httpMethod": "post",
"payload": {
}
}
}
which produces this error
[ERROR]: SamLaunchRequestError: Failed to run launch configuration
-> Error: Cannot find package.json for: C:/aws/EventsApi/template.yaml
And the second configuration, which I attach in the SAM CLI to my AddEventFunction lambda in debug mode, then the debugger catches exceptions but won't hit any endpoints with this configuration. Please note that stopOnEntry is unable to be added to the attach configuration for some reason.
{
"name": "Attach to SAM CLI Config",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "${workspaceRoot}/src/handlers",
"remoteRoot": "",
"sourceMaps": true,
},
I expect the first configuration to find package.json just fine, and the second configuration to hit a breakpoint.

Related

launch.json for azure core function version 3

I was working in the azure core tools version 2 and recently updated to version 3. Now I am not able to run any functions as it is showing error message "connect Econnrefused 127.0.0.1:9091".
Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
I am working on windows machine and using 1.6.1 azure functions versions.
Do I have to change something? I checked and this is common error and reinstalling the extensions should work?I tried all those. Do you think, I am missing anything ?
Please check if the below workarounds help to:
Approach 1:
Use Integrated terminal in the VS Code and then run the Azure Functions Python:
I will also get this issue most of the time and My working launch.json configuration will be like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current file",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Azure Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
Approach 2:
If the above approach gets the issue still, then try changing the port number and then run the Azure Functions Python.
Approach 3:
Instead of running the Azure Functions Python from the VS Code built-in Run Menu, try with the commands func host start in the integrated terminal like:
If that does not help you, please refer to this workaround given here which solves the specific error of Azure Function Python - Error Connection Refused.

VSCode attach debug to nodejs which run on WSL

I get error:
Cannot connect to runtime process, timeout after 10000ms - (reason: cannot connect to the target)
NodeJs v4.3.1 run on WSL.
The below is my launch.json
"type": "node",
"request": "attach",
"name": "Attach to WSL",
"port": 3000,
"address": "localhost",
"restart": true,
"protocol": "inspector",
"localRoot": "${workspaceFolder}/web-frontend",
"remoteRoot": "/mnt/c/workspace/.../web-frontend"
WSL uses the path from Windows so localRoot and remoteRoot are the same.
What am I missing so far?
I created a VS Code extension: WSL workspaceFolder, which will help you to automatically set the remoteRoot in your launch.json to the correct WSL path. For example my launch.json, based upon your file paths would look like below;
/vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "localhost",
"port": 5858,
"localRoot": "${workspaceFolder}/web-frontend",
"remoteRoot": "${command:extension.vscode-wsl-workspaceFolder}/web-frontend"
}
]
}
Then to start a debug session, in the WSL terminal enter NODE_ENV=debug node --nolazy --inspect-brk=5858 before the path to the script you wish to debug.
However, you may run into problems using Node v4 because it does not support the 'inspector protocol'. I would strongly recommend you upgrade to a more recent version of Node. Currently v8 is the latest LTS version: Node.js releases

Can't debug serverless app in Visual Studio Code

I'm trying to find out how can I use Visual Studio Code debugger to debug serverless lambda function. For testing purposes I have very simple test lambda function:
module.exports.handler = async (event) => {
debugger;
console.log(111, event);
};
Then, in launch.json I created such a configuration:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeVersion": "8.4.0",
"runtimeExecutable": "node",
"program": "${workspaceFolder}/node_modules/.bin/sls",
"cwd": "${workspaceRoot}",
"args": [
"invoke",
"local",
"--function",
"test",
"--data",
"{foo:'bar'}"
],
"port": 9229
}
]
}
Now, I'm puting a breakpoint inside my lambda function and pressing F5 to start debugger. I'm expecting then to go in code, put some watches and walk my code step-by-step. But nothing happens. No errors. No console outputs, no code paused on breakpoints. Nothing. All I get is message in debug console: /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}
If I go then in terminal and run that line I got exactly what is expected
set#set-home ~/www/blahblah/ens $ /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}
Serverless: INVOKING INVOKE
111 '{foo:bar}'
What am I doing wrong? How can I debug serverless application in Visual Studio Code?
I suspect you just need to get rid of the "port": 9229 bit.
I've never specified a port for debugging Serverless functions locally, but adding it to any of my working configurations produces the symptoms you observe.
Incidentally, you might be able to take some of the other stuff out, as well. For reference, my Serverless debug configurations typically look like this for invoking locally:
{
"type": "node",
"request": "launch",
"name": "Debug sls local",
"program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
"args": [
"invoke", "local", "--function", "processFirehose", "--path", "sample-event.json",
"--stage",
"nonprod",
]
},

Error: Cannot connect to runtime; make sure that runtime is in 'legacy' debug mode

I see the above when trying to debug Node.js script with Visual Studio Code.
My launch.json looks like
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"protocol": "legacy",
"processId": "${command:PickProcess}"
}
]
}
No matter I put the line "protocol": "legacy", or not I get exactly the same error as above.
My environment
System: OSX
Node: v8.6.0
VSC: 1.17.2
Also, I run the node script with PM2.
Any suggestion would be hugely appreciated
Node v8.6 does not support "legacy" protocol. You should use the "inspector" protocol.
I ran above the same issue using the "legacy" protocol while running the Hello World Extension. After searching a bit, I stumbled on this issue, and change my launch.json file to the following as suggested by #weinand:
{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [ "--extensionDevelopmentPath=${workspaceRoot}" ],
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ]
}
using node 8.9.1
used the latest version of the yeoman generator for the code.

Debug/Run in vscode: Program exists

I have an express.js app.
Whenever I try to debug/run it in vscode, it just runs through and exists. How do I keep this process alive just like it normally does? Or is something wrong with my config?
No errors whatsoever are thrown.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "www",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"www"
],
"address": "localhost",
"port": 5000,
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"env": {
...
}
}
]
My npm-script looks like this:
flow-node www/index.js
I want to debug/run this script from vscode since I have a lot of env-variables and, obviously, for debug-reasons.
You may just need to specify the program attribute of the launch.json config.
program - executable or file to run when launching the debugger
https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes

Resources