Q: How does Auto Scaling determine when to scale in vs scale out?
EC2 Auto Scaling scale in vs scale out decisions come from CloudWatch alarms and scaling policies: when a metric crosses a threshold the group adds instances (scale out), and when it stays well below the target for long enough.
Want to master this scenario in a live sandbox? Stephane Maarek's AWS Certified DevOps Engineer Professional Masterclass on Udemy covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: EC2 <strong>Auto Scaling scale in vs scale out</strong> decisions come from CloudWatch alarms and scaling policies: when a metric crosses a threshold the group adds instances (scale out), and when it stays well below the target for long enough the group removes them (scale in), always bounded by minimum and maximum size. Interviewers ask this to find out whether you know the asymmetry: scale out is aggressive, scale in is deliberately cautious, and different policy types behave differently. A good answer explains target tracking, step scaling, warmup and cooldown, and how Auto Scaling chooses which instance to terminate.
Decision model: metric, alarm, policy, capacity limits
Every dynamic decision is a CloudWatch alarm triggering a policy that changes desired capacity.
- Desired capacity: the number of instances the group tries to run; always clamped between
MinSizeandMaxSize. - Target tracking: you pick a metric and target (for example average CPU 50%). Auto Scaling creates and manages the alarms and computes capacity to keep the metric near the target.
- Step scaling and simple scaling: you define alarms and adjustments yourself. Step scaling can apply bigger adjustments for bigger breaches; simple scaling waits for a cooldown after each action.
- Scheduled and predictive scaling: change capacity by calendar or forecast, complementing reactive policies.
- Evaluation periods: an alarm needs several consecutive datapoints in breach, so brief spikes under a minute or two rarely trigger action. Detailed monitoring (1-minute metrics) makes decisions faster than the 5-minute basic default.
Hands-on: scale out vs scale in with target tracking
Create a policy and inspect the alarms it generates.
cat > tt.json <<'EOF'
{
"TargetValue": 50.0,
"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"},
"DisableScaleIn": false
}
EOF
aws autoscaling put-scaling-policy --auto-scaling-group-name web-asg \
--policy-name cpu50 --policy-type TargetTrackingScaling \
--target-tracking-configuration file://tt.json
# See the alarms Auto Scaling created and the policy details
aws autoscaling describe-policies --auto-scaling-group-name web-asg
aws cloudwatch describe-alarms --alarm-name-prefix TargetTracking-web-asg
- Scale-out alarm: fires when the metric exceeds the target for a short evaluation window, so capacity rises quickly.
- Scale-in alarm: fires only when the metric is below the target by a comfortable margin for a longer window; the exact thresholds are managed by AWS and are intentionally conservative.
- Multiple policies: the group scales out if any policy asks for it, but scales in only when all target tracking policies agree, with the largest capacity winning.
- Disable scale in: the option
DisableScaleInlets a policy only scale out, useful for stateful or slow-draining workloads. - Math behind target tracking: it scales proportionally to how far the metric is from the target, for example roughly doubling capacity when the metric is double the target, rather than adding a fixed step.
Timing controls: warmup, cooldown and scale-in protection
These settings stop flapping and premature termination.
- Instance warmup: for target tracking and step scaling, newly launched instances are excluded from the aggregated metric until warmup ends, so the group does not over-provision while they boot.
- Cooldown: applies to simple scaling and also as a default; after an activity, further simple-scaling actions wait. Default is 300 seconds unless changed.
- Scale-in protection: mark instances so they are not selected during scale in, for example while processing a long job.
- Lifecycle hooks: pause termination to drain connections or upload logs before the instance goes away.
- Health checks: unhealthy instances are replaced independent of scaling policies; a failing ELB health check causes replacement, not scale in.
- Cooldown vs warmup: they solve different problems; warmup keeps new instances out of the metric, cooldown spaces out simple scaling actions.
Which instance gets terminated on scale in
The default termination policy balances first, then ages.
# Protect a busy instance from scale in, then set a termination policy
aws autoscaling set-instance-protection --auto-scaling-group-name web-asg \
--instance-ids i-0abc1234def567890 --protected-from-scale-in
aws autoscaling update-auto-scaling-group --auto-scaling-group-name web-asg \
--termination-policies OldestLaunchTemplate Default
aws autoscaling describe-scaling-activities --auto-scaling-group-name web-asg --max-items 5 \
--query 'Activities[].[StartTime,StatusCode,Cause]' --output table
- Step 1: select the Availability Zone with the most instances, to keep the group balanced across zones.
- Step 2: within that zone, prefer the instance with the oldest launch template or configuration, which phases out old versions.
- Step 3: if still tied, choose the instance closest to the next billing hour (relevant for per-hour billed usage); otherwise pick randomly.
- Override: set termination policies such as
OldestInstance,NewestInstanceorOldestLaunchTemplateif you need different behavior, and combine with scale-in protection.
Verify and operate
Read the activity history and tune from evidence.
- Activity log:
aws autoscaling describe-scaling-activities --auto-scaling-group-name web-asg --max-items 10shows cause strings such as an alarm breach and the capacity change. - Flapping: frequent out-then-in cycles suggest a target too tight, warmup too short or a spiky metric; widen the target or use a request-count metric.
- Pick the right metric: CPU suits compute-bound apps;
ALBRequestCountPerTargetsuits web tiers; use SQS backlog per instance (custom metric) for workers. - Limits: if the group sits at MaxSize or capacity is unavailable, scale out silently stalls; alert on that condition.
- Capacity rebalancing and Spot: with mixed instance groups, Spot interruptions and AZ rebalancing launch and terminate instances independently of scaling policies, so do not read every activity as a policy decision.
- Auto Scaling changes desired capacity when CloudWatch alarms driven by scaling policies fire, bounded by min and max size.
- Scale out reacts quickly; scale in waits for the metric to stay well below target and, with several target tracking policies, requires all to agree.
- I verify with describe-scaling-activities, tune warmup and metrics, and protect instances or use lifecycle hooks to drain safely.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Expecting scale in to mirror scale out thresholds Scale in is intentionally conservative with a longer window and margin to avoid flapping.
- Setting instance warmup too short Booting instances report low metrics and cause over-provisioning or premature scale in.
- Using CPU for an I/O-bound or queue-driven worker fleet The metric does not reflect load, so the group scales at the wrong times.
- Ignoring MaxSize and quota ceilings The group cannot scale out past them, and the failure is easy to miss without an alarm.