Menu
Amazon CloudWatch Events
User Guide

Tutorial: Schedule AWS Lambda Functions Using CloudWatch Events

You can set up a rule to run an AWS Lambda function on a schedule. This tutorial shows how to use the AWS Management Console or the AWS CLI to create the rule. If you would like to use the AWS CLI but have not installed it, see the AWS Command Line Interface User Guide.

CloudWatch Events does not provide second-level precision in schedule expressions. The finest resolution using a cron expression is a minute. Due to the distributed nature of the CloudWatch Events and the target services, the delay between the time the scheduled rule is triggered and the time the target service honors the execution of the target resource might be several seconds. Your scheduled rule is triggered within that minute but not on the precise 0th second.

Step 1: Create an AWS Lambda Function

Create a Lambda function to log the scheduled events. You specify this function when you create your rule.

To create a Lambda function

  1. Open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. If you are new to Lambda, you see a welcome page; choose Get Started Now; otherwise, choose Create a Lambda function.

  3. On the Select blueprint page, type hello for the filter, and then choose the hello-world blueprint.

  4. On the Configure triggers page, choose Next.

  5. On the Configure function page, do the following:

    1. Type a name and description for the Lambda function. (For example, name the function "LogScheduledEvent".)

    2. Edit the sample code for the Lambda function. For example:

      Copy
      'use strict'; exports.handler = (event, context, callback) => { console.log('LogScheduledEvent'); console.log('Received event:', JSON.stringify(event, null, 2)); callback(null, 'Finished'); };
    3. For Role, choose Choose an existing role and then choose your basic execution role from Existing role. Otherwise, create a new basic execution role.

    4. Choose Next.

  6. On the Review page, choose Create function.

Step 2: Create a Rule

Create a rule to run your Lambda function on a schedule.

To create a rule using the console

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. In the navigation pane, choose Events, Create rule.

  3. For Event Source, do the following:

    1. Choose Schedule.

    2. Choose Fixed rate of and specify the schedule interval (for example, 5 minutes).

  4. For Targets, choose Add target and then choose Lambda function.

  5. For Function, select the Lambda function that you created.

  6. Choose Configure details.

  7. For Rule definition, type a name and description for the rule.

  8. Choose Create rule.

If you prefer, you can create the rule using the AWS CLI. First, you must grant the rule permission to invoke your Lambda function. Then you can create the rule and add the Lambda function as a target.

To create a rule using the AWS CLI

  1. Use the following put-rule command to create a rule that triggers itself on a schedule:

    Copy
    aws events put-rule \ --name my-scheduled-rule \ --schedule-expression 'rate(5 minutes)'

    When this rule triggers, it generates an event that serves as input to the targets of this rule. The following is an example event:

    Copy
    { "version": "0", "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa", "detail-type": "Scheduled Event", "source": "aws.events", "account": "123456789012", "time": "2015-10-08T16:53:06Z", "region": "us-east-1", "resources": [ "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule" ], "detail": {} }
  2. Use the following add-permission command to trust the CloudWatch Events service principal (events.amazonaws.com) and scope permissions to the rule with the specified Amazon Resource Name (ARN):

    Copy
    aws lambda add-permission \ --function-name LogScheduledEvent \ --statement-id my-scheduled-event \ --action 'lambda:InvokeFunction' \ --principal events.amazonaws.com \ --source-arn arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule
  3. Use the following put-targets command to add the Lambda function that you created to this rule so that it runs every five minutes:

    Copy
    aws events put-targets --rule my-scheduled-rule --targets file://targets.json

    Create the file targets.json with the following contents:

    Copy
    [ { "Id": "1", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:LogScheduledEvent" } ]

Step 3: Test the Rule

You can verify that your Lambda function was invoked.

To test your rule

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. In the navigation pane, choose Events, Rules, select the name of the rule that you created, and choose Show metrics for the rule.

  3. To view the output from your Lambda function, do the following:

    1. In the navigation pane, choose Logs.

    2. Select the name of the log group for your Lambda function (/aws/lambda/function-name).

    3. Select the name of log stream to view the data provided by the function for the instance that you launched.

  4. (Optional) When you are finished, you can disable the rule.

    1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

    2. In the navigation pane, choose Events, Rules.

    3. Select the rule and then choose Actions, Disable.

    4. When prompted for confirmation, choose Disable.