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, Lambda logs error metrics to Amazon 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 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 Use a blueprint.
-
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 AWS policy templates.
- Role name – lambda-apigateway-role.
- Policy templates – Simple microservice permissions.
- Rule – Create a new rule.
- Rule name – CheckWebsiteScheduledEvent.
- Rule description – CheckWebsiteScheduledEvent trigger.
- Rule type – Schedule expression.
- Schedule expression – rate(1 minute).
- Environment variables
- site – **https://docs\.aws\.amazon\.com/lambda/latest/dg/welcome\.html**\.
- expected – What is AWS Lambda?.
-
Choose Create function.
EventBridge (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 Functions page on the Lambda console.
-
Choose the lambda-canary function.
-
Choose Test.
-
Create a new event using the CloudWatch event template.
-
Choose Create event.
-
Choose Invoke.
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 Functions page on the Lambda console.
-
Choose the lambda-canary function.
-
Scroll down. 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.
You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you're no longer using, you prevent unnecessary charges to your AWS account.
To delete the Lambda function
-
Open the Functions page of the Lambda console.
-
Select the function that you created.
-
Choose Actions, then choose Delete.
-
Choose Delete.
To delete the CloudWatch alarm
-
Open the Alarms page of the CloudWatch console.
-
Select the alarm you created.
-
Choose Actions, Delete.
-
Choose Delete.
To delete the Amazon SNS subscription
-
Open the Subscriptions page of the Amazon SNS console.
-
Select the subscription you created.
-
Choose Delete, Delete.
To delete the Amazon SNS topic
-
Open the Topics page of the Amazon SNS console.
-
Select the topic you created.
-
Choose Delete.
-
Enter delete me in the text box.
-
Choose Delete.