Desired capacity and self-healing
An Auto Scaling group (ASG) maintains capacity between minimum and maximum, aiming at desired capacity. If an instance terminates or fails the configured health check, the group launches a replacement to restore desired capacity. With a target group attached, new instances register automatically.
flowchart LR LT["Launch template v3<br/>AMI, type, SG"] --> G["ASG web-asg<br/>min 2, desired 2, max 6"] G --> A["Instance<br/>AZ 1a"] G --> B["Instance<br/>AZ 1b"] A --> TG["Target group<br/>auto-registered"] B --> TG B -. "fails health check" .-> R["Terminated<br/>and replaced"] %% step LT: The launch template says what each new instance looks like. %% step G: The group keeps the number of running instances at desired, never below min or above max. %% step A: Instances are spread evenly across the subnets and AZs you choose. %% step B: If one AZ has problems, the group rebalances when it can. %% step TG: New instances join the target group, so the load balancer starts sending them traffic. %% step R: An unhealthy instance is terminated and a fresh one launched from the template.
Launch templates
Launch Templates have versions and are the current choice; Launch Configurations are legacy. A template holds the AMI, instance type, key pair, Security Groups, IAM instance profile, user data, and tags. Change an AMI with a new template version and run an instance refresh (rolling replacement with a minimum healthy percentage) rather than editing running instances and expecting automatic rollout.
Health checks
- EC2 health check (default) — replaces instances whose EC2 status checks fail.
- ELB health check — also replaces instances the load balancer marks unhealthy, which catches a crashed application on a healthy VM. Enable it whenever the ASG serves a load balancer, with a grace period long enough for boot and app start.
Spreading across AZs and instance types
Subnets determine AZs and network paths; pick subnets in at least two AZs for HA. Private instances need NAT or VPC endpoints to download packages; avoid public IPs as a workaround for missing egress.
A mixed instances policy lets one group use several instance types and combine On-Demand with Spot, for example "first 2 On-Demand, the rest 70% Spot across c7g.large, c6g.large, m7g.large". More types and AZs mean fewer Spot interruptions.
Lifecycle hooks and warm pools
Lifecycle hooks pause an instance in Pending:Wait or Terminating:Wait so a script can finish setup or copy logs before it goes into or out of service. Warm pools keep pre-initialised, stopped instances ready so scale-out is faster for apps with slow startup.
The ASG itself is free; you pay for the instances it runs.
| Command | Purpose |
|---|---|
aws ec2 create-launch-template --launch-template-name web --launch-template-data file://lt.json |
Create a template |
aws autoscaling create-auto-scaling-group --auto-scaling-group-name web-asg --launch-template LaunchTemplateName=web,Version='$Latest' --min-size 2 --max-size 6 --desired-capacity 2 --vpc-zone-identifier "subnet-0a1b,subnet-0c2d" --target-group-arns ... |
Create an ASG |
aws autoscaling start-instance-refresh --auto-scaling-group-name web-asg |
Roll out a new template version |
aws autoscaling describe-scaling-activities --auto-scaling-group-name web-asg |
See why instances were launched or terminated |
Traps: updating the template and expecting running instances to change; using only EC2 health checks behind a load balancer; a grace period shorter than the app's start time.