Sub topic 12
Connecting an Auto Scaling group to a target group
This chapter assumes you know EC2 instances, security groups, subnets and Availability Zones. It also assumes you already have a launch template named web-lt, an Application Load Balancer, and a target group. The goal is to make the group register new instances with the target group automatically and replace instances the load balancer reports as unhealthy.
What the integration does
An Auto Scaling group (ASG) manages instance count. A target group receives traffic from the load balancer. When you attach the target group to the ASG, three things change:
- Each instance the ASG launches is registered with the target group automatically.
- Each instance the ASG terminates is deregistered first, so the deregistration delay can drain in-flight requests.
- If the health check type is
ELB, the ASG also uses the load balancer's health result to decide whether to replace an instance.
Create the group
This command creates a group of 2 to 6 instances across two subnets and attaches the target group.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--launch-template LaunchTemplateName=web-lt,Version='$Latest' \
--min-size 2 \
--max-size 6 \
--desired-capacity 2 \
--vpc-zone-identifier "subnet-aaaa1111,subnet-bbbb2222" \
--target-group-arns "$TG_ARN" \
--health-check-type ELB \
--health-check-grace-period 300Two settings matter most:
--health-check-type ELBmakes the ASG treat a target that fails the load balancer health check as unhealthy. The default,EC2, only checks whether the instance is running, so an instance with a crashed web server would stay in service.--health-check-grace-period 300gives a new instance 300 seconds before the ASG acts on its health results. Set it longer than your boot and application start time. If it is too short, the ASG can terminate instances that are still starting, which causes a launch and terminate loop.
To attach a target group to an existing group, use aws autoscaling attach-load-balancer-target-groups.
Check the result
Registration success does not mean the target is healthy. Use describe_target_health to see each target's state.
import boto3
elbv2 = boto3.client("elbv2")
def print_target_health(target_group_arn):
response = elbv2.describe_target_health(TargetGroupArn=target_group_arn)
for description in response["TargetHealthDescriptions"]:
target = description["Target"]["Id"]
state = description["TargetHealth"]["State"]
print(target, state)New targets normally pass through initial before reaching healthy. A target that stays unhealthy usually means the security group on the instance does not allow traffic from the load balancer, or the application does not return a success code on the health check path.
Common errors
| Symptom | Likely cause |
|---|---|
| Instances launch, then terminate repeatedly | Grace period shorter than application start time |
| Instance is running but gets no traffic | Instance security group blocks the load balancer |
| Broken instance is never replaced | Health check type is `EC2`, not `ELB` |
| Target group and ASG are in different VPCs | Target group VPC must match the ASG subnets |
Exercise
An ASG uses the default health check type. One instance's web server crashes, but the instance itself keeps running. The load balancer marks the target unhealthy. What happens, and what change fixes it?
Show answer
Nothing happens to the instance. The default EC2 health check only sees a running instance, so the ASG does not replace it. Setting the health check type to ELB makes the ASG replace the instance when the load balancer reports it unhealthy.