Running and debugging Lambda functions directly from code
When testing the AWS SAM application, you can choose to run and debug just the Lambda
function and exclude other resources that the AWS SAM template defines. This approach involves
using the CodeLens
The Lambda handlers that are detected by CodeLens depend on the language and runtime that you're using for your application.
Language/runtime | Criteria for Lambda functions to be identified by CodeLens indicators |
---|---|
C# (dotnetcore2.1, 3.1; .NET 5.0) |
The function has the following features:
The ms-dotnettools.csharp extension |
JavaScript/TypeScript (Node.js 12.x, 14.x) |
The function has the following features:
|
Python (3.7, 3.8, 3.9, 3.10, 3.11, 3.12) |
The function has the following features:
The ms-python.python extension |
Java (8, 8.al2, 11) |
The function has the following features:
The redhat.java extension The vscjava.vscode-java-debug |
Go (1.x) |
The function has the following features:
The golang.go extension |
To run and debug a serverless application directly from the application code
-
To view your application files in the VS Code File Explorer, choose View, Explorer.
-
From the application folder (for example, my-sample-app), expand the function folder (in this case, hello-world) and open the
app.js
file. -
In the CodeLens indicator that identifies an eligible Lambda function handler, choose
Add Debug Configuration
. -
In the Command Palette, select the runtime in which your AWS SAM application will run.
-
In the editor for the
launch.json
file, edit or confirm values for the following configuration properties:-
"name"
– Enter a reader-friendly name to appear in the Configuration dropdown field in the Run view. -
"target"
– Ensure that the value is"code"
so that a Lambda function handler is directly invoked. -
"lambdaHandler"
– Enter the name of the method within your code that Lambda calls to invoke your function. For example, for applications in JavaScript, the default isapp.lambdaHandler
. -
"projectRoot"
– Enter the path to the application file that contains the Lambda function. -
"runtime"
– Enter or confirm a valid runtime for the Lambda execution environment, for example,"nodejs.12x"
. -
"payload"
– Choose one of the following options to define the event payload that you want to provide to your Lambda function as input:-
"json"
: JSON-formatted key-value pairs that define the event payload. -
"path"
: A path to the file that's used as the event payload.
In the example below, the
"json"
option defines the payload. -
For more information about these and other entries in the
launch.json
file, see Configuration options for debugging serverless applications. -
-
If you're satisfied with the debug configuration, to start debugging, choose the green play arrow next to RUN.
When the debugging sessions starts, the DEBUG CONSOLE panel shows debugging output and displays any values that the Lambda function returns. (When debugging AWS SAM applications, AWS Toolkit is selected as the Output channel in the Output panel.)