AWS Lambda
Developer Guide

Tutorial: Using AWS Lambda with Scheduled Events

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.

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 Getting Started to create your first Lambda function.

Create a Lambda Function

  1. Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. Choose Create function.

  3. Choose Blueprints.

  4. Enter canary in the search bar. Choose the lambda-canary blueprint and then choose Configure.

  5. Configure the following settings.

    • Namelambda-canary.

    • RoleCreate a new role from one or more templates.

    • Role namelambda-apigateway-role.

    • Policy templatesSimple microservice permissions.

    • RuleCreate a new rule.

    • Rule nameCheckWebsiteScheduledEvent.

    • Rule descriptionCheckWebsiteScheduledEvent trigger.

    • Schedule expressionrate(1 minute).

    • Enabled – True (checked).

    • Environment variables

      • sitehttps://docs.aws.amazon.com/lambda/latest/dg/welcome.html

      • expectedWhat Is AWS Lambda?

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

Test the function with a sample event provided by the Lambda console.

  1. Open the Lambda console Functions page.

  2. Choose lambda-canary.

  3. Next to the Test button at the top of the page, choose Configure test events from the drop-down menu.

  4. Create a new event using the CloudWatch Events event template.

  5. Choose Create.

  6. Choose Test.

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 topic to receive notifications when the canary function returns an error.

To create a topic

  1. Open the Amazon SNS console.

  2. Choose Create topic.

  3. Create a topic with the following settings.

    • Namelambda-canary-notifications.

    • Display nameCanary.

  4. Choose Create subscription.

  5. Create a subscription with the following settings.

    • ProtocolEmail.

    • Endpoint – Your email address.

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. Choose Alarms.

  3. Choose Create alarm.

  4. Choose Alarms.

  5. Create an with the following settings.

    • Metricslambda-canary Errors.

      Search for lambda canary errors to find the metric.

    • Statistic – Sum.

      Choose the statistic from the drop down above the preview graph.

    • Namelambda-canary-alarm.

    • DescriptionLambda canary alarm.

    • Threshold – Whenever Errors is >=1.

    • Send notification tolambda-canary-notifications

Test the Alarm

Update the function configuration to cause the function to return an error, triggering the alarm.

To trigger an alarm

  1. Open the Lambda console Functions page.

  2. Choose lambda-canary.

  3. Under Environment variables, set expected to 404.

  4. Choose Save

Wait a minute, and then check your email for a message from Amazon SNS