Sub topic 10
Scheduling and autoscaling
Prerequisites
This chapter assumes you know how to launch EC2 instances, read an Auto Scaling group's basic settings, and run AWS CLI commands. It builds on rightsizing: first make each instance the right size, then make sure the right number of them run at the right times.
The working model
You pay for compute for every hour a resource is running, whether or not anyone uses it. Two techniques cut that waste:
- Scheduling turns resources off or down at fixed times, such as nights and weekends for development environments.
- Autoscaling changes the number of running resources automatically as demand changes.
Scheduling suits predictable patterns. Autoscaling suits demand you cannot predict exactly. Many teams use both.
A development environment that is needed 10 hours a day on weekdays runs about 50 of the 168 hours in a week. Stopping it outside those hours removes roughly 70 percent of its compute cost.
Scheduling
Stopping an EC2 instance ends its compute charge. Storage is different. Attached EBS volumes keep billing while the instance is stopped, and so do any Elastic IP addresses that are not in use. Scheduling therefore reduces cost but does not remove it.
You can schedule with Amazon EventBridge Scheduler, which can call the EC2 StopInstances and StartInstances APIs directly. The commands below stop and start instances by tag, which shows the operation your schedule performs. The tag Schedule=office-hours is an example convention.
IDS=$(aws ec2 describe-instances \
--filters Name=tag:Schedule,Values=office-hours Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].InstanceId' \
--output text)
aws ec2 stop-instances --instance-ids $IDSTags make schedules safe to operate. The schedule acts only on resources that opt in, so production systems are not stopped by accident.
Autoscaling
An Auto Scaling group keeps a number of instances between a minimum and a maximum. A scaling policy moves the desired capacity within that range. The most common choice is target tracking, where you pick a metric and a target value, and AWS adds or removes instances to hold the metric near it.
aws autoscaling put-scaling-policy \
--auto-scaling-group-name web-asg \
--policy-name cpu-target-50 \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
},
"TargetValue": 50.0
}'With this policy, the group adds instances when average CPU rises above 50 percent and removes them when it falls well below. Your cost then follows load instead of staying at peak size all day.
For load that follows a calendar, such as a daily peak or a planned campaign, scheduled scaling actions set the minimum and desired capacity at chosen times. You can combine them with target tracking, so the schedule sets a baseline and the policy handles surprises.
Common errors
- Setting the minimum too high. The group never scales below the minimum, so a minimum of 10 means you always pay for 10 instances.
- Scaling on the wrong metric. CPU is a poor signal for a service limited by memory or queue depth. Scale on the metric that actually shows saturation.
- Stopping everything with no start. A schedule that stops resources needs a matching start, and someone must know to expect it.
- Forgetting warm-up time. New instances need time to boot and pass health checks. Without an instance warm-up setting, the group may add more instances than needed.
- Ignoring the storage that remains. Stopped instances still incur EBS charges, so very long idle periods may call for deleting the resource instead.
Exercise
A development server is needed from 08:00 to 18:00 on weekdays. You stop it outside those hours. Roughly what share of its weekly compute cost do you save, and which charge continues while it is stopped?
Show answer
It runs 50 of 168 hours, so you save about 70 percent of compute cost. The attached EBS volume continues to be billed while the instance is stopped.
Exercise
Your Auto Scaling group has a minimum of 8, a maximum of 20, and a target tracking policy at 50 percent CPU. Traffic drops to almost nothing overnight. How many instances run overnight, and how would you lower the cost?
Show answer
Eight instances run, because the group never goes below its minimum. Lower the minimum, or add a scheduled scaling action that reduces the minimum overnight.