The AWS Toolkit for Visual Studio Code has rolled out a new SAM debugging experience, leveraging VS Code’s launch configurations. This enhancement replaces the previous “Run Locally | Debug Locally” feature. To explore the improved SAM debugging experience, simply install the AWS Toolkit from the VS Code Marketplace. The new setup aligns more closely with VS Code’s standard debugging workflow, encompassing configuration, execution, and code stepping.
The AWS Toolkit bridges VS Code with the SAM CLI. Users can create a configuration within VS Code, which, when launched, prompts the AWS Toolkit to build the serverless project, deploy the code to a local Docker environment, and initiate the Lambda in Docker. This integration enables users to step through their Lambda code directly in VS Code.
What’s New
The updated SAM debugging experience for the AWS Toolkit allows users to utilize VS Code’s standard launch configuration interface to run and debug Lambda handlers locally. In addition to a more cohesive experience, aws-sam launch configurations provide increased flexibility for configuring launchers for various SAM templates or handlers.
Local Lambda debugging with SAM includes new features that allow users to:
- Specify AWS credentials (the aws field of the launch config)
- Set SAM CLI options (the sam field of the launch config)
- Debug a Lambda locally without requiring a SAM template.yaml file by designating target: code in the invokeTarget of the launch configuration.
How To Use It
The enhanced AWS Toolkit allows you to run or debug AWS SAM Lambdas locally by executing a launch configuration of type: aws-sam.
Step 1: Create a launch configuration for a SAM Lambda using one of the following methods:
- Navigate to the template.yaml in your SAM project, position your cursor over a Lambda resource, and click the “Add Debug Configuration” CodeLens. This action will add a pre-populated launch configuration to your launch.json file.
- Alternatively, go to the source code file (.js, .cs, or .py) in your SAM project, position your cursor over a Lambda handler, and click the “Add Debug Configuration” CodeLens. This will similarly add a pre-populated launch configuration to your launch.json.
- You can also edit launch.json manually to create a new item (where projectRoot points to a valid directory, lambdaHandler is the name of the handler, and runtime is a recognized runtime such as nodejs12.x, python3.7, dotnetcore2.1, etc.):
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "my-config",
"invokeTarget": {
"target": "code",
"projectRoot": "src",
"lambdaHandler": "myHandler"
},
"lambda": {
"runtime": "nodejs12.x",
"event": {},
"environmentVariables": {}
},
"sam": {},
"aws": {}
}
Step 2: Open the “Run” panel in the VS Code sidebar. Select your launch configuration from the Run panel menu. Click the “play” button in the Run panel or press F5.
Next Steps
In this article, we’ve highlighted the recent advancements in the SAM debugging experience within the AWS Toolkit for Visual Studio Code. To explore these features, install the Toolkit and refer to the user guide for further information. For additional insights on this topic, check out this blog post or visit this authoritative resource. You can also find excellent resources like this job listing that may interest you.
Amazon IXD – VGT2
6401 E Howdy Wells Ave, Las Vegas, NV 89115
Leave a Reply