Lesson 1 of 4 Scaling Compute, Caching and Global Deployment

Scaling Instances with Amazon EC2 Auto Scaling

Amazon EC2 Auto Scaling keeps the right number of instances running: it replaces unhealthy instances, adds capacity when demand rises and removes it when demand falls. This lesson explains capacity settings, the scaling policy types, warm-up and cooldown, warm pools, lifecycle hooks, mixed instance policies with Spot Instances and termination behaviour.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain minimum, maximum and desired capacity and how Auto Scaling maintains them.
  • Compare target tracking, step, simple, scheduled and predictive scaling policies.
  • Explain instance warm-up, cooldowns, warm pools and lifecycle hooks and the problems each solves.
  • Describe mixed instances policies, Spot capacity rebalancing, termination policies and scale-in protection.

Capacity and Health

An Auto Scaling group has a minimum, a maximum and a desired capacity. The group launches or terminates instances to match the desired capacity, which scaling policies change within the minimum and maximum. The group spreads instances across its Availability Zones and rebalances them if one AZ has more than others. It also replaces instances that fail health checks: EC2 status checks by default, plus Elastic Load Balancing health checks when the health check type includes ELB, as explained in the load balancing lesson. The maximum is an important safety limit: if it is too low, scaling stops exactly when demand is highest.

Scaling Policies

Auto Scaling offers several policy types, described below and compared in a table.

Target tracking

The group adjusts capacity to keep a metric at a target value, such as average CPU utilisation at 50%, ALB request count per target at 1,000, or a custom metric such as queue backlog per instance. Auto Scaling creates and manages the CloudWatch alarms. Target tracking is the recommended default because it scales proportionally and needs little tuning. It scales out quickly and scales in more conservatively.

Step scaling

CloudWatch alarms that the team defines trigger adjustments that vary with the size of the alarm breach, for example adding two instances when CPU is between 60% and 80% and four when it is above 80%. Step scaling suits cases where the relationship between metric and capacity is not proportional or where different responses are needed at different levels.

Simple scaling

A single adjustment per alarm, followed by a cooldown during which no further scaling occurs. It is older and less responsive than step scaling and is rarely the best answer.

Scheduled scaling

Changes minimum, maximum or desired capacity at specified times, for example raising the minimum before business hours or a known event. It handles predictable patterns that reactive policies would follow too late.

Predictive scaling

Uses machine learning on up to 14 days of history to forecast load and schedules capacity ahead of recurring daily and weekly patterns. It can run in forecast-only mode for evaluation. It is usually combined with target tracking, which handles deviations from the forecast.

PolicyTriggerBest for
Target trackingKeeps a metric at a targetMost workloads; proportional scaling
Step scalingCustom alarms with breach-dependent stepsNon-linear responses, custom thresholds
Simple scalingOne alarm, one adjustment, then cooldownLegacy configurations
ScheduledTimeKnown events and business hours
PredictiveForecast from historyRecurring cyclical patterns with slow start-up

For queue-based workers, the right metric is not CPU but backlog per instance: the number of visible SQS messages divided by the number of instances, compared with an acceptable backlog derived from processing time and latency targets. It is published as a custom metric, often with metric math, and used in target tracking.

Sort into groups

Choose the scaling policy

Exam skills 4.3.1 · 3.2.2

Choose the EC2 Auto Scaling policy for each workload.

Log in to try this activity Interactive activities are free with an account.

Warm-Up, Cooldown and Warm Pools

New instances need time to start and warm up before they contribute. The default instance warm-up tells Auto Scaling how long to wait before a new instance's metrics count towards group metrics, which prevents scaling out again because the new instance has not yet reduced average CPU. Cooldowns apply to simple scaling and pause further activity after a scaling action.

If instances take a long time to become ready, for example 10 minutes to load data, even a well-tuned policy reacts too slowly to sudden spikes. A warm pool keeps pre-initialised instances in a stopped, hibernated or running state alongside the group. When the group scales out, it takes instances from the warm pool, which become ready in a fraction of the time. Stopped warm pool instances cost only for their EBS volumes. Warm pools suit applications with long initialisation; golden AMIs that reduce initialisation time are a complementary approach.

The following diagram compares a cold launch with an instance taken from a warm pool.

Why a warm pool shortens scale-out. Without one, a new instance must launch, boot and initialise, which for some applications takes ten minutes or more before it can serve. A warm pool keeps instances that are already initialised in a stopped, hibernated or running state; on scale-out, the group takes one from the pool and it is ready in a fraction of the time. Stopped warm pool instances cost only for their EBS volumes.
Durations are illustrative.

Lifecycle Hooks

Lifecycle hooks pause instances during launch or termination so that custom actions can run. A launch hook keeps a new instance in Pending:Wait, for example while software is installed or the instance is registered with a configuration system; a termination hook keeps an instance in Terminating:Wait, for example while logs are copied or work is drained. The hook sends a notification to EventBridge, SNS or SQS, and the action ends when something calls CompleteLifecycleAction with CONTINUE or ABANDON, or when the heartbeat timeout expires and the default result applies. A typical automation is an EventBridge rule on the termination lifecycle action that runs an SSM Automation runbook to upload logs to S3 and then completes the action.

The following diagram shows the wait states that lifecycle hooks add.

Auto Scaling lifecycle hooks. A launch hook holds a new instance in Pending:Wait, for example while software is installed, before it moves to InService. A termination hook holds an instance in Terminating:Wait, for example while logs are copied or work drains, before it is terminated. The hook notifies EventBridge, SNS or SQS, and the wait ends when CompleteLifecycleAction is called with CONTINUE or ABANDON, or the heartbeat timeout applies the default result.
Hooks give time for custom actions during launch and termination.

Losing logs on scale-in

If an instance is terminated by scale-in before its logs are collected, logs on the instance are lost. A termination lifecycle hook gives time to collect them; alternatively, ship logs continuously with the CloudWatch agent.

True or false

Warm-up, pools and hooks

Exam skills 4.3.1

Check four facts about Auto Scaling warm-up, warm pools and lifecycle hooks.

Log in to try this activity Interactive activities are free with an account.

Mixed Instances, Spot and Termination

A mixed instances policy lets one group use several instance types and both On-Demand and Spot Instances. The policy sets an On-Demand base capacity and the percentage of On-Demand above it, and an allocation strategy. For Spot, price-capacity-optimised chooses pools with the lowest interruption risk and low price and is recommended. Attribute-based instance type selection specifies vCPU and memory ranges instead of listing types, so new types are used automatically. Diversifying across many types and AZs makes Spot capacity more available.

Capacity rebalancing launches a replacement when EC2 signals that a Spot Instance is at elevated risk of interruption, before the two-minute interruption notice, so the group stays at capacity. Termination policies decide which instance to remove during scale-in; the default balances AZs and then prefers instances with the oldest launch template or configuration. Scale-in protection on an instance prevents its termination by scale-in, for example while it processes a long job, and instance maintenance policies control whether replacements launch before or after terminations during refreshes and health replacements.

Worked example

A Queue Worker Fleet

Workers on EC2 process jobs from an SQS queue. Each job takes about 30 seconds, jobs must start within 10 minutes of arrival, load peaks sharply every morning, workers take eight minutes to initialise, and cost must be minimised.

  1. Metric

    Publish backlog per instance (visible messages divided by in-service instances). One worker processes 20 jobs in 10 minutes, so the target is 20.

  2. Policies

    Use target tracking on backlog per instance, plus predictive scaling for the morning peak.

  3. Start-up

    Add a warm pool of stopped instances so scale-out takes under a minute.

  4. Cost

    Use a mixed instances policy with 20% On-Demand, price-capacity-optimised Spot across many types and capacity rebalancing.

  5. Graceful scale-in

    Add a termination lifecycle hook so workers finish their current job before terminating.

Check yourself

An Auto Scaling group's CPU is at 95% for an hour, but no instances are added. The target tracking policy is working. What should be checked?

Show answer

Whether the group has reached its maximum capacity. Scaling policies cannot exceed the maximum. Other causes include launch failures, such as insufficient capacity, quota limits or subnet IP exhaustion, visible in the group's activity history.

Exam Tips

Remember for the exam

  • Prefer target tracking; use step scaling for non-proportional responses, scheduled scaling for known events and predictive scaling for recurring patterns.
  • Scale SQS workers on backlog per instance, not CPU.
  • Warm pools pre-initialise instances for fast scale-out; default instance warm-up stops new instances skewing metrics.
  • Lifecycle hooks pause launches (Pending:Wait) and terminations (Terminating:Wait) until CompleteLifecycleAction or timeout.
  • Mixed instances policies with price-capacity-optimised Spot and capacity rebalancing reduce cost; scale-in protection guards busy instances.