Tutorial: Using Amazon Lambda with scheduled events
In this tutorial, you do the following:
-
Create a Lambda function using the Schedule a periodic check of any URL 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 Amazon 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.
-
Prerequisites
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 Create a Lambda function with the console to create your first Lambda function.
Create a Lambda function
Sign in to the Amazon Web Services Management Console and open the Amazon Lambda console at https://console.amazonaws.cn/lambda/
. -
Choose Create function.
-
Choose Use a blueprint.
-
Open the Select blueprint dropdown list and choose the Schedule a periodic check of any URL blueprint.
-
Configure the following settings.
-
Function name –
lambda-canary
. -
Execution role – Create a new role from Amazon policy templates.
-
Role name –
lambda-eventbridge-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.amazonaws.cn/lambda/latest/dg/welcome.html
-
expected –
What is Amazon 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 Lambda function
Test the function with a sample event provided by the Lambda console.
Open the Functions page
of the Lambda console. -
Choose the lambda-canary function.
-
Choose Test.
-
Create a new event using the CloudWatch event template (cloudwatch-scheduled-event).
-
Choose Create event.
-
Choose Invoke.
The output from the function execution is shown at the top of the page.
Create an Amazon SNS topic and subscribe to it
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
. -
Switch to the Amazon Web Services Region where you created the Lambda function.
-
Choose Topics, and then choose Create topic.
-
Create a topic with the following settings.
-
Type – Standard.
-
Name –
lambda-canary-notifications
. -
Display name –
Canary
.
-
-
Choose Create topic.
-
On the lambda-canary-notifications topic page, choose Create subscription.
-
Create a subscription with the following settings.
-
Protocol –
Email
. -
Endpoint – Your email address.
-
-
Choose Create subscription.
Amazon SNS sends an email from Canary <no-reply@sns.amazonaws.com>
, reflecting the friendly name
of the topic. Use the link in the email to confirm your address.
Configure an alarm
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
. -
Switch to the Amazon Web Services Region where you created the Lambda function.
-
Choose All alarms.
-
Choose Create alarm.
-
On the Specify metric and conditions page, choose Select metric.
-
In the Metrics search box, enter lambda-canary Errors.
-
Choose Lambda > By Function 1 Name.
-
Select the lambda-canary Errors metric.
-
On the Specify metric and conditions page, in the Statistic drop-down menu, choose Sum.
-
Set the threshold to Greater/Equal than
1
. -
On the Configure actions page, add a notification with the following settings:
-
Alarm state trigger – In alarm
-
Send notification to... –
lambda-canary-notifications
-
-
On the Add name and description page, enter the following:
-
Name –
lambda-canary-alarm
-
Description –
Lambda canary alarm
-
-
Choose Create alarm.
Test the alarm
Update the function configuration to cause the function to return an error, which triggers the alarm.
To trigger an alarm
Open the Functions page
of 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.
Clean up your resources
You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting Amazon resources that you're no longer using, you prevent unnecessary charges to your Amazon account.
To delete the Lambda function
-
Open the Functions page
of the Lambda console. -
Select the function that you created.
-
Choose Actions, Delete.
-
Type
delete
in the text input field and choose Delete.
To delete the CloudWatch alarm
-
Open the All 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 input field. -
Choose Delete.