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.

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
EdgeGreenery:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs8.10

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:

const AWS = require(‘aws-sdk’);
const dynamodb = new AWS.DynamoDB();
dynamodb.endpoint = ‘http://host.docker.internal:8000’;
exports.handler = async (event) => {
return new Promise((resolve, reject) => {
console.log(‘? edge greenery lambda function invoked’);
const params = {
‘TableName’: ‘edge-greenery-table’,
};
dynamodb.scan(params, (err, data) => {
if (err) {
throw new Error(‘❌ Edge Greenery failed to connect to DynamoDB.’);
}
else {
if (data.Items.length) {
resolve(‘Edge Greenery Found Data!’);
} else {
throw new Error(‘❌ There is something wrong with this lambda.’);
}
}
});
});
};

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 v8Docker v18.03SAM CLI v0.3.0Go v1.10, and Python v2.7. Hope you have benefitted 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!

Read our blog

Introducing easy custom event monitoring for serverless applications.

Today we are excited to announce scheduled searches – a new feature on Dashbird that allows you to track any log event across your stack, turn it into time-series metric and also configure alert notifications based on it.

Why and How to Monitor Amazon OpenSearch Service

One of the most vital aspects to monitor is the metrics. You should know how your cluster performs and if it can keep up with the traffic. Learn more about monitoring Amazon OpenSearch Service.

Why and How to Monitor AWS Elastic Load Balancing

Dashbird recently added support for ELB, so now you can keep track of your load balancers in one central place. It comes with all the information you expect from AWS monitoring services and more!

More articles

Made by developers for developers

Dashbird was born out of our own need for an enhanced serverless debugging and monitoring tool, and we take pride in being developers.

What our customers say

Dashbird gives us a simple and easy to use tool to have peace of mind and know that all of our Serverless functions are running correctly. We are instantly aware now if there’s a problem. We love the fact that we have enough information in the Slack notification itself to take appropriate action immediately and know exactly where the issue occurred.

Thanks to Dashbird the time to discover the occurrence of an issue reduced from 2-4 hours to a matter of seconds or minutes. It also means that hundreds of dollars are saved every month.

Great onboarding: it takes just a couple of minutes to connect an AWS account to an organization in Dashbird. The UI is clean and gives a good overview of what is happening with the Lambdas and API Gateways in the account.

I mean, it is just extremely time-saving. It’s so efficient! I don’t think it’s an exaggeration or dramatic to say that Dashbird has been a lifesaver for us.

Dashbird provides an easier interface to monitor and debug problems with our Lambdas. Relevant logs are simple to find and view. Dashbird’s support has been good, and they take product suggestions with grace.

Great UI. Easy to navigate through CloudWatch logs. Simple setup.

Dashbird helped us refine the size of our Lambdas, resulting in significantly reduced costs. We have Dashbird alert us in seconds via email when any of our functions behaves abnormally. Their app immediately makes the cause and severity of errors obvious.