AWS EventBridge as a Crontab for Lambda

May 1, 2022
aws eventbridgecronjobcron expressions

AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. AWS EventBridge is a serverless event bus service that makes it easy to connect applications together using data from your own applications, integrated Software-as-a-Service (SaaS) applications, and AWS services. This post explains how to configure a cron job for an AWS Lambda function using AWS EventBridge.


Configuring a cron job with AWS EventBridge

  1. Open the AWS Management Console and navigate to the EventBridge service.
  2. Click the "Create rule" button.
  3. Give your rule a name, and choose "Schedule" as the rule type.
  4. Select "Recurring schedule" to create a repeating event.
  5. Choose "Cron expression" as the schedule type, and enter the cron expression for when you want the Lambda function to be executed.
  6. Under "Targets," choose "Lambda function" as the target type.
  7. Choose your Lambda function from the drop-down list of available Lambda functions.
  8. Click the "Create" button to create the rule.


Cron expressions are used in cron job scheduling to define the timing and frequency of recurring tasks. Eventbridge uses the same format as Linux and the timezone is tied to the region the task is setup in. The AWS us-east-1 region for example, uses the Eastern time zone. The cron expression consists several fields, representing different components of the schedule (labeled underneath). Each field has a specific format that determines when the task should run. Here's a breakdown of the format:

  1. Minute (0-59): Specifies the minutes within an hour when the task should run.
  2. Hour (0-23): Specifies the hour of the day when the task should run.
  3. Day of the month (1-31): Specifies the day of the month when the task should run.
  4. Month (1-12 or JAN-DEC): Specifies the month(s) when the task should run.
  5. Day of the week (0-6 or SUN-SAT): Specifies the day(s) of the week when the task should run.
  6. Year: Specifies the year(s) when the task should run. This field is not supported in all cron implementations.

Each field can accept a single value, a comma-separated list of values, a range of values, or a combination of these.

Here are some examples to illustrate the syntax:

  • Run a task every minute: * * * * *
  • Run a task at 12:00 PM every day: 0 12 * * *
  • Run a task at 9:30 AM on the 15th of every month: 30 9 15 * *
  • Run a task every Monday at 8:00 AM: 0 8 * * 1
  • Run a task every 10 minutes: */10 * * * *

In addition to these basic expressions, you can also use special characters to define more complex schedules:

  • Asterisk (*) represents any value and is used as a wildcard.
  • Comma (,) separates multiple values within a field.
  • Hyphen (-) specifies a range of values.
  • Forward slash (/) is used to specify increments. For example, */10 in the minute field means every 10 minutes

Testing the cron job

Once the cron job has been created, you can test it to ensure that it is working correctly. Here's how to test the cron job:

  1. Navigate back to the EventBridge service, and click on your newly created rule.
  2. Click the "Test" button.
  3. Enter a test event that matches the format of your Lambda function's input.
  4. Click the "Test" button again to run the test.

Conclusion

Configuring a cron job for an AWS Lambda function using AWS EventBridge is a simple and efficient way to schedule the execution of your Lambda function, also cheap. With AWS EventBridge, you can easily create rules that specify when your Lambda function should be executed, and the service takes care of the rest. By following the steps outlined in this blog post, you can configure your own cron job for your Lambda function in just a few minutes.