Tutorial: Using Amazon Lambda with scheduled events - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

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

  1. Sign in to the Amazon Web Services Management Console and open the Amazon Lambda console at https://console.amazonaws.cn/lambda/.

  2. Choose Create function.

  3. Choose Use a blueprint.

  4. Open the Select blueprint dropdown list and choose the Schedule a periodic check of any URL blueprint.

  5. Configure the following settings.

    • Function namelambda-canary.

    • Execution roleCreate a new role from Amazon policy templates.

    • Role namelambda-eventbridge-role.

    • Policy templatesSimple microservice permissions.

    • RuleCreate a new rule.

    • Rule nameCheckWebsiteScheduledEvent.

    • Rule descriptionCheckWebsiteScheduledEvent trigger.

    • Rule typeSchedule expression.

    • Schedule expressionrate(1 minute).

    • Environment variables

      • sitehttps://docs.amazonaws.cn/lambda/latest/dg/welcome.html

      • expectedWhat is Amazon Lambda?

  6. 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.

  1. Open the Functions page of the Lambda console.

  2. Choose the lambda-canary function.

  3. Choose Test.

  4. Create a new event using the CloudWatch event template (cloudwatch-scheduled-event).

  5. Choose Create event.

  6. 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
  1. Open the Amazon SNS console.

  2. Switch to the Amazon Web Services Region where you created the Lambda function.

  3. Choose Topics, and then choose Create topic.

  4. Create a topic with the following settings.

    • TypeStandard.

    • Namelambda-canary-notifications.

    • Display nameCanary.

  5. Choose Create topic.

  6. On the lambda-canary-notifications topic page, choose Create subscription.

  7. Create a subscription with the following settings.

    • ProtocolEmail.

    • Endpoint – Your email address.

  8. 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
  1. Open the CloudWatch console.

  2. Switch to the Amazon Web Services Region where you created the Lambda function.

  3. Choose All alarms.

  4. Choose Create alarm.

  5. On the Specify metric and conditions page, choose Select metric.

  6. In the Metrics search box, enter lambda-canary Errors.

  7. Choose Lambda > By Function 1 Name.

  8. Select the lambda-canary Errors metric.

  9. On the Specify metric and conditions page, in the Statistic drop-down menu, choose Sum.

  10. Set the threshold to Greater/Equal than 1.

  11. On the Configure actions page, add a notification with the following settings:

    • Alarm state trigger – In alarm

    • Send notification to...lambda-canary-notifications

  12. On the Add name and description page, enter the following:

    • Namelambda-canary-alarm

    • DescriptionLambda canary alarm

  13. 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
  1. Open the Functions page of the Lambda console.

  2. Choose the lambda-canary function.

  3. Scroll down. Under Environment variables, choose Edit.

  4. Set expected to 404.

  5. 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
  1. Open the Functions page of the Lambda console.

  2. Select the function that you created.

  3. Choose Actions, Delete.

  4. Type delete in the text input field and choose Delete.

To delete the CloudWatch alarm
  1. Open the All alarms page of the CloudWatch console.

  2. Select the alarm you created.

  3. Choose Actions, Delete.

  4. Choose Delete.

To delete the Amazon SNS subscription
  1. Open the Subscriptions page of the Amazon SNS console.

  2. Select the subscription you created.

  3. Choose Delete, Delete.

To delete the Amazon SNS topic
  1. Open the Topics page of the Amazon SNS console.

  2. Select the topic you created.

  3. Choose Delete.

  4. Enter delete me in the text input field.

  5. Choose Delete.