Running and debugging AWS Lambda functions locally with the Serverless framework and VS Code
One of the complaints developers often have for AWS Lambda is the inability to run and debug functions locally. For Node.js at least, the Serverless framework and VS Code provides a good solution for doing just that.
An often underused feature of the Serverless framework is the invoke local command, which runs your code locally by emulating the AWS Lambda environment. Granted, it’s not a perfect simulation and only works with Node.js and Python, but it has been good enough for most of local development needs.
Serverless Framework Commands - AWS Lambda - Invoke Local
Emulate an invocation of your AWS Lambda function locally using the Serverless Framework
With VS Code, you have the ability to debug Node.js applications, including the option to launch an external program.
Debug Node.js Apps using VS Code
The Visual Studio Code editor includes Node.js debugging support. Set breakpoints, step-in, inspect variables and more.
Put the two together and you have the ability to locally run and debug your Lambda functions.
Step 1 : install Serverless framework as dev dependency
In general, it’s a good idea to install Serverless framework as a dev dependency in a project because:
- it allows other developers (and the CI server) to use the Serverless framework for deployment without having to install it themselves
- it prevents incompatibility issues when you have an incompatible version of Serverless framework installed to that used by the
serverless.ymlfile in the project
- since Serverless v1.16.0 dev dependencies are excluded from the deployment package so it wouldn’t add to your deployment size
Step 2 : add debug configuration
Step 3 : enjoy!
There, nice and easy :-)
Couple of things to note:
- if your function depends on environment variables, then you can set those up in the
launch.jsonconfig file in step 2
- if your function needs to access other AWS resources, then you also need to setup the relevant environment variables (eg.
AWS_PROFILE) for the aws-sdk to access those resources in the correct AWS account
- this approach will not work for recursive functions (well, the recursion will happen on the deployed Lambda function, so you won’t be able to debug it)
Hi, my name is Yan Cui. I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
Check out my new course, Complete Guide to AWS Step Functions.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
Get your copy here.
Come learn about operational BEST PRACTICES for AWS Lambda: CI/CD, testing & debugging functions locally, logging, monitoring, distributed tracing, canary deployments, config management, authentication & authorization, VPC, security, error handling, and more.
You can also get 40% off the face price with the code ytcui.
Get your copy here.