DEV Community

ajjindal
ajjindal

Posted on • Edited on

How to debug errors in Lambda functions

Troubleshooting production issues in Lambda environments is often challenging. CloudWatch is not easy to use to quickly identify and resolve issues. CloudWatch requires me to go into logs, search for relevant terms that I may not always know and has hard to consume stack traces.

I am using Sentry to instrument my Lambda Functions code in order to report error stack traces and associated debugging context.

Here's how I instrumented my Node function. If you are using Python environment, there are parallel docs.

Step 1: Create a dummy function to test the error reporting - I created a simple function that calls an unknown function.

const mySyncHandler = (event, context, callback) => {

  notDefinedFunc();
};
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Sentry to the Node folder - I used npm for this but you can use yarn as well

npm install --save @sentry/serverless
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize Sentry in my Lambda function - This required me to import Sentry's serverless package and wrap. my handler.

const Sentry = require("@sentry/serverless");

Sentry.init({
  dsn:
    "Get your dsn by creating a free account with Sentry",
});

const mySyncHandler = (event, context, callback) => {

  notDefinedFunc();
};

exports.handler = Sentry.AWSLambda.wrapHandler(mySyncHandler);
Enter fullscreen mode Exit fullscreen mode

That's it. I now have visibility into errors from my lambda functions.

The stack traces are detailed and point me to the line of code that's causing the exception.

Screen_Shot_2020-09-23_at_11.13.05_AM

Function context like aws_request_id and remaining_time_in_milis are super helpful. In addition, I have deep links to CloudWatch logs.

Screen_Shot_2020-09-23_at_11.13.28_AM

In addition, I made couple customizations to my error reporting:

  1. I really care about timeout issues. They end up bleeding into unknown issues that are difficult to debug. So, I enabled timeout warnings.

    exports.handler =     Sentry.AWSLambda.wrapHandler(yourHandler, {
      captureTimeoutWarning: false,
    });
    
  2. Also, while Sentry automatically captured breadcrumbs from console logs, I added some of my own

Sentry.addBreadcrumb({
  category: "auth",
  message: "Authenticated user " + user.email,
  level: Sentry.Severity.Info,
});
Enter fullscreen mode Exit fullscreen mode

With this instrumentation, I have visibility that's unparalleled to any other solution out there. I can quickly identify issues, reproduce issues in my local environment and get to root cause of the problem.

Please share if you have used a better way to monitor errors for your Lambda functions.

Top comments (1)

Collapse
 
netanelbasal profile image
Info Comment hidden by post author - thread only accessible via permalink
Netanel Basal

You can use the built-in AWS tools like CloudWatch or AWS X-Ray, but you will quickly see that it doesn’t give you everything you need to debug real-world issues quickly. I recommend using a third-party product such as Lumigo. Lumigo is Effortless AWS Lambda Monitoring. Here are its main features:

Find & Fix Issues in Seconds with Visual Debugging
Automatic Distributed Tracing
Identify & Remove Performance Bottlenecks
Serverless-Specific Smart Alerts
visualizes your entire serverless stack,
including all your favorite services
And many more!

You can try the free tier plan, and I'm sure you'll fall in love with it. Good luck!

Some comments have been hidden by the post's author - find out more