paint-brush
Debugging AWS Lambda Runtime Errorsby@akanksharana
1,302 reads
1,302 reads

Debugging AWS Lambda Runtime Errors

by AkankshaNovember 23rd, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Amazon CloudWatch is the central logging and monitoring service for all AWS services, including AWS Lambda. Using traditional practices such as remote debugging or locally simulating the environment is not an option. The only way to go about it is through the logs of each Lambda function. In a log stream, there are hundreds or thousands of logs from multiple invocations, all mixed up. We'll also look at an alternate way to debug your Lambda functions with KloudMate in mere minutes, through a quick demonstration.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Debugging AWS Lambda Runtime Errors
Akanksha HackerNoon profile picture


AWS Lambda (and serverless technologies in general) have made most aspects of designing and building applications easy, except for debugging such applications, which is far different from traditional monitoring methods. In this article, we talk about the challenges of debugging and troubleshooting Lambda functions, using Amazon CloudWatch.


We'll also look at an alternate way to debug your AWS Lambda functions with KloudMate in mere minutes, through a quick demonstration.

Debugging AWS Lambda with Amazon CloudWatch

Amazon CloudWatch is the central logging and monitoring service for all AWS services, including AWS Lambda.


Debugging AWS Lambda using traditional practices such as remote debugging or locally simulating the environment is not an option. Therefore, the only way to go about it is through the logs.


Amazon CloudWatch makes a dedicated log group for each Lambda function. Inside a log group, you will find log streams and inside a log stream, there are hundreds or thousands of logs from multiple invocations, all mixed up. So if you are looking for a particular log from a failed invocation, there's a good chance that you will end up spending precious time, sifting through hundreds or thousands of logs within Amazon CloudWatch.


Let's look at this entire process through a short demonstration.

Demonstration: Debugging a Runtime Error With CloudWatch

Let's write a sample Lambda code that produces a runtime error and see how we'll go about finding that error using CloudWatch.


exports.handler = async (event) => {

    console.log('This is a log line');

    event.run();

};


This Lambda has 2 lines of code:

  • The first line logs the text to stdout.
  • The second line throws an error because the event object has no function called run. The error thrown is "event.run is not a function".


By default, such logs and unhandled errors are sent to AWS CloudWatch. To view it on CloudWatch, you will need to select the log group for this lambda and then select a log stream.


Open the CloudWatch console, navigate to the log groups page, and select the Lambda function you want to analyze.


You will be taken to the log streams page for the selected Lambda function. Select a stream to view log events.


You will see multiple log events for the selected Lambda function. You can search and filter the logs on keywords and timestamps. Here you will have to sweep through all the logs to find the error.


You can go to the Log insights page and run queries against logs.


You can also go to the Lambda insights page to gain insights on the performance of Lambda based on metrics. Note that Lambda insights have to be enabled on CloudWatch by enabling Enhanced Monitoring.

As you can see the process of debugging Lambda using CloudWatch is quite cumbersome and non-intuitive. You'll need to spend quite a bit of time searching and navigating through multiple pages to pinpoint the exact execution you're interested in.


However, there is an alternative way to implement all of this in mere minutes or even seconds. Let's take a look.

Debugging AWS Lambda with KloudMate

KloudMate is an all-in-one Serverless Observability and Debugging platform that works on top of CloudWatch. It automatically gathers all the data and logs to provide centralized visibility into AWS Lambda and other AWS services.


KloudMate can be set up in less than 2 minutes and requires no code change or agent installation. It's designed to overcome all the limitations of AWS CloudWatch revolving around data representation, issue discovery, and resolution time, through comprehensive and intuitive dashboard views.


Following are some of the features within KloudMate to assist with debugging AWS Lambdas:

  • KloudMate's main dashboard is designed to give a bird's eye view of your entire serverless stack. You will see recent errors, top errored functions, recent alarms, and more as soon as you open up KloudMate.
  • You can view all the handled (logged) and unhandled errors & warnings on KloudMate's Issues page without any further action.
    • You can click on any issue to view the stack trace and invocation associated with the issue, you can also view details of previous occurrences of this issue.
  • Further, if you want to search through your logs it can be done on KloudMate's Logs explorer.
    • You can use KloudMate's full-text log search to look for logs by Lambda name, relevant keywords, execution id and more.
    • Logs can be filtered by functions, error type, date & time range

Demonstration: Debugging a Runtime Error With KloudMate

Let's use the same sample Lambda code that we used in the CloudWatch demonstration.


exports.handler = async (event) => {

    console.log('This is a log line');

    event.run();

};


This Lambda is supposed to throw the error "event.run is not a function".


Now, as soon as you open KloudMate, you will see the Lambda function that is throwing the error listed under the Top Errored Functions, right there on the main dashboard. The error will also be listed under Recent Issues in the main dashboard itself.

KloudMate Dashboard

This automatic error detection and centralized access to the errors will save you hours of manual digging through the logs to get to the error.


You can click on the error in the main dashboard or open up the Issues page, this error will be listed under the All Unresolved list of the issues, as shown below.

KloudMate Issues Page

It will also display the total number of occurrences and the severity level.

Note: KloudMate provides default notifications for fatal errors. You can also configure entirely customizable alarms as per your needs.


If you click on the issue shown in the above screenshot, you can view further details of the error.

KloudMate Issues Details


You can click on the View Invocation button to view the latest invocation of the issue.

KloudMate Log Details


You can navigate to the Lambda section to view all the recent invocations for this particular Lambda. You can also view a set of aggregated metrics for this Lambda function presented in graphical representation.

KloudMate Lambda Section


And if you are looking for the invocations of all your Lambdas, you can navigate to the Logs page in KloudMate.

KloudMate Log Explorer

Closing Notes

KloudMate is a purpose-built platform to help developers monitor all their Lambda functions centrally rather than manually digging through mountains of log files in Amazon CloudWatch. The use of KloudMate has proven to reduce the error & issue discovery time by up to 80%. If you've gone off the deep end trying to debug AWS Lambda using Amazon CloudWatch, you might want to check out KloudMate.


Many thanks to Vinay Gaddam for providing the Lambda code and the debugging demonstrations.


For more such articles, you can also visit https://blog.kloudmate.com/