Terraform aws aws_autoscaling_schedule with recurrance , start time initiates on next day instead current date

Viewed 2444

We have asg with current policy. example current date time UTC : 23 May 2020 , 0600 AM and i have policy to stop and start instance at cron time 1000 AM but when i m applying now terraform plan , at aws we can see policy start time being taken as 24 MAY 1000 AM.

so question is why it does not consider today's date

policy objective : for everyday demolish and create new instance

resource "aws_autoscaling_schedule" "scale_down" {
  scheduled_action_name = "scale_down"
  min_size = 0
  max_size = 0
  recurrence = "0 10 * * *"
  desired_capacity = 1
  autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
}
resource "aws_autoscaling_schedule" "scale_up" {
  scheduled_action_name = "scale_up"
  min_size = 1
  max_size = 1
  recurrence = "10 10 * * *"
  desired_capacity = 1
  autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
}
3 Answers

I have workaround it in this way, take a look and give me your opinion. The idea with the recurrence is once you created you don't modify anymore. I put the last minute of the day.

  locals {   
    today_date = formatdate("YYYY-MM-DD",timestamp())
    min = 0
    max = 1
  }
  resource "aws_autoscaling_schedule" "off" {
      scheduled_action_name  = "off_instance"
      min_size               = local.min
      max_size               = local.max
      desired_capacity       = 0
      start_time             =  "${local.today_date}T23:59:00Z" 
      recurrence             = "0 ${var.hour_to_switch_off} * * *"
      autoscaling_group_name = aws_autoscaling_group.bastion.name
      lifecycle {
        ignore_changes = [start_time]
      }
    }
    
    resource "aws_autoscaling_schedule" "on" {
      scheduled_action_name  = "on_instance"
      min_size               = local.min
      max_size               = local.max
      desired_capacity       = 1
      start_time             =  "${local.today_date}T23:59:00Z" 
      recurrence             = "0 ${var.hour_to_switch_on} * * MON-FRI"
      autoscaling_group_name = aws_autoscaling_group.bastion.name
      lifecycle {
        ignore_changes = [start_time]
      }
    }

The start date of an asg schedule is problematic, even if you set it in the UI. Sometimes it assigns as the start day today, some other tomorrow.

You can however bypass this, by manually editing the start day and time after you make a change from terraform, and verify that it is the one you want.

I was able to use the timestamp/timeadd function and added an extra 30 min. This allowed the initial deploy to work. But it gets complicate. What I've observed is the "Start Time" is what runs the show. So you'll most likely want to deploy your start time a little after the cron/recurrence is suppose to run. That recurrence will then reset the start time to the next recurrence. Be sure to include the lifecycle ignore changes so that reapplying will not trigger an update to the start time.

resource "aws_autoscaling_schedule" "full" {
  scheduled_action_name  = "up_scale"
  min_size               = var.asg_min
  max_size               = var.asg_max
  desired_capacity       = var.asg_desired_capacity
  recurrence             = var.asg_schedule_full_cron
  start_time             = timeadd(timestamp(), "30m") #adjust to runtime
  time_zone              = "US/Central" #set to your region
  autoscaling_group_name = aws_autoscaling_group.JBoss_EAP_ASG.name
   # Ignore changes to the start_time, otherwise the resource is re-created with every apply
  lifecycle {
        ignore_changes = [start_time]
      }
}
Related