In this tutorial, you do the following:
- Create a Lambda function using the lambda-canary blueprint. You configure the Lambda function to run every minute. Note that if the function returns an error, AWS Lambda logs error metrics to CloudWatch.
- Configure a CloudWatch alarm on the
Errors
metric of your Lambda function to post a message to your Amazon SNS topic when AWS Lambda emits error metrics to CloudWatch. You subscribe to the Amazon SNS topics to get email notification. In this tutorial, you do the following to set this up:- Create an Amazon SNS topic.
- Subscribe to the topic so you can get email notifications when a new message is posted to the topic.
- In Amazon CloudWatch, set an alarm on the
Errors
metric of your Lambda function to publish a message to your SNS topic when errors occur.
This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda console. If you haven't already, follow the instructions in Getting started with AWS Lambda to create your first Lambda function.
-
Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.
-
Choose Create function.
-
Choose Blueprints.
-
Enter canary in the search bar. Choose the lambda-canary blueprint, and then choose Configure.
-
Configure the following settings.
- Name – lambda-canary.
- Role – Create a new role from one or more templates.
- Role name – lambda-apigateway-role.
- Policy templates – Simple microservice permissions.
- Rule – Create a new rule.
- Rule name – CheckWebsiteScheduledEvent.
- Rule description – CheckWebsiteScheduledEvent trigger.
- Schedule expression – rate(1 minute).
- Enabled – True (checked).
- Environment variables
- site – **https://docs\.aws\.amazon\.com/lambda/latest/dg/welcome\.html**\.
- expected – What is AWS Lambda?.
-
Choose Create function.
CloudWatch Events emits an event every minute, based on the schedule expression. The event triggers the Lambda function, which verifies that the expected string appears in the specified page. For more information on expressions schedules, see Schedule expressions using rate or cron.
Test the function with a sample event provided by the Lambda console.
-
Open the Lambda console Functions page.
-
Choose lambda-canary.
-
Next to the Test button at the top of the page, choose Configure test events from the drop-down menu.
-
Create a new event using the CloudWatch Events event template.
-
Choose Create.
-
Choose Test.
The output from the function execution is shown at the top of the page.
Create an Amazon Simple Notification Service (Amazon SNS) topic to receive notifications when the canary function returns an error.
To create a topic
-
Open the Amazon SNS console.
-
Choose Create topic.
-
Create a topic with the following settings.
- Name – lambda-canary-notifications.
- Display name – Canary.
-
Choose Create subscription.
-
Create a subscription with the following settings.
- Protocol – Email.
- Endpoint – Your email address.
Amazon SNS sends an email from Canary <[email protected]>
, reflecting the friendly name of the topic. Use the link in the email to confirm your address.
Configure an alarm in Amazon CloudWatch that monitors the Lambda function and sends a notification when it fails.
To create an alarm
-
Open the CloudWatch console.
-
Choose Alarms.
-
Choose Create alarm.
-
Choose Alarms.
-
Create an alarm with the following settings.
-
Metrics – lambda-canary Errors.
Search for lambda canary errors to find the metric.
-
Statistic – Sum.
Choose the statistic from the drop-down menu above the preview graph.
-
Name – lambda-canary-alarm.
-
Description – Lambda canary alarm.
-
Threshold – **Whenever Errors is >=**1.
-
Send notification to – lambda-canary-notifications.
-
Update the function configuration to cause the function to return an error, which triggers the alarm.
To trigger an alarm
-
Open the Lambda console Functions page.
-
Choose lambda-canary.
-
Under Environment variables, choose Edit.
-
Set expected to 404.
-
Choose Save.
Wait a minute, and then check your email for a message from Amazon SNS.