Join the webinar. Best practices and open Q&A session - Register here!

Responding to AWS Lambda Errors with Serverless Application Model (SAM) Tools

When a lambda function errors, a developer can now respond with new tools. Lambda developers can execute their lambda function off the cloud with Amazon Web Services (AWS) Serverless Application Model (SAM) tools. SAM tools execute your lambda function on your machine in Docker containers provided by AWS.

Why Run Lambda Functions Locally

This is very helpful because it is similar to the environment that AWS Lambda functions run in on the cloud ‐ in containers managed by AWS Lambda. The lambda functions generate logs locally to the console instead of sending them to AWS Cloudwatch. SAM tools will also launch AWS API Gateway locally, which is helpful for developers who execute lambda functions through http endpoints hosted on AWS API Gateway. SAM tools provide a richer execution environment on your laptop than running Node.js alone.

Dashbird's Error Notifications

Here's a developer operations (devops) scenario: Dashbird notifies a developer than a one of the company's lambda function called 'EdgeGreenery' errored. The developer browses to Dashbird.io to review the error. Note: the developer's lambda function logs stack traces to AWS Cloudwatch. That's why Dashbird can extract the stack trace from the Cloudwatch logs and display it on the website. After reviewing the stack trace, the developer also reviews the frequency of the error, which Dashbird helpfully counts. Now, how does a developer efficiently respond?

Responding to an Error

To debug the error, the developer's strategy is to reproduce the problem locally on a laptop. First, they install the Serverless Application Model (CLI) tools, a set of Python v2.7 compatible scripts.

pip install --user aws-sam-cli

The package aws-sam-cli is a tool for local development and testing of Serverless applications. In the SAM template below, the developer describes an execution environment, more precisely Node.js 8.10 and launches the index.js file.

The lambda function also uses DynamoDB. So, the developer downloads DynamoDB and runs it locally with Java 8, with an endpoint at http://localhost:8000.

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

With AWS command line tools, the developer creates a table locally for the lambda function.

aws dynamodb create-table --endpoint-url http://localhost:8000 --cli-input-json file://edge-greenery-schema.json

They then invoke the lambda function locally with sam local invoke. Here's the final command with sending an empty JSON payload via stdin.

echo "{}" | sam local invoke

Have a look at the source code for the lambda function below:

The console outputs a stack trace from the app. Let's fix it! The app errors. There is no data in the table. The database and the lambda function are running on the laptop. The developer can efficiently improve the lambda function. For example, the app could gracefully handle an empty table. How easy!

Summary

In this simplified example, we show how to run a lambda function and DynamoDB locally. This shows an execution environment on a Macbook. It has lots of software including Java v8, Docker v18.03, SAM CLI v0.3.0, Go v1.10, and Python v2.7. Hope you will have a benefit from this short guide. Please see the GitHub project edge greenery to check out the code.


We aim to improve Dashbird every day and user feedback is extremely important for that, so please let us know if you have any feedback about these improvements and new features! We would really appreciate it!

Keep up to speed on the latest serverless trends!

Write for us!

We're looking for developers to share their experience with Serverless.

Emails and pull requests welcome!

Start using Dashbird for free!

Failure detection, analytics and visibility for serverless applications in under 5 minutes.

Request Demo