Auto Scaling

Auto Scaling automatically adds or removes EC2 instances so capacity matches demand and failed instances are replaced.

EC2 Auto Scaling keeps the right number of instances running for your load. It adds instances when traffic rises, removes them when it falls, and replaces any instance that fails a health check.

The pieces

  • Launch template — describes how to build each instance (AMI, type, user data).
  • Auto Scaling group (ASG) — the pool, with a minimum, desired and maximum size, spread across Availability Zones.
  • Scaling policy — the rule, e.g. keep average CPU at 50%.

Paired with an Elastic Load Balancer, an ASG gives you a self-healing, elastic fleet.

Users reach an Elastic Load Balancer that spreads traffic across an Auto Scaling group of EC2 instancesUsersLoadBalancerAuto Scaling groupEC2 #1EC2 #2EC2 #3
A load balancer distributes traffic across instances managed by an Auto Scaling group.

Example

Example · bash
# Create an Auto Scaling group from a launch template
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name web-asg \
  --launch-template LaunchTemplateName=web-template,Version='$Latest' \
  --min-size 2 --max-size 6 --desired-capacity 2 \
  --vpc-zone-identifier "subnet-0a1b2c3d,subnet-0e5f6a7b"

# Keep average CPU near 50%
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name web-asg \
  --policy-name cpu50 --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{"PredefinedMetricSpecification":{"PredefinedMetricType":"ASGAverageCPUUtilization"},"TargetValue":50.0}'

When to use it

  • An e-commerce site's Auto Scaling group adds EC2 instances automatically when CPU exceeds 70% during a flash sale, then scales back down when traffic drops.
  • A batch processing system uses scheduled scaling to add capacity at 2 AM when nightly jobs run and remove it by 6 AM.
  • A gaming platform configures Auto Scaling with a minimum of 2 and maximum of 50 instances so it never goes offline but never over-spends.

More examples

Create a Launch Template

Creates a launch template that defines the AMI, instance type, and security group for Auto Scaling instances.

Example · bash
aws ec2 create-launch-template \
  --launch-template-name web-lt \
  --version-description v1 \
  --launch-template-data '{
    "ImageId": "ami-0abcdef1234567890",
    "InstanceType": "t3.small",
    "SecurityGroupIds": ["sg-0abc123"]
  }'

Create an Auto Scaling Group

Creates an Auto Scaling group across two subnets with a minimum of 2 and maximum of 10 instances.

Example · bash
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name web-asg \
  --launch-template LaunchTemplateName=web-lt,Version=1 \
  --min-size 2 \
  --max-size 10 \
  --desired-capacity 2 \
  --vpc-zone-identifier "subnet-0abc,subnet-0def"

Add CPU-Based Scaling Policy

Attaches a target tracking policy that keeps average CPU at 70% by automatically adding or removing instances.

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

Discussion

  • Be the first to comment on this lesson.