How to Schedule a Cron to Run on Specific Days Using Serverless?

Create AWS CloudWatch scheduled event to run on specific days using Serverless framework

The cron() expression has the following syntax in Serverless framework:

cron(Minutes Hours Day-of-Month Month Day-of-Week Year)

Please note that all six fields are required.

From the syntax above, you can see that the cron() expression allows you to specify one of the following two options to schedule the cron to run on specific days:

Field Values Wildcards
Day-of-Month 1-31 , - * ? / L W
Day-of-Week 1-7 or SUN-SAT , - * ? L #

The wildcards have the following meaning/uses:

  • , — to specify multiple values (e.g. JAN,FEB,MAR);
  • - — to specify ranges (e.g. 1-15);
  • * — to include all values;
  • ? — to use one or other between Day-of-Month and Day-of-Week fields;
  • / — to specify increments;
  • L — to specify last day of month or week;
  • W — to specify weekday in the Day-of-month field;
  • # — to specify a certain day of the week within a month (e.g. 3#1 which refers to first Tuesday of the month).

Please note that:

  1. You cannot use multiple expressions in the Day-of-Week field when you're using "#" character. You may only define one expression in such a case;
  2. You cannot use the * (asterisk) wildcard for both, "Day-of-Month" and "Day-of-Week" fields in the same cron() expression. If the expression you're using contains * for both fields, then you must replace one of them with ? (question mark).

You can learn more about wildcards and scheduled events in the official AWS CloudWatch Scheduled Events doc.

For example:

# Run at 6:00 pm (UTC) every Monday through Friday
cron(0 18 ? * MON-FRI *)

# Run at 8:00 am (UTC) every 1st day of the month
cron(0 8 1 * ? *)

Hope you found this post useful. It was published . Please show your love and support by sharing this post.