AWS Made Easy
Search
Close this search box.

Tip #21: How to schedule a Lambda function to run periodically

AWS tools and steps to help you schedule your Lambda function

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.

Rate expressions

Syntax

rate(value unit)

Restrictions

  • Value must be a positive number
  • Unit must be one of the following values:
    • minute
    • hour
    • day
  • The singular form of the unit must be used for value = 1
  • The plural form of the unit must be used for value > 1

Examples

ExpressionMeaning
rate(1 day)The event is triggered every day, starting from the creation time.
rate(4 hours)The event is triggered every four hours, starting from the creation time.
rate(15 minutes)The event is triggered every 15 minutes, starting from the creation time.

Cron expressions

Syntax

cron(minutes hours day-of-month month day-of-week year)

Possible values

FieldValuesWildcards
minutes0-59, – * /
hours0-23, – * /
day-of-month1-31, – * / ? L W
month1-12 or JAN-DEC, – * /
day-of-week1-7 or SUN-SAT, – * ? L #
year1970-2199, – * /

Wildcards

WildcardMeaning
,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.
LThe L wildcard means that rather than using every occurrence only the last occurrence is used.
WThe 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.

Examples

ExpressionMeaning
cron(30 7 * * ? *)7:30 AM every day
cron(10 15 ? * TUE-THU *)3:10 PM Tuesday through Friday
cron(0/15 * ? * MON-FRI *)Every 15 min on weekdays
cron(0 17 ? * 6#1 *)5:00 PM on the first Friday of each month
cron(30 9 15W ? *)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.
cron(00 22 L * ? *)10:00 PM on the last day of each month.

Restrictions

  • 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. 

Instructions

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

Amazon EventBridge

  • 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

References

AWS Made Easy

Email
Twitter
Facebook
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Tips & Tricks