Invalid CRON expression 0 */8 * * * - amazon-web-services

I am trying to use AWS Cloudwatch service. Inside that I am create a Synthetic Canary.
And for the schedule I am inserting a cron job with value 0 */8 * * * .
Basically I want to execute it every 8 Hours, Every Day.
But the AWS Cron checker tells that it is wrong expression. I've checked various links but everywhere same expression is there.
I am not sure what is wrong with above expression.

If this needs to be executed every 8 hours, then you can Schedule expressions using the rate as provided here.
For CRON expression will be:
0 0/8 * * ? *
You can read here more.

Related

AWS cron jobs to run every minute across a span of time across two hours?

I have a Lambda function on AWS scheduled using the following cron expression:
* 19-20 ? * SAT *
So it runs every minute from 19:00 - 20:59 on Saturdays. In reality, though, it really just needs to run from 19:50 - 20:30 on Saturdays, and my current setup is costing me money for no good reason. Is there any way to specify that with a cron expression (or is there another AWS scheduling mechanism I could use to accomplish this)?
I think you have to split it to two expressions:
From 19:50-19:59:
50/1 19 ? * SAT *
From 20:00-20:30:
0-30/1 20 ? * SAT *

How can I make sure my lambda runs at the start of each minute

I have a Cloud Watch Rule scheduling a lambda with a cron expression 0/1 * * * ? *.
The next 10 trigger dates show that it will run on the first second of every minute which is exactly what I want.
However when I monitor the lambda Recent Invocations (or look in the CouldWatch logs) I see it is always run somewhere around the 37th second of each minute. I am able to adjust this by enabling and disabling my CouldWatch rule again and again until I randomly get a start time closer to start of each minute.
But there must be a better solution than this?

AWS cloudwatch cron event

I want to setup a cloudwatch event which will get triggered in every X minutes, but should not be triggered at 0th minute. ie it should be triggered at current time+X minutes, 2X minutes, 3X minutes etc. How can i do that.
Update: i had done setting up cloudwatch event, my only problem is crone expression. I want to get a cron expression which can schedule the event which start from 23rd minutes from the current time and at every 23rd minutes thereafter.
0/23 * * * ? * doesnt work because it gets triggered at 0th minute
23/23 * * * ? * doesnt work because 1st event may not be 23rd minutes apart from current time
Jay,
You'll need to setup cloudwatch event for this. Here's the link to it. You can further look at the cron expression using this link. And, you can build the cron expression online using this link.
The above mention links should fulfill your requirements.
Let me know if you need any further help. Thanks!
UPDATE:
As per your comments, following cron expression should fulfill your requirements. You can check by pasting this expression here.
0 23/23 * * * ? *
I think that you just want the events to be triggered at every :23 and :46 minutes.
Here should be the correct cron expression:
23,46 * * * ? *

Validate that a CloudWatch Events ScheduleExpression value is valid?

I have an internal API, backed by CloudWatch Events, that lets developers schedule cronjobs. The user provided schedule expression must be a valid value for CloudWatch Events. Is there a utility/library for validating the rate and cron schedule expression values before making the API call to AWS to create the rule?
https://ap-southeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-southeast-1#rules:action=create
There is a undocumented API when using the above, you provide a cron expressiaon and it will validate the expression and return the next 10 dates that this cron will run.
The console does a POST to here
https://ap-southeast-1.console.aws.amazon.com/cloudwatch/CloudWatch/data/jetstream.TestScheduleExpression/20191203005825803-2287194097589902
With this payload:
{"Expression":"cron(0 19 ? * MON-FRI *)","Limit":10}
!CW-Client-Metrics!
{"clientMetrics":{"cwdbSetWizardRuleScheduleExpressionAct":1,"cwdbSaveCronAndGetTriggerDatesAct":1}}
Returns the next 10 runtimes as epoch in the NextTriggerDates response:
I am afraid I have not seen any good regex to handle the AWS style so far.
You can use following regex to validate it:
// Minutes
/^([*]|([0-5]?\d)|((([0-5]?\d)|(\*))\/([0-5]?\d))|(([0-5]?\d)-([0-5]?\d))|((([0-5]?\d)|(\*))(,(([0-5]?\d)|(\*)))*))$/
// Hours
/^([*]|[01]?\d|2[0-3]|((([01]?\d|2[0-3]?)|(\*))\/([01]?\d|2[0-3]?))|(([01]?\d|2[0-3]?)-([01]?\d|2[0-3]?))|((([01]?\d|2[0-3]?)|(\*))((,)(([01]?\d|2[0-3]?)|(\*))){0,23}))$/
// Day of months
/^([*]|[?]|(([1-9]|[12]\d|3[01])[LW]?)|(([1-9]|[12]\d|3[01])-([1-9]|[12]\d|3[01]))|((([1-9]|[12]\d|3[01])|(\*))(\/)([1-9]|[12]\d|3[01]))|((([1-9]|[12]\d|3[01])|(\*))((,)(([1-9]|[12]\d|3[01])|(\*)))*))$/
// Months
/^([*]|([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|((([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|(\*))\/(([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|((([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))-(([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|((([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|(\*))((,)(([2-9]|1[0-2]?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|(\*)))*))$/
// Day of Week
/^([*]|[?]|([1-7]L?)|(SUN|MON|TUE|WED|THU|FRI|SAT)|((([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT))(-|,|#)(([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT)))|((([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT)|(\*))\/(([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT)))|((([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT)|(\*))((,)(([1-7])|(SUN|MON|TUE|WED|THU|FRI|SAT)|(\*)))*))$/
// Year
/^([*]|([1-2]\d{3})|(((([1-2]\d{3})|(\*)))\/((\d{0,4})))|(([1-2]\d{3})-([1-2]\d{0,3}))|((([1-2]\d{3})|(\*))((,)(([1-2]\d{3})|(\*)))*))$/
note: years regex don't check range (1970 - 2199) but it can be easily done with code.
I'm not sure if it helps, but I created a AWS cron expression validator in Python and published it on PyPI here https://pypi.org/project/aws-cron-expression-validator/
pip install aws-cron-expression-validator
Usage;
from aws_cron_expression_validator.validator import AWSCronExpressionValidator
my_expression = "0 18 ? * MON-FRIbad *"
try:
AWSCronExpressionValidator.validate(my_expression)
except ValueError as e:
print(f"Oh no! My expression was invalid: {e}")
# Returns; Oh no! My expression was invalid: Invalid day-of-week value 'MON-FRIbad'.

Run Amazon CloudWatch Event every Sunday at 10:00 am UTC

I have already read Amazon CloudWatch Events - Schedule Expressions for Rules.
Now I want to trigger an event every Sunday at 10:00 am and I am confused if this is correct?
Currently I have cron(0 10 * * SUN *) & cron(0 10 ? * SUN *)
I don't know the difference between ? & * in Cronjob. So which one is correct is confusing?
Side Note
What is the difference between rate and schedule because rate is simpler to use so I can do like rate(7 days) but I don't know when it will run?
Will it run every 7 days from the moment of deployment?
That page says:
You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value (or a *) in one of the fields, you must use a ? (question mark) in the other.
It also says:
A rate expression starts when you create the scheduled event rule, and then runs on its defined schedule.
To run every Sunday at 10:00 AM UTC,
cron(0 10 ? * SUN *) or cron(0 10 ? * 1 *)
Use ? for Day-of-month part when Day-of-week has a value(here, SUN or 1) because when you want to run only on a specific day of the week, you can't use *, you can't say run everyday. Instead, we should say run one or another day of the month, using ?, which will be every Sunday of the month because we have set Day-of-week.
Refer Schedule Expressions for all details