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

Wir verwenden Cookies, um unsere Webseite für Sie zu optimieren. Mit dem Besuch unserer Webseite erklären Sie sich damit einverstanden. // Our website is using cookies to improve your experience. By continuing to browse the site, you are agreeing to our use of cookies.

Weitere Informationen finden Sie in unserer Datenschutzerklärung. // For more information, please refer to our privacy policy.

contact icon

Contact us now