You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Auto Scaling is one of EC2's most powerful features. An Auto Scaling Group (ASG) automatically adjusts the number of EC2 instances based on demand, ensuring you have the right amount of capacity at all times — no more, no less. This lesson covers ASG concepts, scaling policies, health checks, and best practices.
An Auto Scaling Group is a logical grouping of EC2 instances that share the same configuration and scaling rules. The ASG:
| Parameter | Description |
|---|---|
| Minimum size | The fewest instances the ASG will maintain (floor) |
| Maximum size | The most instances the ASG can scale to (ceiling) |
| Desired capacity | The number of instances the ASG currently targets |
| Launch template | The configuration used to launch new instances (AMI, instance type, key pair, security groups, user data) |
| Availability Zones | The AZs across which instances are distributed |
| Health check type | EC2 (instance status) or ELB (load balancer health check) |
| Health check grace period | Time to wait before checking a new instance's health (allows time for startup) |
The ASG constantly monitors its instances and takes action:
If current capacity < desired capacity → Launch new instances
If current capacity > desired capacity → Terminate excess instances
If an instance is unhealthy → Terminate and replace it
Instances are distributed as evenly as possible across the configured Availability Zones. If one AZ becomes unavailable, the ASG launches replacement instances in the remaining AZs.
A Launch Template defines the instance configuration:
aws ec2 create-launch-template \
--launch-template-name my-web-app \
--version-description "v1.0" \
--launch-template-data '{
"ImageId": "ami-0abcdef1234567890",
"InstanceType": "t3.medium",
"KeyName": "my-key",
"SecurityGroupIds": ["sg-0123456789abcdef0"],
"UserData": "'$(base64 -w0 <<'USERDATA'
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
USERDATA
)'"
}'
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name my-web-asg \
--launch-template LaunchTemplateName=my-web-app,Version='$Latest' \
--min-size 2 \
--max-size 10 \
--desired-capacity 2 \
--vpc-zone-identifier "subnet-0abc123,subnet-0def456" \
--health-check-type ELB \
--health-check-grace-period 300 \
--target-group-arns "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-tg/abc123"
Scaling policies determine when and how the ASG adjusts capacity.
The simplest and most commonly used policy. You define a target metric value, and the ASG automatically adjusts capacity to maintain it.
aws autoscaling put-scaling-policy \
--auto-scaling-group-name my-web-asg \
--policy-name cpu-target-tracking \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
},
"TargetValue": 50.0
}'
Common target metrics:
| Metric | Description |
|---|---|
| ASGAverageCPUUtilization | Average CPU across all instances |
| ASGAverageNetworkIn | Average inbound network bytes |
| ASGAverageNetworkOut | Average outbound network bytes |
| ALBRequestCountPerTarget | Average requests per target in a target group |
| Custom metric | Any CloudWatch metric you define |
Define different scaling actions for different alarm thresholds:
| CPU Range | Action |
|---|---|
| 70-80% | Add 1 instance |
| 80-90% | Add 2 instances |
| > 90% | Add 3 instances |
| < 30% | Remove 1 instance |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.