How to Schedule AWS Lambda Functions with CloudWatch Events in CDK (Cron Jobs Guide)

Automate cloud tasks without servers using AWS Lambda and scheduled CloudWatch Events. Run cleanups, reports, or backups with simple triggers.

a few seconds ago   •   3 min read

By Hannes Michael
Photo by stefan moertl / Unsplash

Ever wished you could run tasks automatically in the cloud without setting up a server? AWS Lambda makes that possible, and you can schedule functions to run whenever you need. In this post, we will show you how to set up AWS Lambda to run on a schedule using CloudWatch Events. By the end, you will have a working scheduled Lambda function ready to go.

Project Setup

Before we start we need to setup a fresh CDK project. Today we will go with python, but feel free to use any supported programming language. Let's assume you already have CDK installed. If not, HERE is a detailed guide.

The command to initialise a project is following:

cdk init app --language python

After running the command you should have a project structure similar to this:

my_project/
├─ app.py
├─ my_project/
│  ├─ __init__.py
│  └─ my_scheduled_lambda_stack.py

Lambda Setup

First of all create a file called handler.py inside of a freshly created folder named lambda. This should result in following folder structure:

my_project/
├─ app.py
├─ my_project/
│  ├─ __init__.py
│  └─ my_scheduled_lambda_stack.py
└─ lambda/
   └─ handler.py

Inside of the lambda we will keep our setup rather minimal, but feel free to extend your lambda as you wish.

def main(event, context):
    print("Hello from scheduled Lambda!")

Scheduling Setup

Finally we can setup the lambda for hosting in AWS and the event rule to trigger the serverless function every 5 minutes.

from aws_cdk import (
    Stack,
    aws_lambda,
    aws_events,
    aws_events_targets,
    Duration,
)
from constructs import Construct


class ScheduledLambdaStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Lambda function from file
        fn = aws_lambda.Function(
            self, "MyScheduledLambda",
            runtime=aws_lambda.Runtime.PYTHON_3_12,
            handler="handler.main",
            code=aws_lambda.Code.from_asset("lambda"),
        )

        # Run every 5 minutes (example)
        rule = aws_events.Rule(
            self, "ScheduleRule",
            schedule=aws_events.Schedule.rate(Duration.minutes(5)),
        )

        rule.add_target(aws_events_targets.LambdaFunction(fn))

Variants of Scheduling

AWS Event Rules support two different types of scheduling. Rate expressions or regular Cron-style expressions..

Rates are always on fixed points in time after deployment e.g. every hour, 5 minutes or every day. This has the benefit that its easily human-readable, but also has certain limitations like that you cant limit it to certain hours and that the minimum possible timespan is 1 Minute.

# Every 5 Minutes
aws_events.Schedule.rate(Duration.minutes(5))

# Every 1 hour
aws_events.Schedule.rate(Duration.hours(1))

# Every 1 day
aws_events.Schedule.rate(Duration.days(1))

To resolve those limitations you can use the standard Cron syntax. This syntax can be quite complex, but easily learnable. The website i would recommend is crontab.guru. But here are some examples:

# Every day at 6:00 UTC
events.Schedule.cron(minute="0", hour="6")

# Every Monday at 12:30 UTC
events.Schedule.cron(minute="30", hour="12", week_day="MON")

# Every 1st day of the month at midnight
events.Schedule.cron(minute="0", hour="0", day="1")

# Multiple weekdays (Mon, Wed, Fri) at 8:00 UTC
events.Schedule.cron(minute="0", hour="8", week_day="MON,WED,FRI")

# 3 times per day on Mon-Fri
events.Schedule.cron(minute="0", hour="8,12,16", week_day="MON-FRI")

An important note is that you cant combine the day with the week_day flag. While CDK synthesizes this to CloudFormation yaml you cant deploy this.

When to use scheduling?

Now that we have a properly scheduled lambda you might wonder on when to use those. Here is a small and incomplete list for what this could be useful.

  • clean up expired database records or s3 files
  • generate daily / weekly / monthly reports
  • health monitoring of other services
  • nightly ran ETL (Extract, Transform, Load) pipelines
  • backup RDS or DynamoDB tables at off-peak hours
  • post social media updates automatically
  • analytics on game or app usagex

Final words

With your scheduled Lambda set up, you can start automating all sorts of tasks without worrying about servers. From cleaning up old data and generating reports to backing up databases or posting updates automatically, your cloud is now working for you on autopilot. Once you’re comfortable with rate and cron expressions, you can explore more advanced setups, chaining Lambdas or connecting other AWS services to make your automation even smarter, all running when you need it.

Spread the word

Keep reading