SkillSprout
Chapter 5 · Lesson 2
Intermediate
8 min read

Auto Scaling

Overview

Amazon EC2 Auto Scaling automatically adds or removes EC2 instances to match demand. You define a minimum, desired and maximum number of instances in an Auto Scaling group (ASG), plus scaling rules based on metrics such as CPU or request count. When traffic climbs, the group launches instances; when it falls, instances are terminated so you stop paying for idle capacity.

An ASG also provides self-healing. It continuously checks the health of its instances (EC2 status checks and optionally load balancer health checks) and replaces any unhealthy instance. Because the group spans multiple Availability Zones, it keeps capacity balanced and survives an AZ failure.

ASGs, load balancers and launch templates work together: the launch template says what to launch, the ASG says how many and where, and the load balancer spreads traffic across the instances. This trio is the textbook answer to "highly available and elastic" on the SAA-C03.

Key Concepts

  • An ASG defines minimum, desired and maximum capacity and spans one or more subnets/AZs in a single Region.
  • A launch template (preferred; launch configurations are deprecated) defines AMI, instance type, key, SGs, IAM role, user data and storage.
  • Scaling policies: target tracking (e.g. keep average CPU at 50%), step, simple, scheduled, and predictive (ML-based forecasting).
  • Health checks: EC2 status checks by default; enable ELB health checks so instances failing the application test are replaced.
  • Cooldown / warm-up periods stop the group from over-scaling while new instances start (default cooldown 300 seconds).
  • Termination policy defaults to balancing AZs first, then terminating the instance with the oldest launch template/configuration, then closest to the next billing hour.
  • Lifecycle hooks pause instances in Pending:Wait or Terminating:Wait to run custom actions (install agents, drain connections, upload logs).
  • Instance refresh performs rolling replacement when you change the AMI/launch template; warm pools keep pre-initialised instances ready.
  • Mixed instances policy combines On-Demand and Spot across instance types for cost optimisation.

Detailed Notes

ASG building blocks

ComponentPurpose
Launch templateWhat to launch: AMI, type(s), SGs, IAM role, user data, tags, versions
Auto Scaling groupWhere and how many: subnets/AZs, min/desired/max, health checks, policies
Scaling policyWhen to change the desired capacity
Load balancer / target groupWhere traffic goes; provides health checks
Lifecycle hookCustom action during launch/terminate

The group never exceeds max or drops below min; scaling policies change desired.

Scaling policy types

PolicyHow it worksUse when
Target trackingKeep a metric at a target (CPU 50%, ALBRequestCountPerTarget, network in/out); ASG creates the CloudWatch alarmsDefault recommendation — simplest
Step scalingDifferent adjustments for different alarm breach sizesNeed finer control of response
Simple scalingOne alarm, one adjustment, then cooldownLegacy
ScheduledChange min/max/desired at set timesKnown patterns (business hours, sales)
PredictiveML forecasts from up to 14 days of history and scales ahead of demandRecurring cyclical traffic

Common predefined metrics: ASGAverageCPUUtilization, ASGAverageNetworkIn/Out, ALBRequestCountPerTarget. Memory is not a default metric — publish a custom metric with the CloudWatch agent.

json
{
  "AutoScalingGroupName": "web-asg",
  "PolicyName": "cpu50",
  "PolicyType": "TargetTrackingScaling",
  "TargetTrackingConfiguration": {
    "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageCPUUtilization" },
    "TargetValue": 50.0
  }
}

Creating an ASG

bash
aws autoscaling create-auto-scaling-group --auto-scaling-group-name web-asg \
  --launch-template LaunchTemplateName=web-lt,Version='$Latest' \
  --min-size 2 --max-size 10 --desired-capacity 2 \
  --vpc-zone-identifier "subnet-0aaa,subnet-0bbb" \
  --target-group-arns arn:aws:elasticloadbalancing:us-east-1:111122223333:targetgroup/web/abc \
  --health-check-type ELB --health-check-grace-period 300

Health checks and self-healing

  • EC2 health — instance state and system/instance status checks.
  • ELB health — enable by setting health check type to ELB; the ASG replaces instances that the load balancer marks unhealthy.
  • Health check grace period (default 300 seconds) gives new instances time to boot before checks count.
  • If an AZ becomes unavailable, the ASG launches replacements in the remaining AZs (AZ rebalancing).

Termination and rebalancing

Default termination policy: (1) pick the AZ with the most instances, (2) within it choose the instance with the oldest launch template/configuration, (3) if tied, the one closest to the next billing hour. Scale-in protection protects selected instances. Use a lifecycle hook to drain connections or ship logs before termination.

Cost optimisation: mixed instances and Spot

A mixed instances policy lets one ASG use multiple instance types and purchasing options, for example a base of On-Demand with extra capacity on Spot, using the capacity-optimized allocation strategy to reduce interruptions.

Updating a fleet

  • Instance refresh rolls out a new launch template version gradually, controlled by minimum healthy percentage.
  • Blue/green or canary — separate ASGs behind weighted target groups.
  • Warm pools hold pre-initialised stopped/running instances to cut scale-out latency for slow-starting apps.

Key numbers

ItemDefault
Default cooldown300 seconds
Health check grace period300 seconds
Default instance warm-upUses cooldown unless set
ASGs per Region500
Launch configurations per Region200 (deprecated; use templates)
Predictive scaling forecast horizon48 hours ahead, 14 days of history

ASG vs. other scaling services

Auto Scaling is also available as AWS Auto Scaling / Application Auto Scaling for DynamoDB capacity, ECS services, Aurora replicas, Lambda provisioned concurrency and more — the same policy concepts apply.

Troubleshooting

  • Instances keep terminating immediately — health check grace period too short, failing ELB checks, or user data errors.
  • Not scaling out — max reached, subnet out of IPs, vCPU quota exceeded, or unavailable instance type in the AZ.
  • Thrashing — cooldown/warm-up too low or scale-in/out thresholds too close.

Real World Example

Scenario — an online ticketing site survives a concert on-sale.

A ticketing company sells out arena shows in minutes. Normal traffic needs about six web servers, but on-sale moments can need 200.

Their architecture uses an ALB and an Auto Scaling group (min 6, max 300) across three AZs. A scheduled action raises the minimum to 60 fifteen minutes before a known on-sale, so servers are already warm. A target-tracking policy on ALBRequestCountPerTarget handles the real surge, and a predictive scaling policy handles regular weekend peaks.

A warm pool of pre-initialised instances cuts scale-out time from four minutes to under one. The ASG uses a mixed instances policy with a base of On-Demand and Spot for the rest to control cost, and ELB health checks replace any instance that fails to serve pages. After the event, scale-in policies and a lifecycle hook drain connections and flush logs to S3 before termination, bringing the fleet back to six servers and cost back to baseline.

Architecture Diagram

Auto Scaling group behind an Application Load Balancer
Architecture
AWS Cloud
Users
Region
Application Load Balancer
Auto Scaling groupmin 2 / max 10
VPC
AZ A
Private subnet A
Instance
Instance
AZ B
Private subnet B
Instance
Instance
CloudWatch alarmsCPU / request count

CloudWatch alarms drive scaling; the ASG spreads instances across AZs and replaces unhealthy ones.

  1. 1The ALB distributes requests across healthy instances in all AZs.
  2. 2CloudWatch publishes metrics (CPU, requests per target) and alarms evaluate them.
  3. 3A scaling policy changes the ASG's desired capacity within min and max.
  4. 4The ASG launches from the launch template or terminates instances and rebalances AZs; failing ELB health checks cause replacement.

Best Practices

  • ✓Use at least two AZs and balance instances across them; the group rebalances automatically.
  • ✓Prefer target tracking policies for their simplicity, and add scheduled/predictive scaling for known patterns.
  • ✓Enable ELB health checks so application-level failures trigger replacement, with an appropriate grace period.
  • ✓Use launch templates with versioning and instance refresh for rolling updates; bake golden AMIs for fast start.
  • ✓Design instances to be stateless: store sessions in ElastiCache/DynamoDB, files in S3/EFS, data in RDS.
  • ✓Use mixed instances with Spot for cost savings, diversified across several instance types and AZs.
  • ✓Use lifecycle hooks and connection draining (deregistration delay) for graceful scale-in.
  • ✓Monitor scaling activity with CloudWatch and set alarms for the max-capacity cap; check service quotas before big events.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • ASG + ALB across multiple AZs = highly available and elastic. It is the default correct answer for web tiers.
  • Scaling is horizontal (add instances). Resizing an instance type is vertical and requires downtime.
  • Target tracking is the simplest and usually the best answer; scheduled for known events; predictive for recurring patterns.
  • Health checks: to replace instances failing the application check, enable ELB health checks on the ASG.
  • Launch templates replace launch configurations; templates support versions, Spot and multiple instance types.
  • Memory utilisation is not a built-in metric — use the CloudWatch agent to publish a custom metric.
  • Warm pools / lifecycle hooks / instance refresh are the tools for fast scale-out, custom actions and rolling updates.
  • An ASG can't span Regions; for multi-Region use Route 53 with an ASG in each Region.

Finished this lesson?

0 of 64 lessons done · 0% of the course