Lambda functions can be triggered by other AWS services and you can invoke them directly, but how can you run them periodically?
Scheduled events and rules
You can create scheduled rules with Amazon EventBridge to invoke your Lambda function periodically. You can also use AWS SAM to create these resources for you.
How to define recurrent events
You have two options to schedule events: rate and cron. Rate expressions are easy to use, they define the intervals at which the Lambda function will be invoked, e.g. every 5 minutes. If you need something more fine grained, you can use cron expressions. Cron expressions use six parameters where you can combine values and wildcards to schedule your Lambda functions as needed.
- Value must be a positive number
- Unit must be one of the following values:
- The singular form of the unit must be used for value = 1
- The plural form of the unit must be used for value > 1
|The event is triggered every day, starting from the creation time.|
|The event is triggered every four hours, starting from the creation time.|
|The event is triggered every 15 minutes, starting from the creation time.|
cron(minutes hours day-of-month month day-of-week year)
|minutes||0-59||, – * /|
|hours||0-23||, – * /|
|day-of-month||1-31||, – * / ? L W|
|month||1-12 or JAN-DEC||, – * /|
|day-of-week||1-7 or SUN-SAT||, – * ? L #|
|year||1970-2199||, – * /|
|The comma wildcard is used to specify a list of values, e.g. JAN,FEB,MAR|
|The dash wildcard is used to specify ranges, e.g. 1-15|
|The asterisk wildcard means any value.|
|The forward slash wildcard specifies a starting value and increments, e.g. 2/10 which translates to 2,12,22…|
|The question mark is used as a placeholder when the other day field is used.|
|The L wildcard means that rather than using every occurrence only the last occurrence is used.|
|The W wildcard is used to determine the closest weekday within the month. If you use 1W it will be triggered on the 1st of each month if it is a weekday, otherwise on the next Monday which has to be on the 2nd or 3rd.|
|The hash wildcard specifies a certain instance of the day of the week within a month.|
|7:30 AM every day|
|3:10 PM Tuesday through Friday|
|Every 15 min on weekdays|
|5:00 PM on the first Friday of each month|
|09:30 AM every 15th of each month. If the 15th falls on a Saturday, the trigger fires on Friday the 14th. If the 15th is a Sunday, it will be triggered on Monday the 16th.|
|10:00 PM on the last day of each month.|
- You can specify a value or the * wildcard for day only in one of the day fields – day-of-month or day-of-week. You must use the question mark for the other field.
- You can use only one day of the week if you use the hash wildcard.
AWS SAM and CloudFormation
If you aren’t aware of AWS SAM yet, be sure to check it out. It is an amazing tool to define resources for both production and development environments. If you are already using AWS SAM, then you simply need to modify or add the Events section as follows:
Resources: YourFunction: Type: AWS::Serverless::Function Properties: CodeUri: your_function/ Handler: app.handler Events: ScheduledEvent: Type: Schedule Properties: Schedule: cron(00 22 L * ? *) Description: Your Function description. Enabled: True
- Open the Amazon EventBridge console and click on Rules in the navigation tab.
- Click on Create Rule.
- Enter the rule name and description and select Schedule as rule type.
- Define the schedule using the rate expression parameters described above, and check that the next 10 trigger dates match your expectations.
- Define the target by selecting Lambda function as target type and then the name of your Lambda function. Please note that it might take a couple of seconds to load.
- Optional: define tags for this rule
- Review all settings are correct and click on Create rule
- AWS Serverless Application Model – Amazon Web Services. (n.d.). Amazon Web Services, Inc. Retrieved April 27, 2022, from https://aws.amazon.com/serverless/sam/
- Creating an Amazon EventBridge rule that runs on a schedule – Amazon EventBridge. (n.d.). Amazon EventBridge User Guide. Retrieved April 29, 2022, from https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html