4

I want to trigger an AWS Lambda function, using Cloudwatch Rules and the requirement is as follows.

  • Condition 1: Trigger Daily
  • Condition 2: Every 5 mins
  • Condition 3: It should NOT trigger between 11PM and 1AM every day (Maintenance Window).

I read the documentation on crons and am unable to come up with a Cron expression to fulfill condition 3. I came up with the following expression.

0/5 ## * * ? *

How can I fulfill condition 3 mentioned above? I have left it as ## on the cron expression. I am well aware of the timezones.

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
  • https://stackoverflow.com/questions/8764150/crontab-run-every-15-minutes-except-at-3am this can help you. – Avinash Dalvi Dec 27 '19 at 06:10
  • https://stackoverflow.com/help/someone-answers Help by voting and accepting answer if its works. – Avinash Dalvi Jan 01 '20 at 12:05
  • 1
    Please see the difference between Cloudwatch Cron Expressions and Regular Cron expressions. @aviboy2006. These answers presented here are not applicable to Cloudwatch Rules. This Cron expression has 6 fields.https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html – Keet Sugathadasa Jan 15 '20 at 14:27

4 Answers4

4

You can utilize cron online tools such as

https://crontab.guru/

http://www.cronmaker.com/

Here is the expression that I created as per your requirement

*/5 1-23 * * *

enter image description here

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • This is very good site to help for cron job creator. I did also same. – Avinash Dalvi Dec 27 '19 at 06:22
  • 1
    These cron expressions are not applicable to AWS Cloudwatch. That is why I have shared a reference in the question. Please see documentation on https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html – Keet Sugathadasa Jan 15 '20 at 14:29
3

I'm answering my own question. Regular cron expressions are not applicable for AWS Cloudwatch Rules. AWS Cloudwatch Rules have a cron expression of SIX (6) Required Fields. (Link to Documentation). The answer to this question is as follows. I will present different scenarios, because in some cases, the timezone is important and we might have to skip different hours instead of Midnight.

Both of the below scenarios are working and I tested in the AWS Cloudwatch Rules.

Scenario 1: Trigger Daily - Every 5 Mins - Skip 11PM to 1AM.

0/5 1-22 * * ? *

Explanation: First field mentions, it should be triggered only in 0th and 5th Minutes. Second field mentions that it should be triggered from 1st Hour to 22nd Hour. Hence, after 22:55 the next trigger will be 1:00. It will skip from 23:00 to 00:55.

Scenario 2: Trigger Daily - Every 5 Mins - Skip 5PM to 7PM

0/5 0-16,19-23 * * ? *

Explanation: First field mentions, it should be triggered only in 0th and 5th Minutes. Second field mentions that it should be triggered from 0th Hour to 16th Hour and from the 19th Hour to 23rd Hour. Hence, after 16:55 the next trigger will be 19:00. It will skip from 17:00 to 18:55.

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
1

This command can help you :

*/5 1-23* * * * /usr/bin/php /home/username/public_html/cron.ph >/dev/null 2>&1

Reference : crontab run every 15 minutes between certain hours

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
1

The answer is already given but few thing that I can add, and the https://crontab.guru/ create confusion as AWS cron expression is consist of 6 figures while crontab consist of 5, which just create confusion as mentioned by @keet

So first you can ignore the year section and check your cron express in crontab but again crontab does not understand ? mark when it comes to days of week.

So my suggestion is to use AWS console for expression testing which will list next 10 triggers.

enter image description here

So hope it helps someone else to know about the next trigger.

enter image description here

For more details, you check AWS CronExpressions

Adiii
  • 54,482
  • 7
  • 145
  • 148