- Published on
Set Breakpoints to Locally Debug AWS Lambda
- Authors

- Name
- Duncan Leung
- @leungd
Debugging Use Case
Serverless functions can be invoked locally with the Serverless CLI.
$ sls invoke local
If you set up your functions to write event payloads into your CloudWatch logs, the logged event payload can be used as the local event payload with the --path flag.
Using the VSCode debugger:
You can set breakpoints to step-through and inspect your function during runtime with the exported payload that caused your function to error.
Run a Serverless function locally
In this exampole we're invoking the function named index with the flag --function. The function name index is specified by the serverless.yml.
We're also passing a data payload with the flag, --path, which points to a local json file that will be passed to the function as an event payload.
## --function | -f: Name of the function
## --path | -p: Path to a json file to be used as the event input data
$ sls invoke local --function index --path 'logs-export.json'
## Output
{
"statusCode": 200,
"body": "{\n
\"message\": \"Running function, \"index\" \",\n
\"input\": {\n \"foo\": \"bar\"\n }
\n}"
}
name: aws
runtime: nodejs12.x
functions:
index:
handler: functions/index.handler
events:
- http:
path: /
method: get
return {
statusCode: 200,
body: JSON.stringify(
{
message: `Running function, "index"`,
input: event,
},
null,
2
),
};
};
Attaching a Visual Studio Code debugger
Since we're invoking the function locally, we can also attach a VSCode debugger breakpoint.
- Install serverless as a local dependency to project
$ yarn add -D serverless
- Set up a new VSCode
launch.json
The main part of the launch.json is to:
- Point the
programto the locally installed serverless dependency innode_modules - Pass in the same arguments that we used to run
sls invoke local
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/node_modules/.bin/sls",
"args": ["invoke", "local", "-f", "hello", "-p", "logs-export.json"]
}
]
}
- Set up a breakpoint locally in VSCode
- Use VSCode debugger to run the created launch.json