Over a million developers have joined DZone.

AWS Lambda: Programmatically Scheduling a CloudWatch Event

DZone's Guide to

AWS Lambda: Programmatically Scheduling a CloudWatch Event

AWS Lambda is a solid serverless option, but setting up automatically scheduled events might not be intuitive. Let's see how CloudWatch can solve the problem.

· Cloud Zone ·
Free Resource

Get the Metrics Collection and Monitoring Essentials tutorial collection. A 4-part tutorial series from DigitalOcean.

I recently wrote a blog post showing how to create a Python ‘Hello World’ AWS Lambda function and manually invoke it, but what I really wanted to do was have it run automatically every hour.

To achieve that in AWS Lambda land, we need to create a CloudWatch Event. The documentation describes them as follows:

Using simple rules that you can quickly set up, you can match events and route them to one or more target functions or streams.

Image title

This is actually really easy from the Amazon web console, as you just need to click the ‘Triggers’ tab and then ‘Add trigger’. It’s not obvious that there are actually three steps are involved as they’re abstracted from you.

So what are the steps?

  1. Create rule
  2. Give permission for that rule to execute
  3. Map the rule to the function

I forgot to do step 2 initially, and then you just end up with a rule that never triggers, which isn’t particularly useful.

The following code creates a ‘Hello World’ Lambda function and runs it once an hour:

import boto3

lambda_client = boto3.client('lambda')
events_client = boto3.client('events')

fn_name = "HelloWorld"
fn_role = 'arn:aws:iam::[your-aws-id]:role/lambda_basic_execution'

fn_response = lambda_client.create_function(
    Code={'ZipFile': open("{0}.zip".format(fn_name), 'rb').read(), },

fn_arn = fn_response['FunctionArn']
frequency = "rate(1 hour)"
name = "{0}-Trigger".format(fn_name)

rule_response = events_client.put_rule(


            'Id': "1",
            'Arn': fn_arn,

We can now check if our trigger has been configured correctly:

$ aws events list-rules --query "Rules[?Name=='HelloWorld-Trigger']"
        "State": "ENABLED", 
        "ScheduleExpression": "rate(1 hour)", 
        "Name": "HelloWorld-Trigger", 
        "Arn": "arn:aws:events:us-east-1:[your-aws-id]:rule/HelloWorld-Trigger"

$ aws events list-targets-by-rule --rule HelloWorld-Trigger
    "Targets": [
            "Id": "1", 
            "Arn": "arn:aws:lambda:us-east-1:[your-aws-id]:function:HelloWorld"

$ aws lambda get-policy --function-name HelloWorld
    "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"default\",\"Statement\":[{\"Sid\":\"HelloWorld-Trigger-Event\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:[your-aws-id]:function:HelloWorld\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:events:us-east-1:[your-aws-id]:rule/HelloWorld-Trigger\"}}}]}"

All looks good, so we’re done! 

Be sociable and share!

Getting Started with Kubernetes: A Webinar Series brought to you by DigitalOcean

cloud ,aws lambda ,cloudwatch ,scheduling ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}