thehme
thehme

Reputation: 2886

AWS cron expression to run every other Monday

I want to schedule a CloudWatch event to run every other Monday and have started with this command:

0 14 ? * 2 *

Currently with the above command, I get a weekly schedule of Monday executions:

Mon, 27 Jul 2020 14:00:00 GMT
Mon, 03 Aug 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 17 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 31 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 14 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT
Mon, 28 Sep 2020 14:00:00 GMT

However, I would like the schedule to be set to every other Monday, e.g.

Mon, 27 Jul 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT

I have seen examples with exp and # being used, but I don't think AWS CloudWatch events accept these sort of parameters.

Upvotes: 8

Views: 12007

Answers (4)

JonasV
JonasV

Reputation: 1031

You can do this with a rate scheduler and setting the start date.

In AWS SAM it would look like this for example:

  Events:
    CWSchedule:
      Type: ScheduleV2
      Properties:
        ScheduleExpression: "rate(14 days)"
        FlexibleTimeWindow:
          Mode: FLEXIBLE
          MaximumWindowInMinutes: 5
        StartDate: "2024-04-01T12:00:00.000Z"
        ScheduleExpressionTimezone: UTC

See here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedulev2.html

And here: https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-StartDate

Upvotes: 1

Yee Lee
Yee Lee

Reputation: 1

Adding on to @Chris answers in the bullet points,

one can add in additional rule to accommodate for months that have 5 Mondays

  • 0 14 ? * 2#5 * - Run on the FIFTH Monday of the month.

Upvotes: 0

Chris Williams
Chris Williams

Reputation: 35188

You won't be able to do any of the fancier commands (especially those using variables from the command line).

You could do this very basically but would require 2 separate events in order to carry it out:

  • 0 14 ? * 2#1 * - Run on the first Monday of the month.
  • 0 14 ? * 2#3 * - Run on the third Monday of the month.

Unfortunately there is no compatible syntax for scheduled expressions that would allow the concept of every other week, so the above commands occasionally could lead to a 3 week gap.

If you don't care about the Monday you could of course use 0 14 1,15 * * to run on the 1st and 15th of each month (roughly every 2 weeks).

The final option would be to run every Monday, but have the script exit if it is not the every other week, the expression would then just be 0 14 ? * 2 *.

More information about the syntax is available on the Cron Expressions section of the Scheduled Events page.

Upvotes: 8

Dennis Traub
Dennis Traub

Reputation: 51634

Chris' answer is correct. Currently, there is no way that I could think of to express this as part of CloudWatch Scheduled Events.

However, a workaround could be to set it to every Monday (0 14 ? * 2 *) and trigger a Lambda function that checks whether it's in the on-week or the off-week before triggering the actual target.

Even though this adds some complexity, it would be a viable solution.

Upvotes: 8

Related Questions