Scheduled Scaling with Autoscaling Groups

Sometimes we want to scale up and scale down the desired capacity of an autoscaling group based on a schedule.

For example:

To do this, Amazon offers scheduled scaling

Terraform supports creating scaling schedules using the aws_autoscaling_schedule resource.

Example

Here are some things learned from using aws_autoscaling_schedule:

Scheduled Scale Down

resource "aws_autoscaling_schedule" "scheduled_scale_down" {
  scheduled_action_name  = "my-cluster-scale-down"
  min_size               = 1
  max_size               = -1
  desired_capacity       = 1
  start_time             = "${local.upcoming_friday}T23:59:00Z"
  recurrence             = "59 23 * * FRI"
  time_zone              = "US/Pacific"
  autoscaling_group_name = aws_autoscaling_group.my_cluster.name
  lifecycle {
    ignore_changes = [start_time]
  }
}

Scheduled Scale Up

resource "aws_autoscaling_schedule" "scheduled_scale_up" {
  scheduled_action_name  = "my-cluster-scale-down"
  min_size               = local.my_cluster_member.min_size
  max_size               = local.my_cluster_member.max_size
  desired_capacity       = local.my_cluster_member.desired_capacity
  start_time             = "${local.upcoming_monday}T03:00:00Z"
  recurrence             = "00 03 * * MON"
  time_zone              = "US/Pacific"
  autoscaling_group_name = aws_autoscaling_group.my_cluster.name
  lifecycle {
    ignore_changes = [start_time]
  }
}