Sub topic 17
Scaling out: Auto Scaling groups and load balancers
This chapter assumes you can launch an instance, you understand security groups and subnets, and you know the basics of CloudWatch metrics. It introduces the idea of scaling out. It does not cover tuning.
Scaling up versus scaling out
Scaling up means moving to a larger instance type. It is simple, but it needs a stop and start, and one instance is still a single point of failure. Scaling out means running several identical instances and sharing the work between them. If one fails, the others keep serving requests.
Two services make this work:
- An Auto Scaling group (ASG) keeps a chosen number of instances running and replaces unhealthy ones.
- An Application Load Balancer (ALB) accepts traffic at one DNS name and spreads it across healthy instances.
Launch templates
An ASG does not copy a running instance. It launches new ones from a launch template. A launch template stores the settings you would pass to run-instances: AMI, instance type, key pair, security groups, instance profile, user data, and metadata options. Because new instances start from the template, they must configure themselves. Use user data or a prebuilt AMI, and keep no state on the instance disk.
How the pieces fit
The ASG spans subnets in several Availability Zones. It registers each new instance with a target group, and the ALB sends requests to the targets in that group.
Creating a group
This command creates a group that keeps between 2 and 4 instances running and registers them with an existing target group. The IDs are examples.
aws ec2 create-launch-template \
--launch-template-name web-template \
--launch-template-data '{"ImageId":"ami-0123456789abcdef0","InstanceType":"t3.micro","SecurityGroupIds":["sg-0123456789abcdef0"]}'
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--launch-template LaunchTemplateName=web-template,Version='$Latest' \
--min-size 2 --max-size 4 --desired-capacity 2 \
--vpc-zone-identifier "subnet-0123456789abcdef0,subnet-0fedcba9876543210" \
--target-group-arns arn:aws:elasticloadbalancing:us-east-1:111122223333:targetgroup/web-tg/0123456789abcdef \
--health-check-type ELB \
--health-check-grace-period 120Setting --health-check-type ELB lets the ASG replace an instance when the load balancer's health check fails, not only when EC2 reports it as down. The grace period gives a new instance time to boot before health checks count.
Scaling policies
The group size moves between the minimum and maximum. A target tracking policy is the simplest way to do this. You pick a metric and a target, such as 50 percent average CPU, and the ASG adds or removes instances to stay near it. Scheduled actions change capacity at set times. Both rely on the monitoring data covered earlier.
Common errors
- The security group on the instances blocks traffic from the load balancer, so every target shows as unhealthy. Allow the port from the load balancer's security group.
- The grace period is shorter than the boot time, so the ASG terminates instances that are still starting, and it repeats this in a loop.
- Subnets are all in one Availability Zone, which removes the failure protection.
- Data is stored on instance disks. When the ASG terminates an instance, that data is lost.
Exercise
Why does an ASG use a launch template instead of copying an existing instance, and what does that mean for how you set up each instance?
Show answer
A launch template stores the launch settings, so the ASG can create identical new instances at any time. Each instance must configure itself at boot through user data or a prebuilt AMI, and it must not hold state that only exists on its own disk, because the ASG can terminate it at any time.
Exercise
Instances in your group keep being terminated shortly after launch, even though the application works when you start it by hand. Name one likely cause and the setting to change.
Show answer
The health check grace period is probably shorter than the time the instance needs to boot and start the application. Increase --health-check-grace-period so health checks do not count until the application is ready.