I'm trying to follow the instructions I found here for debugging a Python SAM application in VS Code
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-python.html
I'm not sure why they don't use sam build in the expamle, and point to .aws-sam/build but that's what I'm attempting.
My launch.json looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "SAM CLI Python debug test",
"type": "python",
"request": "attach",
"port": 5890,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/.aws-sam/build",
"remoteRoot": "/var/task"
}
]
}
]
}
I'm triggering the Lambda's directly for now and so I'm invoking them like this:
sam local invoke -d 5890
I'm then putting a breakpoint at the beginning of the Lambda I find in the build folder, but when I start the debugger in VS Code it executes the Lambda without stopping at the breakpoint.
I created a GitHub repo with the test project I'm using and description of how I'm using it.
https://github.com/rupe120/aws-sam-python-debugging-test
Could someone help point me at what I'm missing in my setup?
So, the recommended way to do this is with the AWS Toolkit extension.
https://github.com/awslabs/aws-sam-cli/issues/1926#issuecomment-616600799
So the docs suggest using a localRoot of "${workspaceFolder}/hello_world/build" (assuming one is using the Hello World template). However, it only works when removing the build at the end of the path:
"localRoot": "${workspaceFolder}/hello_world"
This way, I got it to work without the AWS Toolkit.
Related
Is it possible to both Dockerize a Django app and still be able to debug it using Visual Studio Code's debugging tool? If yes, how? E.g, using docker-compose to run Django app, postgres, and a redis instance and be able to debug the Django app via Visual Studio Code.
Yes, this is possible.
I've done it with a NestJs app and should be a similar setup.
Expose a specific port on the Django app service in the compose file first.
Create the launch.json file the following configuration, then replace <port-exposed-on-container> and <directory-on-container> with real values.
{
"version": "0.2.0",
"configurations":
[
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach",
"port": <port-exposed-on-container>,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/<directory-on-container>",
"protocol": "inspector",
"restart": true
},
]
}
Until recently my Django projects would debug fine using launch.json file. But it stopped working today and I have no idea about it.
Changes made by me in system were:
Clean-Installing Windows
Installing python in default path just for current user and not all users
Problem Description:
As soon as I click F5 key the debugger starts for some milliseconds and stops automatically without any prompt. As it does not shows any error I tried to log errors by nothing in it. It does not even load terminal to run commands.
The added configuration in launch.json file is as below:
{
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true,
"logToFile": true,
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"host": "localhost"
}
]
}
Can someone help to troubleshoot this, I have already tried:
Creating new projects
Reinstalling Python
Creating new environment
Resetting VS Code Sync Settings
Run other dubug configurations {they are working fine)
Changing django debug configuration
My current options are:
Research more (Already spent a hours would take more)
Wait for solution by someone
Clean Install Windows and all software's (Would be like BhramaAstra)
Posting answer within few minutes of posting question seems weird but I got a solution that could help to get perfect answer to my problem.
The problem was the python environment path: I created environment in documents folder of C drive of current non-admin user hoping of no issues since python is installed for just the current user in default path. But as soon as created new environment in current user directory the debugger started working normally.
The issue is related to permissions and file paths, hope this helps to get solution to new questions generated:
Why can't I use Documents folder for creating a python environment?
Is there some other solution to my actual problem?
Adding images for further details:
Debugger not working from this environment -
Debugger working as usual if environment created here -
I am unable to find example or good documentation on how to debug functions locally when building Lambda functions using the SAM CLI, AWS CDK, and Lambda Layers.
When building an RestApi and simple Lambda function with the CDK and then attempting to debug locally with VSCode using a launch configuration like:
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "hello:app.handler (nodejs12.x)",
"invokeTarget": {
"target": "code",
"projectRoot": "${workspaceFolder}/infrastructure/handlers/hello",
"lambdaHandler": "app.handler"
},
"lambda": {
"runtime": "nodejs12.x",
"payload": {},
"environmentVariables": {}
}
}
the import statements referencing modules from any layer throw an error such as:
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '/opt/nodejs/utils'"
Steps to reproduce:
Clone this repo: https://github.com/swizzmagik/sam-lambda-layer-test
Run npm install
Run npm run build
Run npm run api
Observe that the hello function properly resolves the layer references and works
fine when called using curl or postman
Open handlers/hello/app.ts
and try to debug in VSCode by opening the hello.ts file and hitting
F5 Notice that the debugger starts but is unable to import the
module and fails on this line import { buildResponseHeaders,
handleError } from "/opt/nodejs/utils";
I'm using the VS Code to write a Lambda function with AWS SAM (Serverless Application Model) CLI configured locally. I had started this project using this command sam init --runtime nodejs12.x --name
After executing this command, I got a sample hello-world project scaffold and I can see options like Run Locally| Debug Locally | Configure like in this screenshot:-
I believe this options comes using the 'Code Lens' plugin installed in VS Code editor. I was able to write my own code & debug using this option locally but after a while this option disappeared like below:-
Now, I could not run my Lambda code in the Debug mode anymore. By executing this command:-
sam local invoke --event
sam local invoke --no-event
The Lambda is running locally as normal but not in Debug mode. When I create new AWS SAM projects, the options are coming but not anymore with my code. Is it something to do with Code Lens or SAM Serverless Template?
Please assist to help me:-
Option A) To enable this options back
Option B) Equivalent command in AWS SAM to run the Lambda in Debug mode
Thank you,
there is a tool called Thundra. It has a plugin for VSCode where you can debug Lambda's real-time. You can check it from here: https://marketplace.visualstudio.com/items?itemName=thundra.thundra-debugger
I was able to find the solution to my problem here. This is out of box provided by AWS SAM CLI.
We need to configure the launch.json of VS Code first like below to a debugger port 9999 or anything:-
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to SAM CLI",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 9999,
"localRoot": "${workspaceRoot}/hello-function",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false
}
]
}
And then execute the below command :-
sam local invoke -d 9999 --no-event <function name>
I am working on a Google Cloud Functions Project and I want to use the AWS-SDK so I edit the package.json and it looks like this:
{"name": "sample-http",
"version": "0.0.1"
"dependencies": {
"aws-sdk": "2.574.0"
}
}
The deployment crashes with the following message in the logs: INVALID_ARGUMENT
I am working in the Browser environment.
Can somebody help or does it have something to do with the fact that I don't use the paid plan until now?
I already saw this post but the answer is not working for me as you can see.
Your JSON is not valid, it is missing a comma after the version item:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"aws-sdk": "2.574.0"
}
}
Using npm install to create a package.json should allow you to avoid issues like this. Additionally, there's a good number of publicly usable JSON validators on the web to test for issues like this.
by some reason when I install packages for cloud functions, package Json is not updated