Search code examples
amazon-web-servicescronterraformamazon-cloudwatch

How do I write a cron or rate expression as a CloudWatch event rule that runs at specific hours in a day?


I want a to write an AWS cloudwatch event rule that fires at 1:00, 7:00, 13:00 and 19:00 hrs every day, this can be a cron or rate expression.

I also have a similar requirement for another event rule that fires every day at 2:00, 8:00, 14:00, 20:00 hrs

# will be writing the expressions in terraform scripts like this

resource "aws_cloudwatch_event_rule" "stop_instances" {
  name                = "StopInstance"
  description         = "Stop instances nightly"
  schedule_expression = "cron(0 0 * * ? *)"
}

Solution

  • You could do this as either

    cron(0 1,7,13,19 * * ? *) or cron(0 1/6 * * ? *)

    The first option simply specifies the hours to run at, the second says to run every 6 hours with an offset of 1 hour.

    For more information about cron expressions with CloudWatch events read the docs here