Scheduled scaling for EC2 Auto Scaling
December 23, 2021
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