Amazon CloudWatch Events
User Guide

Scenario 6: Run an AWS Lambda Function on a Schedule Using the AWS CLI

You can set up a rule to run an AWS Lambda function on a schedule. This scenario uses the AWS CLI to create the rule.


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 will be triggered within that minute but not on the precise 0th second.

Step 1: Create an AWS Lambda function

To create an AWS Lambda function

  1. Open the AWS Lambda console at

  2. Choose Create a Lambda function, and then on the Select blueprint screen, choose hello-world.

  3. On the Configure function screen, in the Name field, enter SomethingHappened.

  4. In the Lambda function code section, edit the sample code to match the following example:

    console.log('Loading function');
    exports.handler = function(event, context) {
        console.log('Here is the event:', JSON.stringify(event, null, 2));
  5. Under Lambda function handler and role, in the Role field, if you have a lambda_basic_execution_rule, select it. Otherwise, create a new basic execution role.

  6. Choose Next, and then on the Review screen, choose Edit to make any changes. If you're satisfied with the function, choose Create function.

Step 2: Create an Amazon CloudWatch Events Rule

To create a CloudWatch Events rule

  1. At a command prompt, type the following to trust

    aws lambda add-permission \
    --function-name SomethingHappened \
    --statement-id MyId \
    --action 'lambda:InvokeFunction' \
    --principal \
    --source-arn arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule


    If you're using a specific Lambda alias or version, you must add the --qualifier parameter in the aws lambda add-permission command.

    aws lambda add-permission \
    --function-name SomethingHappened \
    --statement-id MyId \
    --action 'lambda:InvokeFunction' \
    --principal \
    --source-arn arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule
    --qualifier alias or version
  2. At a command prompt, type the following to create a rule that self-triggers on a schedule:

    aws events put-rule \
    --name MyScheduledRule \
    --schedule-expression 'rate(5 minutes)'

    When the above rule self-triggers it will generate an event (that looks like the following), which can be used as an input to the targets of this rule:

        "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
        "detail-type": "Scheduled Event",
        "source": "",
        "account": "123456789012",
        "time": "2015-10-08T16:53:06Z",
        "region": "us-east-1",
        "resources": ["arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule"],
        "detail": {}
  3. Run the following command to add the AWS Lambda function you created in Step 1 to this rule so that it is run every 5 minutes:

    aws events put-targets \
    --rule MyScheduledRule \
    --targets '{"Id" : "1", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:SomethingHappened"}'

Step 3: Verify Your Amazon CloudWatch Events Rule

You can verify your rule by checking your AWS Lambda metrics in the Amazon CloudWatch console.

To verify your CloudWatch Events rule using the console

  1. To view your AWS Lambda metrics, open the CloudWatch console

  2. In the navigation pane, under Metrics, choose Lambda to view the metrics generated by your Lambda function.

  3. To view the output from your function, in the navigation pane, choose Logs, and then in the Log Groups list, select the /aws/lambda log group that contains the data.

  4. Under Log Streams, select a log stream to view the data about the instance that you launched.