Local debugging of AWS lambdas? Serverless-offline to the rescue
Problem
You have arrived in the modern era and write your business code serverless now? Great! Single lambda functions run in the Amazon infrastructure and you only have to write a few lines of JavaScript in the AWS console. But is that what you want? Maybe you prefer to use an IDE and write TypeScript? Sure. But how do I know that my lambda code is working without deploying it to AWS?
Solution
Use the tool serverless (short: sls) in combination with the plugin serverless-offline! In general, sls helps you with the local development of lambdas, the deployment to AWS etc. There are several plugins – one of them is serverless-offline. This plugin simulates a local AWS API gateway. This gateway links single lambda functions and you can test it locally e.g. with Postman or IDEA Scratch File.
Example
functions: {
getCoffee: {
events: [
{
// 'http' simulates the RestApi of AWS
http: {
method: 'get',
path: '/coffee',
},
},
],
handler: 'src/config/handlers/getCoffee',
},
}
Working directory: ~/Code/CoffeeLambdas
JavaScript file: ~/.nvm/versions/node/v12.14.1/bin/serverless # npm global binary file for sls
Application parameters: offline start --aws-profile jambit --stage dev
Environment variables: SLS_DEBUG=* # activate debug output to get more details
Further Aspects
- https://www.serverless.com/framework/docs/providers/: Does not only support AWS but also Azure, Google etc.
- https://github.com/dherault/serverless-offline: Source of the recommended plugin
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html: Distinction between API gateway types
- https://www.jetbrains.com/help/idea/run-debug-configuration.html: Documentation on run/debug profile configuration in IntelliJ IDEs
- https://www.jetbrains.com/help/idea/scratches.html: Documentation on Scratch Files
---
Author: Robert Gruner / Software Engineer / Office Leipzig
Download Toilet Paper #134: Local debugging of AWS lambdas (pdf)
Want to write the next ToiletPaper? Apply at jambit!