Using CloudWatch to Create Alarms
Overview: CloudWatch can be used in combination SNS so you can be notified when specific events or issues occur in your Lambda function. In this case Errors in a Lambda function. Alarms can be raised if the number of errors is ever over a predefined amount and they can either be by a per function basis or overall created Lambda functions.
AWS Cost: $0.10 per alarm
Creating the SNS (Used to send emails):
- Navigate to the AWS SNS > Topic.
- Click “Create New Topic” and fill in the fields to create a topic.
- Once you have created the topic click on the newly created topic in the topics page.
- Once in the topic details page, click “Create Subscription” and add the email or emails you want the alerts to be sent to.
- A confirmation email will then be sent to the email endpoint which needs to be accepted to receive emails from SNS.
Creating the Alert:
- Navigate to AWS’s service CloudWatch > Alarms > Create Alarm
- Select Lambda Metric “By Function Name”.
- Select the Lambda Function you want to create the alarm for with the Metric Errors, select Sum from the dropdown, and then click next to further define the alarm.
- Give the Alarm a name and description and set the period (Usually do 1 hour).
- The best way to determine the what to set the threshold is for the number of error is to check the Lambda function to see how many times it is called within the time period you selected and use a percentage (10%-25%) to determine how the number of errors that will trigger the alert. (Ex Function is called 100 times and hour and you want an alert if errors are ever 15% then you would set the threshold to 15).
- Lastly, in the actions section select the SNS topic you created earlier and create the Alarm.