Amazon EC2 Auto Scaling keeps the right number of EC2 instances running. It adds instances when demand goes up, removes them when demand drops, and replaces any instance that fails its health check. Together with the load balancer from the last post, it gives you an app that scales and heals itself.
What it does
- Keeps a group of instances between a minimum and a maximum size.
- Scales out when load rises and scales in when it falls.
- Replaces unhealthy instances automatically.
- Spreads instances across Availability Zones.
Key concepts
| Piece | What it is |
|---|---|
| Launch template | The recipe for an instance: AMI, type, security group, user data |
| Auto Scaling group (ASG) | The group of instances that Auto Scaling manages |
| Minimum, desired, maximum | The lowest, current and highest number of instances |
| Scaling policy | The rule that changes the desired number |
| Target tracking | A policy that keeps a metric, such as average CPU, near a target |
| Health check | EC2 status checks, or load balancer health checks, that decide if an instance is replaced |
| Cooldown and warm-up | Time to wait so the group does not react too fast |
Good to know
- Use a launch template because it is the current way to define instances, and it supports versions.
- Target tracking is the simplest policy: for example, keep average CPU at 50 percent.
- Turn on ELB health checks so an instance that fails the load balancer's check is also replaced.
- Attach the group to a target group so new instances join the load balancer automatically.
- Use scheduled scaling when you know a busy time in advance.
- Set the minimum high enough to survive the loss of one instance or Availability Zone.
- Use an instance refresh to roll out a new launch template version.
Hands-on: try it in your own account
- Create a launch template with a small instance type, an Amazon Linux AMI, your web server security group, and user data that installs a simple web server.
- Open EC2 > Auto Scaling Groups and choose Create Auto Scaling group. Select the launch template.
- Choose subnets in two Availability Zones.
- Attach the group to your load balancer's target group from the last post, and turn on ELB health checks.
- Set minimum 1, desired 2, maximum 3.
- Add a target tracking policy that keeps average CPU at 50 percent.
- Wait for two healthy instances, then terminate one by hand and watch the group start a replacement.
- Optional: raise the CPU on an instance for a few minutes and watch the group scale out.
- Clean up: delete the Auto Scaling group (this terminates its instances), then the load balancer, target group and launch template.
Common mistakes
- Setting minimum, desired and maximum all to the same number, so nothing can scale.
- Using only EC2 health checks, so an instance that fails the app check is never replaced.
- Putting every instance in one Availability Zone.
- A launch template with bad user data, so new instances never become healthy.
- Forgetting to delete the group, the load balancer and the instances after the practice.
Key takeaways
- Auto Scaling matches the number of instances to demand.
- Use a launch template, target tracking and ELB health checks.
- Use at least two Availability Zones and a sensible minimum.
- Delete the practice group, load balancer and instances when you are done.
Tomorrow I'll start a new AWS topic.
Part of #120DaysOfAWS.
Top comments (0)