Monday, January 29, 2024

[SOLVED] AWS Scheduler creation through TF fails due to cron validation error

Issue

The TF plan shows the following:

# aws_scheduler_schedule.gh_runners_ubuntu_start["gh_runner_ubuntu"] will be created
  + resource "aws_scheduler_schedule" "gh_runners_ubuntu_start" {
      + arn                          = (known after apply)
      + group_name                   = (known after apply)
      + id                           = (known after apply)
      + name                         = "gh_runners_ubuntu_start-ormos-runner"
      + name_prefix                  = (known after apply)
      + schedule_expression          = "cron(0 23 * * *)"
      + schedule_expression_timezone = "UTC"
      + state                        = "ENABLED"
      + flexible_time_window {
          + mode = "OFF"
        }
      + target {
          + arn      = "arn:aws:scheduler:::aws-sdk:ec2:startInstances"
          + input    = jsonencode(
                {
                  + InstanceIds = [
                      + "i-xxxxxxxxxxxxxxxxx",
                    ]
                }
            )
          + role_arn = "arn:aws:iam::12345678:role/gh_runners_ubuntu_scheduler_role-runner"
        }
    }

Creation fails with:

ValidationException: Invalid Schedule Expression cron(0 23 * * *).

Any ideas?


Solution

The terraform docs provide a link to the AWS docs about the schedule expression

schedule_expression - (Required) Defines when the schedule runs. Read more in Schedule types on EventBridge Scheduler.

If you reach the cron expression docs it tells you the format is

cron(minutes hours day-of-month month day-of-week year)

where the year part is optional. You are passing 5 parameters to cron expression which is correct. However if you read the docs it states

The * (asterisk) wildcard includes all values in the field. In the Hours field, * includes every hour. You can't use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other.

You are using * in both the day-of-mont field and the day-of-week field which is not allowed. Thats why you get invalid expression. you should instead set it as

schedule_expression          = "cron(0 23 * * ?)"


Answered By - Chris Doyle
Answer Checked By - Mary Flores (WPSolving Volunteer)