Monitoring and TroubleShooting using Cloudwatch



Functions created in AWS Lambda are monitored by Amazon CloudWatch. It helps in logging all the requests made to the Lambda function when it is triggered.

Consider that the following code is uploaded in AWS Lambda with function name as lambda and cloudwatch.

exports.handler = (event, context, callback) => {
   // TODO implement
   console.log("Lambda monitoring using amazon cloudwatch");    
   callback(null, 'Hello from Lambda');
};

When the function is tested or triggered, you should see an entry in Cloudwatch. For this purpose, go to AWS services and click CloudWatch.

Click Cloudwatch

Select logs from left side.

Select Left Side

When you click Logs, it has the Log Groups of AWS Lambda function created in your account. Select anyAWS Lambda function and check the details. Here, we are referring to Lambda function with name:lambdaandcloudwatch. The logs added to the Lambda function are displayed here as shown below −

Log Groups

Now, let us add S3 trigger to the Lambda function and see the logs details in CloudWatch as shown below −

S3 Trigger

Let us update AWS Lambda code to display the file uploaded and bucket name as shown in the code given below −

exports.handler = (event, context, callback) => {
   // TODO implement
   console.log("Lambda monitoring using amazon cloudwatch");
   const bucket = event.Records[0].s3.bucket.name;
   const filename = event.Records[0].s3.object.key;
   const message = `File is uploaded in - ${bucket} -> ${filename}`;
   console.log(message);
   callback(null, 'Hello from Lambda');
};

Now, add file in s3storetestlambdaEventbucket as shown −

S3 Store Test

When the file is uploaded, AWS Lambda functions will get triggered and the console log messages from Lambda code are displayed in CloudWatch as shown below −

Cloudwatch Display

If there is any error, CloudWatch gives the error details as shown below −

Cloudwatch Details

Note that we have referred to the bucket name wrongly in AWS Lambda code as shown −

exports.handler = (event, context, callback) => {
   // TODO implement
   console.log("Lambda monitoring using amazon cloudwatch");
   const bucket = event.Records[0].bucket.name;
   const filename = event.Records[0].s3.object.key;
   const message = `File is uploaded in - ${bucket} -> ${filename}`;
   console.log(message);
   callback(null, 'Hello from Lambda');
};

The bucket name reference from the event is wrong. Thus, we should see an error displayed in CloudWatch as shown below −

Error Displayed

CloudWatch Metrics

The details of the Lambda function execution can be seen in the metrics. Click Metrics displayed in the left side.

Metrics Displayed

All Metrics

The graph details for the lambda function lambdaandcloudwatch are as shown below −

Graphed Metrics

Graph Details Graph Details Lambda

It gives details such as the duration for which the Lambda function is executed, number of times it is invoked and the errors from the Lambda function.

Advertisements