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.
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.
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:
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.
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:
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.
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.
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.
You can click on the View Invocation button to view the latest invocation of the issue.
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.
And if you are looking for the invocations of all your Lambdas, you can navigate to the Logs page in KloudMate.
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/