☁️ Cloud

Auto Scaling

Auto Scaling

Automatically adjusts the number of instances according to load. Achieves cost optimization and high availability at the same time.

📖 Detailed Explanation

Auto Scaling is a technology that automatically scales computing resources out (Scale Out) or in (Scale In) according to real-time demand. When traffic increases, it adds instances, and when it decreases, it removes them, achieving cost optimization and high availability at the same time. It is the core mechanism that implements "elasticity," a key advantage of the cloud.

In AWS, EC2 Auto Scaling is the most representative. It was released together with EC2 in 2009 and has a history of more than 15 years. It has now expanded into various services such as ECS Service Auto Scaling, DynamoDB Auto Scaling, and Application Auto Scaling.

Auto Scaling has three core concepts. First, a Launch Template (or Launch Configuration) defines the specifications of new instances. Second, an Auto Scaling Group (ASG) manages the minimum/maximum/desired number of instances. Third, a Scaling Policy decides when to scale out/in. There are three policy types: Target Tracking, Step Scaling, and Simple Scaling.

In practice, Auto Scaling is essential for web services with unpredictable traffic patterns, batch processing systems, and event-driven workloads. A well-configured Auto Scaling minimizes costs under normal conditions while responding automatically to traffic spikes, ensuring service stability.

💻 Code Example

# 1. Launch Template 생성
aws ec2 create-launch-template \
  --launch-template-name my-app-template \
  --version-description "v1" \
  --launch-template-data '{
    "ImageId": "ami-0c55b159cbfafe1f0",
    "InstanceType": "t3.medium",
    "KeyName": "my-key",
    "SecurityGroupIds": ["sg-12345678"],
    "IamInstanceProfile": {
      "Arn": "arn:aws:iam::123456789:instance-profile/my-app-role"
    },
    "UserData": "IyEvYmluL2Jhc2gKY2QgL2hvbWUvZWMyLXVzZXIKLi9zdGFydC5zaA=="
  }'

# 2. Auto Scaling Group 생성
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name my-app-asg \
  --launch-template LaunchTemplateName=my-app-template,Version='$Latest' \
  --min-size 2 \
  --max-size 10 \
  --desired-capacity 2 \
  --vpc-zone-identifier "subnet-abc123,subnet-def456" \
  --target-group-arns "arn:aws:elasticloadbalancing:ap-northeast-2:123456789:targetgroup/my-tg/xxx" \
  --health-check-type ELB \
  --health-check-grace-period 300

# 3. Target Tracking Scaling Policy (CPU 70% 유지)
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name my-app-asg \
  --policy-name cpu-target-tracking \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "ASGAverageCPUUtilization"
    },
    "TargetValue": 70.0,
    "ScaleOutCooldown": 60,
    "ScaleInCooldown": 300
  }'

# 4. Scheduled Scaling (예: 평일 오전 9시 확장)
aws autoscaling put-scheduled-update-group-action \
  --auto-scaling-group-name my-app-asg \
  --scheduled-action-name scale-out-morning \
  --recurrence "0 0 * * 1-5" \
  --desired-capacity 5 \
  --min-size 3

# ASG 상태 확인
aws autoscaling describe-auto-scaling-groups \
  --auto-scaling-group-names my-app-asg \
  --query 'AutoScalingGroups[0].{Min:MinSize,Max:MaxSize,Desired:DesiredCapacity,Instances:Instances[*].InstanceId}'

🗣️ Say It Like This in Practice

💬 In a capacity planning meeting
"We need to review the Auto Scaling settings in preparation for Black Friday. The current max is 10, but last year's peak reached 8. Considering a margin, let's raise max to 20 and use Scheduled Scaling to raise min to 5 in advance on the morning of the day. If we also turn on Predictive Scaling, it learns the pattern from two days before and scales out ahead of time."
💬 In an Interview
"With EC2 Auto Scaling, I mainly used a Target Tracking Policy. I set the CPU target to 70% and gave the Scale-in cooldown a generous 300 seconds. I linked it with the ALB health check so that unhealthy instances are replaced automatically, and with a Mixed Instance Policy I mixed Spot and On-Demand at a 7:3 ratio, reducing costs by 40%."
💬 In a incident analysis meeting
"The scale-out was slow during yesterday's traffic spike because the Instance Warmup is 300 seconds. Provisioning a new instance takes 2 minutes and the app boot takes 1 minute, so it adds up to as much as 5 minutes. With the Warm Pool feature, you can keep instances waiting in a stopped state in advance."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring the Cooldown period

A "thrashing" phenomenon, where scale-out occurs again right after a scale-out, can occur. We recommend a Scale-out cooldown of 60 seconds and a Scale-in cooldown of 300 seconds or more. When a new instance starts receiving traffic, the metrics need time to stabilize.

❌
Health check configuration errors

The ALB health check and the Auto Scaling health check operate separately. If health-check-type is not set to ELB, the ASG will not know even when the ALB judges an instance unhealthy, and will keep sending traffic to it. The health-check-grace-period must also be set longer than the app startup time.

✅
The right approach

Don't look only at CPU; also monitor Application Load Balancer Request Count Per Target. Even when CPU is low, a request queue can build up. If you attach a Target Tracking Policy to each of the two metrics, scaling out occurs when either one exceeds its threshold.

🔗 Related Terms

📚 Learn More