TP#134

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

In your sls configuration you have to define http events for all lambdas which should be included in the local gateway. In TypeScript, it would look like the following for a getCoffee function:
functions: {
   getCoffee: {
      events: [
        {
          // 'http' simulates the RestApi of AWS
          http: {
            method: 'get',
            path: '/coffee',
          },
        },
      ],
      handler: 'src/config/handlers/getCoffee',
    },
}
Create a Nodejs run/debug profile to start sls offline:
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
Set breakpoint in code and fire a request against the local gateway! Happy debugging!

Further Aspects

---

Author: Robert Gruner / Software Engineer / Office Leipzig

Local debugging of AWS lambdas? Serverless-offline to the rescue

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now