Lambda Remote Debugging lets you use breakpoints, inspect variables, and step through your Lambda function code locally within VS Code. It is supported for Python, Node.js, and Java, and works with SAM-based or standalone Lambda projects.
Lambda Remote Debugging with AWS Toolkit for VS Code
This guide describes how to use the AWS Toolkit for VS Code to debug Lambda functions running in LocalStack. This new integration enables interactive, IDE-native debugging for Python, Node.js, and Java Lambda functions with minimal setup.
Lambda Debug Mode is a preview feature in LocalStack designed to enhance your Lambda debugging workflows.
This feature provides an optimized environment for debugging Lambda functions, ensuring that you have the
necessary tools and flexibility to troubleshoot effectively.
Automatic Timeout Management: Integrates with API Gateway to prevent Lambda function timeouts,
giving developers ample time to connect remote debuggers and inspect the function’s behavior.
For further customization, you can use a configuration file.
Specify the path to this file with the LAMBDA_DEBUG_MODE_CONFIG_PATH environment variable, ensuring the
file is mounted into the LocalStack container.
Manually setting LAMBDA_DOCKER_FLAGS is unnecessary when using this configuration.
Here is an example of mounting a debug_config.yaml in your LocalStack container to start your Debug Mode:
Any change to the configuration file on your local filesystem would be automatically picked by the LocalStack container.
After debugging a Lambda function, its associated container will automatically stop.
The configuration file should contain a functions block where you can define debug settings
for each specific Lambda function ARN.
To debug multiple Lambda functions simultaneously, assign a different debug port to each function.
Note that this configuration affects the container’s internal debugger port as well, so the debugger
port must be set accordingly.
Lambda functions debugging used to be a difficult task.
LocalStack changes that
with the same local code mounting functionality that also helps you
to iterate quickly over your function code.
For providing the debug server, we use debugpy
inside the Lambda function code.
In general, all you need is the following code
fragment placed inside your handler code:
1
import debugpy
2
debugpy.listen(("0.0.0.0", 19891))
3
debugpy.wait_for_client() # blocks execution until client is attached
For extra convenience, you can use the wait_for_debug_client function from our example.
It implements the above-mentioned start of the debug server and also adds an automatic cancellation of the wait task if the debug client (i.e. VSCode) doesn’t connect.
1
defwait_for_debug_client(timeout=15):
2
"""Utility function to enable debugging with Visual Studio Code"""
For attaching the debug server from Visual Studio Code, you need to add a run configuration.
1
{
2
"version": "0.2.0",
3
"configurations": [
4
{
5
"name": "Python: Remote Attach",
6
"type": "python",
7
"request": "attach",
8
"connect": {
9
"host": "localhost",
10
"port": 19891
11
},
12
"pathMappings": [
13
{
14
"localRoot": "${workspaceFolder}",
15
"remoteRoot": "."
16
}
17
]
18
}
19
]
20
}
In the next step we create our function.
In order to debug the function in Visual Studio Code, run the preconfigured remote debugger, which will wait about 15 seconds as defined above, and then invoke the function.
Make sure to set a breakpoint in the Lambda handler code first, which can then later be inspected.
The screenshot below shows the triggered breakpoint with our 'Hello from LocalStack!' in the variable inspection view:
Due to the ports published by the lambda container for the debugger, you can currently only debug one Lambda at a time.
Due to the port publishing, multiple concurrently running lambda environments are not supported.
The host.docker.internal is a special DNS name by Docker and will make sure that the lambda running in the docker can connect to PyCharm running on your Localhost.
You can use the wait_for_debug_client and add it to your lambda (please adapt the path to your venv directory if necessary):
defwait_for_debug_client():
"""Utility function to enable debugging with PyCharm"""
import sys, glob
# enter the correct path here to your venv (where pydev_pycharm is installed
my_venv ="venv/lib/python*/site-packages"
sys.path.insert(0, glob.glob(my_venv)[0])
import pydevd_pycharm
# host.docker.internal should resolve to the host
# see also: https://docs.docker.com/desktop/networking#use-cases-and-workarounds-for-all-platforms
In the next step we create our function.
In order to debug the function in PyCharm set a breakpoint in your function, run the Remote Debug configuration and then invoke the function.
To create the Lambda function, you just need to take care of two things:
Deploy the function via an S3 Bucket.
You need to use the magic variable hot-reload as the bucket name.
Set the S3 key to the path of the directory your lambda function resides in.
The handler is then referenced by the filename of your lambda code and the function in that code that should be invoked.
Note the suspend=y option here, it will delay code execution until the debugger is attached to the debugger server.
If you want to change that, simply switch to suspend=n.
By default the runtime environment for Java will set -Xshare: on, so we’ll have to disable it here again.
Your IDE might show you the listen address as *:5050, but please note that this only works for Java 9+.
Configuring IntelliJ IDEA for remote JVM debugging
Open the Run/Debug Configurations window and create a new Shell Script with
the following content:
Terminal window
while [[ -z $(dockerps | grep:5050) ]]; dosleep1; done
This shell script should simplify the process a bit since the debugger server is not immediately available (only once Lambda container is up).
Then create a new Remote JVM Debug configuration and use the script from above as a Before launch target:
Now to debug your Lambda function, simply click on the Debug icon with Remote JVM on LS Debug configuration selected, and then invoke your Lambda function.
The debugger can also act as a server by changing the drop-down “Debugger mode” to “Listen to remote JVM”.
In this case you should not set LAMBDA_DOCKER_FLAGS since the port will be exposed on your host instead of the Lambda container.
Compared to the previous setup the “Wait Remote Debugger Server” run configuration should also be removed and instead tick the mark at “Auto restart” after switching to the “Listen to remote JVM” mode.
For the Lambda function you will have to adjust the environment variable to "_JAVA_OPTIONS": "-Xshare:off -agentlib:jdwp=transport=dt_socket,server=n,address=172.17.0.1:5050,suspend=y,onuncaught=n".
Notice the address=172.17.0.1:5050.
Here we tell the Lambda function to connect to port 5050 on 172.17.0.1.
When using Docker desktop you might have to set this to address=host.docker.internal:5050 instead.
Configuring Visual Studio Code for remote JVM debugging
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.