89

I'm trying to setup a Cloudwatch Scheduled Event and my cron expression seems to be invalid, though I can't figure out why.

My cron expression is:

cron(5,15,25,35,45,55 * * * *)

I want it to run on the 5th, 15th, 25th, 35th, 45th and 55th minute of every hour of every day. This seems to coincide with the AWS Scheduled Events documentation here http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html.

The above documentation allows for minutes to be represented with comma separated values between 0 and 59, and hours, day-of-month (or day-of-week), month and year to be reflected with a * wildcard to reflect ALL.

I have tried setting the cron expression on the Lambda console (when creating the function and choosing Cloudwatch Schedule Event), and in the Cloudwatch console (along with choosing the target of the trigger). Neither worked with my custom cron expression.

I have tried the following:

5,15,25,35,45,55 * * * *
5,15,25,35,45,55 * ? * *
cron(5,15,25,35,45,55 * * * *)
cron(5,15,25,35,45,55 * ? * *)

Everytime I get an error saying the ScheduleExpression is not valid. I can, however, use one of the premade rate() expressions.

How can I use my own custom cron expression?

Thanks.

Brooks
  • 7,099
  • 6
  • 51
  • 82

3 Answers3

165

Could you try : cron(5,15,25,35,45,55 * * * ? *)

Cron expressions have six required fields here.

AWS documentation


EDIT: Also, don't miss this important wildcard note...

You cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other.

Timothy Aaron
  • 3,059
  • 19
  • 22
Manish Joshi
  • 3,550
  • 2
  • 21
  • 29
  • 5
    Exactly what ended up working. Never having worked with cron, I was misled by the AWS documentation. This helped me out: http://2ndwatch.com/blog/aws-lambda-scheduled-event-function-deep-dive-part-1/ – Brooks Sep 15 '16 at 11:32
  • 24
    To be clear, what was killing me was that you need to use ? for Day of Month or Day of Week, not *.... which is crazy because the AutoScalingGroups have scheduled actions that take CRON expressions and are fine with the *. – Brett Green Feb 19 '20 at 18:31
  • 4
    @BrettGreen The "?" tripped me up as well - I was adding a scheduled task to Fargate. It'd be nice if the online editor picked this up as a validation step. – Michael Berry Mar 02 '20 at 15:18
  • 9
    "Cron expressions have six required fields here" - Not only that, but they'll _freak out_ if you use `*` for the day-of-week (fifth) parameter. You have to use `?` there. – aroth Mar 03 '20 at 04:35
  • 1
    Don't know if it was updated but I was getting the error also because now you don't need the "cron()" – David Gomes May 03 '20 at 03:34
  • I had the error `Parameter ScheduleExpression is not valid. (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; ...)`. Adding the AWS-required and non-standard "year" field and dealing with the day-of-month and day-of-week difference fixed the problem for me. – calvinf Sep 29 '20 at 23:46
  • Amazon updated the AWS CloudWatch Console so when you enter a *valid* Cron expression, it will automatically list the next 10 trigger dates directly below the input field. So if you don't see the text "Next 10 Trigger Date(s)" with the dates; then it means your expression is wrong and you have to fix it. Sadly, there's still no explicit validation error so I can imagine those who aren't Linux-savvy (like me) will run into this error. – Martin Devillers Mar 06 '21 at 03:10
  • @MartinDevillers yes that is a great "your expression is valid and here is how it will be interpreted." Unfortunately if the expression is invalid you get the late and unhelpful "Parameter Schedule Epression is not valid." Plus the AWS CloudWatch cron requirement for day of week vs day of month constraint which isn't called out in the documentation as "THIS IS DIFFERENT FROM THE EXPRESSIONS USED BY CRON." – Jason Harrison Jul 13 '21 at 19:41
  • 1
    Mentioned here but to clarify even further the AWS documentation note "You cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other." means that cron(0/7 01-07 * * MON-FRI *) is invalid expression and should be cron(0/7 01-07 ? * MON-FRI *) – Ricardo stands with Ukraine Apr 24 '22 at 18:37
5

I think this would fit your use-case as well, and offers better legibility: rate(5 minutes)

Or to put it in CloudFormation code:

Resources:
  ChangeDetectFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: example/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        ScheduledEvent:
          Name: Every5min
          Type: Schedule
          Properties:
            Schedule: rate(5 minutes)
Herman
  • 750
  • 1
  • 10
  • 23
-1

It can be related to AWS CRON not containing any seconds

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Hagay r
  • 15
  • 5