Using Lumigo to debug AWS Lambda timeouts

Tags: aws, tutorial, debugging
Published: 2021-11-19 18:38:53

Most [AWS Lambda](https://aws.amazon.com/lambda/) functions don't perform CPU-intensive tasks that take a long time to complete. However, they often have to perform multiple input/output (I/O) operations in a single invocation. An *invocation* occurs when your application invokes a Lambda function. For example, they fetch data from an [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) table, talk to third-party APIs such as Stripe, or communicate with other internal APIs in your application. Sometimes these I/O operations don't complete quickly, and your function times out while waiting for a response.

A Lambda function's timeout is one of the trickier problems to debug for the following reasons:

-   Lambda doesn't report a separate error metric for timeouts, so timeout errors are mixed in with other, generic errors.
-   Lambda functions often have to perform multiple I/O operations during an invocation, so there is more than one potential reason for the timeout.
-   You have to rely on the absence of an indicator to tell you that the function timed out waiting for something to finish. This requires discipline to consistently signpost the start and end of every operation.

In this blog post, I show how to subscribe to and deploy [Lumigo](http://lumigo.io/), available in [AWS Marketplace](https://aws.amazon.com/marketplace/seller-profile?id=33b56061-d85b-48bd-8b54-7f9d157c5ace), which helps you detect and troubleshoot Lambda timeouts.

Prerequisites
-------------

To use this solution, you need an AWS account with Lambda functions.

Runtime environments supported:

-   JS-supported runtimes: 8.10, 10.x, 12.x, 14.x
-   Python-supported runtimes: 3.6, 3.7, 3.8
-   JVM-supported runtimes: Java 8
-   .net-supported runtimes: 2.1, 3.1

Solution walkthrough
--------------------

Connecting Lumigo to your account is automated. It takes a few minutes with no manual code changes.

### A.   Subscribe to Lumigo

To subscribe to Lumigo, do the following:

1.  In the AWS Marketplace Management Console search bar, enter Lumigo.
2.  On the Lumigo product page, choose Continue to subscribe.
3.  Under Contract options, choose the tier that best answers your requirements, and on the right side choose to Create Contract.
4.  In the dialog box, choose Pay now. Note you can also choose the Free Community tier which is a free tier, so you will not be charged for your traced Lambda invocations.
5.  Choose Set up your account.

### B.   Deploy Lumigo

To deploy Lumigo, do the following:

1.  After completing step A, you are redirected to Lumigo's website. In the top right, choose Get Started.
2.  Choose a username (email) and password.
3.  Enter your name and company name.
4.  Choose Connect Lumigo. This deploys an [AWS CloudFormation](https://aws.amazon.com/cloudformation/) template to your account and provisions the necessary [IAM](https://aws.amazon.com/iam/) role so Lumigo can monitor your application.
5.  Wait for the stack to complete.
6.  Through the onboarding wizard, select all of the functions you want to trace with Lumigo and choose Auto-Trace. After the onboarding, you can trace more functions via the Function tab in the navigation pane.

Congratulations! Now you can gain more in-depth insight into what your serverless application is doing and debug problems in production quickly.

Lumigo has a [free tier](https://lumigo.io/pricing/), and you can trace up to 150,000 Lambda invocations per month for free. If you require more than that, you can trace up to 10 million Lambda invocations per month by subscribing to [one of our plans](https://aws.amazon.com/marketplace/saas/ordering?productId=6f8f306f-7a31-4067-a5e8-a1a39ee00a01&offerId=6lrjefxoirj1smxi2nlelqept) in AWS Marketplace.

How to debug Lambda timeouts
----------------------------

After you log in to Lumigo, in the navigation pane, choose the Issues tab. This page labels timeout issues against the relevant functions.

Choosing one of the issues takes you to the Functions page, which presents high-level information and metrics about the function. This page highlights the timed-out invocations. Choosing one of them directs you to the Transaction page. Lumigo can trace invocations through [Amazon SNS](https://aws.amazon.com/sns/), [Amazon SQS](https://aws.amazon.com/sqs/), [Amazon Kinesis Data Streams](https://aws.amazon.com/kinesis/data-streams/), [DynamoDB Streams](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html), [Amazon EventBridge](https://aws.amazon.com/eventbridge/), and more.

The Transaction page displays the entire end-to-end transaction, including the function that timed out. The page contains the logs for all of the functions that were part of this transaction and a visual diagram of the architecture components involved in the transaction. Choosing the Timeline tab on the Transaction page displays a timeline of everything that happened during this transaction. Operations that are never completed are labeled N/A.

The following image shows an example of a timeout and its root cause: the HTTP endpoint didn't respond within six seconds.

![Lumigo timeout and root cause example](https://d2908q01vomqb2.cloudfront.net/761f22b2c1593d0bb87e0b606f990ba4974706de/2021/10/19/Lumigo-blackk-arrow.png)

To determine if this is a one-off occurrence or a systemic performance issue with this service, in the navigation pane, choose Dashboard. The Service Latency widget on the bottom right shows you the tail latency for services that you're calling from your Lambda functions, as shown in the following image. By default, this widget is sorted by the p95 (ms) column. It enables you to identify slow dependencies. The following screenshot shows the dashboard with the Service Latency widget highlighted.

![Lumigo dashboard with Service Latency highlighted](https://d2908q01vomqb2.cloudfront.net/761f22b2c1593d0bb87e0b606f990ba4974706de/2021/11/12/Screenshot-2-High-resolution-.png)

Choosing any of the latency values takes you to the Explore page, displaying the relevant transactions where those tail latencies were recorded. This enables you to identify other cases where this service has responded slowly. Based on this information, you can make a more informed decision about whether to optimize this service (if it's an internal API) or replace it (if it's a third-party service).

Conclusion
==========

In this blog post, I showed how to use Lumigo to debug timeout errors with Lambda and identify slow dependencies. Lumigo requires no manual instrumentation, and you can [sign up today](https://lumigo.io/pricing/) for a free account and trace up to 150,000 Lambda invocations.