EC2 Auto Scaling basics
Topic: Cloud aws core
Summary
Use an Auto Scaling group (ASG) to maintain a desired number of instances; scale on demand or on a schedule. Attach to a load balancer target group for traffic distribution. Use this when you need high availability or when scaling instance count based on load or schedule.
Intent: How-to
Quick answer
- Create launch template (AMI, instance type, key, security group). Create ASG: min, max, desired capacity; attach launch template; select VPC and subnets (multiple AZs for HA). Optionally attach to target group so new instances get traffic.
- Scaling: add scaling policy (target tracking on CPU or request count, or step scaling on CloudWatch alarm). Scale-out adds instances; scale-in removes (respect cooldown and scale-in protection). Use instance refresh to roll out new launch template version.
- Health: ASG replaces instances that fail ELB health check or EC2 check. Set health check type to ELB so unhealthy instances are replaced. Protect scale-in for critical instances if needed.
Prerequisites
Steps
-
Launch template
Create launch template: choose AMI, instance type, key pair, security group, user data if needed. Use for ASG so every new instance is consistent.
-
Create ASG
Create ASG: min 1, max 10, desired 2 (example); attach launch template; select at least two subnets (two AZs). Attach to target group so LB sends traffic to new instances. Enable ELB health check.
-
Scaling policy
Add target tracking policy: e.g. target CPU 70%; or scale on request count per target. Scale-in: allow or disable; set cooldown. Test by triggering scale (increase load or lower desired to see scale-in).
-
Instance refresh
When updating launch template (new AMI or config), use instance refresh to roll out new instances gradually. Set min healthy percentage and instance warmup so traffic is not dropped.
Summary
Create a launch template and an ASG with min/max/desired; attach to a target group; add scaling policies. Use this for HA and to scale instance count with load.
Prerequisites
Steps
Step 1: Launch template
Create a launch template with the desired AMI, type, and config.
Step 2: Create ASG
Create ASG with capacity; attach launch template and target group; use multiple AZs and ELB health check.
Step 3: Scaling policy
Add target tracking or step scaling; configure scale-in and cooldown.
Step 4: Instance refresh
Use instance refresh to roll out launch template changes.
Verification
- ASG maintains desired capacity; scaling policies add/remove instances; new instances pass health check and receive traffic.
Troubleshooting
Instances not receiving traffic — Ensure ASG is attached to target group; wait for health check to pass. Scale-in too aggressive — Increase cooldown; use scale-in protection for critical instances.