How to create Cloudwatch alarms for a Lambda Function on AWS

There are various invocation metrics, performance metrics, and concurrency metrics available for Lambda functions in Cloudwatch to monitor. Invocation metrics are the outcome of an invocation and binary in nature, performance details about a single invocation are provided by Performance metrics.

  1. Invocations – The number of times the Lambda function code is executed, including successful executions and unsuccessful executions that result in a function error.
  2. Errors – This returns the number of unsuccessful invocations that result in a function error that includes exceptions thrown by the Lambda code and exceptions thrown by the Lambda runtime. 
  3. DeadLetterErrors – This returns the number of times Lambda attempts to send an event to a dead-letter queue but fails for asynchronous invocation.
  4. DestinationDeliveryFailures – This returns the number of times Lambda attempts to send an event to a destination but fails for asynchronous invocation.
  5. Throttles – This gives the number of invocations that are throttled.
  6. ProvisionedConcurrencyInvocations – This gives the number of times the Lambda function code is executed on provisioned concurrency.
  7. ProvisionedConcurrencySpilloverInvocations – This gives the number of times the Lambda function code is executed on standard concurrency when all provisioned concurrency is in use.
  8. Duration – This returns the amount of time that the Lambda function code spends processing an event.
  9. PostRuntimeExtensionsDuration – This is the cumulative amount of time that the Lambda function runtime spends running code for extensions after the function code has completed.
  10. IteratorAge – This is the age of the last record in the event for event source mappings that read from streams.
  11. ConcurrentExecutions – This gives the number of Lambda function instances that are processing events.
  12. ProvisionedConcurrentExecutions – This returns the number of Lambda function instances that are processing events on provisioned concurrency.
  13. ProvisionedConcurrencyUtilization – This returns the value of ProvisionedConcurrentExecutions divided by the total amount of provisioned concurrency allocated for a version or alias.
  14. UnreservedConcurrentExecutions – This returns the number of events being processed by Lambda functions that do not have reserved concurrency.

Click here to know more about metrics available for Lambda functions.

In this article, we will create an alarm for the "Invocations" metric which will send a notification to the SNS topic when it is triggered. Before we proceed, it is assumed that you are familiar with Lambda functions and SNS topics. Also, it is assumed that you already have a Lambda function and an SNS topic.

Pre-requisites

  1. AWS Account(Create if you don’t have one).
  2. Lambda function(Click here to learn to create a Lambda function to start and stop an EC2 instance).
  3. SNS Topic(Click here to learn to create an SNS Topic using Cloudformation)

What will we do?

  1. Login to AWS.
  2. Create an alarm for the existing Lambda function.

Login to AWS

Click here to go to AWS Login Page.

You will see the login page as follows when you hit the above link. Enter your credentials to login into your AWS account.

Login screen

You will see the main AWS Management Console as follows when you successfully login into your account.

AWS Management console

Create an alarm for the existing Lambda function

To create an alarm for an existing Lambda function, go to the Cloudwatch dashboard. Search for Cloudwatch in the search box at the top of the screen.

Search Cloudwatch

You will see the overview screen of Cloudwatch as follows. Click on "Alarms" in the left panel.

Cloudwatch Dashboard

If you have any existing alarms in your account under the selected region, you will see them here. To create a new alarm for an existing lambda function click on the "Create alarm" button.

Alarms Screen

You will get a screen to specify a metric and conditions. Click on the "Select metric" button.

Specify metric and conditions

Here you will find services with metrics available for them. 

Select Metric

Filter the metrics by Lambda. Now you will see metrics available for  Lambda functions. Click on "Lambda  > By Function Name".

Browse Lambda

Select any one of the metrics available for the existing lambda functions. Here I will proceed with the "Invocations" metric. Click on the "Select metric" to proceed.

Select Metric

Choose Statistic, Threshold Type, Alarm Condition, and Threshold Value. Here, the alarm will be triggered if the Sample Count of the Invocations is greater than 1 for 5 minutes. 

Specify metric and conditions

Choose the Alarm state trigger and the SNS topic. Here, the alarm will move to the "In alarm" state when triggered and send a notification to the specified SNS topic.

Configure actions

Add name and description to the alarm to be created and click on the "Next" button to proceed.

Add name and description\

Review all the configurations and click on the "Create alarm" button if everything looks fine. 

Preview and create

Once the alarm is created you can see it under the "Alarms" console. The state of the alarm will change based on the condition and threshold.

Alert in Ok state

Conclusion

In this article, we created an alarm of the existing Lambda function to monitor the "Invocations" metric. The alarm we created gets triggered when "Sample count" is "Greater" than threshold "1" for "5 minutes" and sends a notification to the subscription in the SNS topic. You can now use the same steps to create alarms for different metrics, statistics, and thresholds.

Share this page:

0 Comment(s)