Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 70 ยท Load balancing, scaling and delivery

Auto Scaling Groups

Learn EC2 Auto Scaling: launch templates, min/desired/max, target tracking, step and scheduled scaling, lifecycle hooks and instance refresh, with simulators.

A load balancer spreads traffic, but it does not create servers. Someone, or something, must add instances when traffic grows and remove them when it falls. An Auto Scaling group (ASG) is that something. It keeps a fleet of identical EC2 instances at the size you want, replaces instances that fail, spreads them across Availability Zones, and changes the size automatically by following policies you write.

This lesson shows how a group is defined (launch template and capacity limits), the four ways to scale (target tracking, step, scheduled, predictive), lifecycle hooks for custom work at launch and termination, and instance refresh for rolling out a new image. It builds on EC2 Instances and Elastic Load Balancing.

Auto Scaling runs inside AWS and cannot run on this site. The CLI and JSON snippets are read-only. The Python examples are simplified simulators: they model the arithmetic and the order of events, not real instance boot times, CloudWatch delays or cooldown details.

What an Auto Scaling group is

A group has three parts:

  1. A launch template that describes one instance: AMI, instance type, key pair, security groups, instance profile, user data, EBS settings, and tags. Templates are versioned, so you can create version 2 with a new AMI and point the group at it. (The older launch configurations are deprecated; use launch templates.)
  2. Capacity limits: MinSize, MaxSize and DesiredCapacity. The group always keeps the number of instances between min and max, and scaling policies change the desired number inside that range. Min protects you from scaling to zero by mistake, and max is your cost ceiling.
  3. Where to place instances: a list of subnets in different Availability Zones. The group balances instances across them and rebalances after a zone problem.
flowchart TD
    LT["Launch template<br/>AMI, type, security groups, user data"] --> ASG["Auto Scaling group<br/>min 2, desired 4, max 10"]
    P["Scaling policies<br/>target tracking, step, scheduled"] --> ASG
    ASG --> A["Subnet in AZ a"]
    ASG --> B["Subnet in AZ b"]
    ASG --> C["Subnet in AZ c"]
    ALB["Application Load Balancer<br/>target group"] --> A
    ALB --> B
    ALB --> C

The group launches instances from the template into several zones, and registers them with the load balancer's target group.

Creating a group with the CLI (read-only; the ARNs are placeholders):

Terminal
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name web-asg \
  --launch-template LaunchTemplateName=web,Version='$Latest' \
  --min-size 2 --max-size 10 --desired-capacity 4 \
  --vpc-zone-identifier "subnet-0aaa,subnet-0bbb,subnet-0ccc" \
  --target-group-arns arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/web/6d0ecf831eec9f09 \
  --health-check-type ELB --health-check-grace-period 300

Health checks and replacement

By default the group checks the EC2 status: an instance that is stopped or fails its system checks is terminated and replaced. With --health-check-type ELB the group also trusts the load balancer, so an instance that serves errors (but is running) is replaced too. The grace period (default 300 seconds) gives a new instance time to boot before health checks count against it. If your application takes four minutes to start and the grace period is one minute, the group will kill every new instance in a loop. That is one of the classic ASG mistakes.

Because the group replaces failed instances, you can use one with min = max = 1 as a cheap way to keep a single server alive.

Ways to scale

Target tracking

You pick a metric and a target value, and AWS creates and manages the alarms and the arithmetic. Common predefined metrics are ASGAverageCPUUtilization, ASGAverageNetworkIn/Out and ALBRequestCountPerTarget. It works like a thermostat: "keep average CPU at 50 percent". This is the first policy to reach for.

Terminal
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name web-asg \
  --policy-name cpu-50 \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{
    "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageCPUUtilization" },
    "TargetValue": 50.0
  }'

The core idea is proportional: if 4 instances run at 80 percent and the target is 50, you need about 4 ร— 80 / 50 = 6.4, so 7 instances. Scale-out is eager; scale-in is deliberately slower, because removing too much capacity hurts users more than keeping some extra for a while.

Step scaling

You write a CloudWatch alarm (for example CPU above 60 percent) and a table of steps that say how much to add depending on how far the metric is past the threshold: add 1 instance when it is 0 to 10 points over, 2 when it is 10 to 20 over, 4 beyond that. You control the response curve, at the cost of tuning and maintaining alarms. Prefer target tracking unless you need a custom curve.

Scheduled and predictive scaling

Scheduled actions change min, max or desired at set times, such as scaling up before a daily 9:00 peak or down overnight. They are the right tool for known, regular events:

Terminal
aws autoscaling put-scheduled-update-group-action \
  --auto-scaling-group-name web-asg \
  --scheduled-action-name weekday-morning \
  --recurrence "45 8 * * 1-5" --time-zone "Europe/Berlin" \
  --min-size 6 --desired-capacity 8

Predictive scaling looks at the past 14 days of load and schedules capacity ahead of expected demand. It works well for cyclical traffic and is often combined with target tracking, which still handles the surprises.

The simulator below compares two policies on the same load pattern. Each instance serves up to 100 requests per second, so CPU is modeled as load / (instances ร— 100). The target-tracking policy follows the proportional rule with a one-instance-at-a-time scale-in. The step policy uses the alarm at 60 percent shown above and one alarm at 30 percent to remove an instance. Both are clamped between min 2 and max 10.

Python Target tracking vs step scaling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the first rows. In minute 0 the group starts with 2 instances at 75 percent CPU. Target tracking computes 3 instances, which gives exactly 50 percent. The step policy only knows "more than 15 points over the threshold", so it adds 2 and ends at 38 percent, which is more than needed. In minute 1 the roles flip: tracking grows to 5 instances (44 percent), while the step policy sits at 4 instances and 55 percent because that is inside its dead zone between 30 and 60. In minutes 3 to 5 both policies are stuck at the maximum of 10 instances, and CPU rises to 70 percent: the cap, not the policy, is the limit, which is why MaxSize is a cost decision. On the way down both remove one instance per minute. The numbers show the shape of each policy, not a promise: real alarms need several evaluation periods before they fire, and new instances need time to warm up.

Horizontal versus vertical scaling

Scaling comes in two shapes. Horizontal scaling adds or removes instances, which is what an Auto Scaling group does. It has no practical ceiling, survives the loss of any one instance, and needs no downtime, but the application must be able to run on several servers (stateless web tiers, shared session store). Vertical scaling makes one instance bigger or smaller by changing its type. On EC2 that means stopping the instance, changing the instance type and starting it again, so there is a short outage. It is simple, and the only choice for software that cannot spread across machines (a single relational primary, for example), but it hits the largest available size and leaves you with one point of failure.

The scaling policies above are all reactive: they respond to a metric after load changes. Scheduled scaling and predictive scaling act before the load arrives. Combine them with a multi-Availability-Zone group so that scaling out also spreads you across zones, and with containers or small images so that new capacity becomes useful quickly.

Lifecycle hooks

Sometimes an instance needs extra work before it takes traffic or before it disappears: download a model, register with a licensing server, drain connections, or upload logs. A lifecycle hook pauses the instance in a wait state:

  • autoscaling:EC2_INSTANCE_LAUNCHING holds a new instance in Pending:Wait.
  • autoscaling:EC2_INSTANCE_TERMINATING holds an old one in Terminating:Wait.

While it waits, you run your own code (often a Lambda function triggered through EventBridge or an SQS message). When the work is done it calls complete-lifecycle-action with CONTINUE or ABANDON. If nothing answers before the heartbeat timeout (3600 seconds by default), the group applies the default result you configured. A hook is more flexible than user data, because it can wait for outside systems and also run when an instance terminates.

Instance refresh

When you publish a new AMI as a new launch template version, existing instances keep running the old image. Instance refresh replaces them gradually without downtime:

Terminal
aws autoscaling start-instance-refresh \
  --auto-scaling-group-name web-asg \
  --preferences '{"MinHealthyPercentage": 80, "InstanceWarmup": 120}'

MinHealthyPercentage is the share of the desired capacity that must stay healthy during the replacement. With 80 percent and 6 instances, the group may replace 1 at a time; with 50 percent it may replace 3. InstanceWarmup is how long a new instance must be up before it counts as healthy. Lower the percentage to finish faster, raise it to keep more capacity during the rollout. You can also use checkpoints and automatic rollback on alarms.

Python Rolling instance refresh

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 80 percent the rollout takes six steps and capacity never drops below 5 of 6. With 50 percent it takes two steps, but capacity falls to 3 of 6 for a moment, which might be too little for peak traffic. In real groups, new instances can also be launched first (extra capacity above the desired number) when you allow it, so the choice is a trade-off between speed, cost and risk.

Other settings worth knowing

  • Termination policy. The default picks the zone with the most instances, then (among those) an instance with the oldest launch template, then the one closest to the next billing hour. You can choose other policies, and scale-in protection keeps specific instances safe.
  • Mixed instances and Spot. A group can combine several instance types and purchasing options, for example 2 On-Demand instances as a base and the rest Spot, as described in On-Demand, Reserved, Savings Plans and Spot.
  • Warm pools. Keep pre-initialized stopped instances ready, so scale-out is quick when your application needs minutes to start.
  • Cooldowns and warmup. Target tracking uses the instance warmup time so that booting instances do not distort the average metric. Simple scaling policies use a cooldown, which is mostly replaced by the policies above.
  • Auto Scaling beyond EC2. The same idea applies to ECS services, DynamoDB capacity and Lambda provisioned concurrency through Application Auto Scaling, which you will see in Containers: ECR, ECS and Fargate.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. An Auto Scaling group has min 2, desired 4, max 8. A scaling policy computes that 12 instances are needed. How many instances does the group run?

    Min and max are hard limits. Policies change the desired capacity only inside that range, so the group stops at 8.
  2. 2. Which policy type is the best first choice for keeping average CPU near 50 percent?

    Target tracking works like a thermostat. You set the target and AWS manages the alarms and the amount to add or remove.
  3. 3. New instances keep being terminated about a minute after they launch, while the application needs four minutes to start. What is the likely cause?

    Health checks count against an instance as soon as the grace period ends. If the app is still starting, the group treats it as failed and replaces it, again and again.
  4. 4. What does MinHealthyPercentage control in an instance refresh?

    A higher value replaces fewer instances at a time and keeps more capacity, a lower value finishes faster but leaves less spare capacity during the rollout.
  5. 5. When would you use a lifecycle hook?

    Hooks pause an instance in Pending:Wait or Terminating:Wait so that your code can finish setup or cleanup, then continue or abandon the action.

Summary

  • An Auto Scaling group keeps a fleet from a launch template between min and max, spread over subnets in several zones, and replaces unhealthy instances.
  • Use --health-check-type ELB and a grace period longer than your boot time, so broken instances are replaced and healthy ones are left alone.
  • Target tracking is the simplest policy. Step scaling gives a custom response curve, scheduled scaling handles known peaks, and predictive scaling learns daily patterns.
  • Lifecycle hooks pause launch and termination for custom work, with a heartbeat timeout as the safety net.
  • Instance refresh rolls out a new template version in batches, and MinHealthyPercentage trades rollout speed against spare capacity.

Next: CloudFront CDN