Debugging Serverless Applications can be a bit of a headache...
We use NodeJs a lot, we also use a lot of CI/CD pipelines to automate the tedious stuff for us, so this brings us to a point where we need to debug things locally because if you are reading this article I am sure you know how much of a pain it is to debug things on AWS lambda.
- Run Serverless Deploy
- Test APIGateway
- Check Cloudwatch Logs
- Scratch through cloudwatch to find the correct log
By the end of all of this you are pretty frustrated.
The solution is serverless-offline and a bit of configuration, install serverless offline using
npm install -D serverless-offline
yarn add -D serverless-offline
Then include it in your serverless.yml under plugins along with your other plugins, I added serverless-webpack for my TS project
# Add the serverless-webpack plugin
Once this is done setup your package.json scripts to make a life a little easier.
"start": "serverless offline -s dev",
To configure the debug environment within VS Code, on the left hand side click the debug menu items as highlighted below and then click the gear at the top
VS Code will then open a launch.json file which will allow you to configure your debug environment
The snippet below assumes that you have serverless installed locally to your project and not globally, to do this run npm install -D serverless or yarn add -D serverless
Here is my debug configuration
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"name": "Launch Serverless Offline",
// Here we set some environment vars that should be set locally.
// They can and will overwrite the ones coming from your serverless.yml
In the example above, I included the "--aws-profile=#PROFILENAME#" argument, This is so that serverless knows which AWS profile I am using for deployment
Run your configured debug and test to see if everything runs as it should