An Auto Scaling group keeps a number of instances running: between a minimum and a maximum, at the desired capacity. One stops or fails: it launches another from its launch template.
aws autoscaling create-auto-scaling-group --auto-scaling-group-name web-asg --launch-template LaunchTemplateName=web-lt,Version='$Latest' --min-size 2 --max-size 4 --desired-capacity 2 --vpc-zone-identifier "$SUBNET_A,$SUBNET_B" --target-group-arns $TG --health-check-type ELB --health-check-grace-period 180 --tags Key=Name,Value=web,PropagateAtLaunch=true- It spreads instances over the zones of
--vpc-zone-identifier, and registers each one in--target-group-arns. Version='$Latest'launches the newest version; the quotes stop the shell reading$Latestas a variable.- A tag with
PropagateAtLaunch=truenames every instance.
Which checks count
EC2 checks (the default) only see whether the instance runs. ELB checks also replace an instance its target group calls unhealthy: a crashed web server.
The grace period is how long a new instance has before checks count. It is 0 unless set. If user data takes two minutes and the group uses ELB checks without a grace period, every new instance fails, is replaced, and the next fails too.
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names web-asg --query "AutoScalingGroups[0].Instances[].[InstanceId,AvailabilityZone,LifecycleState,HealthStatus]" --output tableWhy it did what it did
Every launch and termination is a scaling activity, with a cause:
aws autoscaling describe-scaling-activities --auto-scaling-group-name web-asg --max-items 4 --query "Activities[].[Description,Cause]" --output textEach activity prints what happened, then why: Terminating EC2 instance: i-0… with ...in response to an ELB system health check failure.
aws autoscaling update-auto-scaling-group --auto-scaling-group-name web-asg --health-check-grace-period 300Terminate one of its instances with aws ec2 terminate-instances --instance-ids $ONE and the group launches a replacement. To run more, raise the desired capacity:
aws autoscaling set-desired-capacity --auto-scaling-group-name web-asg --desired-capacity 3