Blog

Scheduled scaling for EC2 Auto Scaling

23 Dec, 2021
Xebia Background Header Wave

Scheduled scaling for EC2 Auto Scaling can help out with predicable load by specifying capacity changes on a schedule.
For example when a busy period is expected or to save money for your development environment scaling them down outside office hours.
Scheduled autoscaling supports one time only scaling events or on a recurring basis.
To create a recurring scaling schedule you can use a cron expression with optionally a start and/or end date and time.
The examples are for Terraform and CloudFormation.
Both will do the following:

  • monday-friday scale down at 22:00
  • monday-friday scale up at 06:00

Terraform

resource "aws_autoscaling_schedule" "development-up" {
  scheduled_action_name  = "development-up"
  min_size = 1
  max_size = 3
  desired_capacity = 2
  time_zone = "Europe/Amsterdam"
  recurrence = "00 06 * * 1-5"
  autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_autoscaling_schedule" "development-down" {
  scheduled_action_name  = "development-down"
  min_size = 0
  max_size = 0
  desired_capacity = 0
  time_zone = "Europe/Amsterdam"
  recurrence = "00 22 * * 1-5"
  autoscaling_group_name = aws_autoscaling_group.example.name
}

CloudFormation

Resources:
  ScheduledActionUp:
    Type: AWS::AutoScaling::ScheduledAction
    Properties:
      AutoScalingGroupName: !Ref autoScalingGroup
      MaxSize: '3'
      MinSize: '1'
      DesiredCapacity: '2'
      TimeZone: 'Europe/Amsterdam'
      Recurrence: 00 06 * * 1-5
  ScheduledActionDown:
    Type: AWS::AutoScaling::ScheduledAction
    Properties:
      AutoScalingGroupName: !Ref autoScalingGroup
      MaxSize: '0'
      MinSize: '0'
      DesiredCapacity: '0'
      TimeZone: 'Europe/Amsterdam'
      Recurrence: 00 22 * * 1-5

Source

AWS Scheduled scaling for Amazon EC2 Auto Scaling

Tibor Hercz
Tibor is a Cloud Consultant specialized in AWS with a strong background in Software engineering and has a passion for Compute, Networking and Security. His goal is to create simple Cloud Solutions that increases the efficiency and overall happiness of the teams and business. Sharing knowledge is important to him, so you will see him blogging and sharing knowledge about solutions he has built.
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts