09-Auto Scaling

Module tạo Auto Scaling Group và Launch Template cho API tier

Stage Modulemodules/09-autoscaling

Mục đích

Tạo ASG cho API tier với khả năng auto-scale và rolling refresh khi thay đổi Launch Template.

Resources được tạo

ResourceMô tả
aws_launch_template.apiLaunch Template cho API instances
aws_autoscaling_group.apiAuto Scaling Group cho API

Configuration

ASG Configuration
# terraform.tfvars
asg_min_size         = 2     # Minimum instances
asg_max_size         = 4     # Maximum instances
asg_desired_capacity = 2     # Desired count

# Rolling refresh settings
asg_refresh_min_healthy_percentage = 50    # Keep 50% healthy during refresh
asg_refresh_instance_warmup        = 300   # 5 min warmup

Thông tin

Đặt asg_min_size = 0 để không launch instances (chỉ tạo infra).

Rolling Refresh

Khi thay đổi Launch Template (user_data, AMI), ASG tự động thực hiện rolling refresh:

  1. Launch instance mới với template mới
  2. Chờ instance healthy (warmup period)
  3. Terminate instance cũ
  4. Lặp lại cho đến khi tất cả instance được refresh
Force Refresh
# Force immediate refresh
terraform apply -replace='module.autoscaling.aws_launch_template.api'