Sub topic 6
Target groups and registering targets
This chapter assumes you know EC2 instances, security groups, and how a load balancer listener forwards requests. You should also be able to run AWS CLI commands.
What a target group does
A target group is the set of destinations a load balancer sends traffic to. A listener rule forwards requests to a target group, and the target group chooses one of its healthy targets. The target group also holds the settings that apply to those targets: protocol, port, health check, and deregistration delay.
Target types
You choose the target type when you create the group. You cannot change it afterward.
| Target type | What you register | Typical use |
|---|---|---|
| instance | EC2 instance IDs | Instances, and Auto Scaling groups |
| ip | Private IP addresses | Containers, on-premises servers over VPN or Direct Connect |
| lambda | One Lambda function | Serverless backends behind an Application Load Balancer |
| alb | An Application Load Balancer | Network Load Balancer in front of an ALB |
With instance targets, traffic goes to the instance's primary network interface. With ip targets, each address can use a different port, which suits containers.
Create a target group and register targets
The following commands create a group and register two instances. The protocol and port are the defaults for targets. A target can override the port when you register it.
aws elbv2 create-target-group \
--name web-tg \
--protocol HTTP \
--port 80 \
--vpc-id vpc-0123456789abcdef0 \
--target-type instance \
--health-check-path /health
aws elbv2 register-targets \
--target-group-arn "$TG_ARN" \
--targets Id=i-0aaa1111bbbb2222c Id=i-0ddd3333eeee4444f,Port=8080Registration is manual here. When you attach an Auto Scaling group to the target group, the group registers and deregisters instances for you, so you do not call register-targets yourself.
Check target state
A newly registered target starts in the initial state while the load balancer runs health checks. It moves to healthy after enough passing checks. The other states you will see are unhealthy, draining, unused (the target is not in use, for example because its Availability Zone is not enabled for the load balancer), and unavailable.
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)When a state is unhealthy, the Reason and Description fields in the same response explain why, such as Target.Timeout or Target.ResponseCodeMismatch.
Common errors
- The target is in a different VPC. Instance targets must be in the same VPC as the target group.
- The target security group blocks the load balancer. Allow the port in the instance's security group from the load balancer's security group.
- The health check path returns a redirect or 404. The default success code is 200, so change the path or the matcher.
- A target stays in
unused. Enable the target's Availability Zone on the load balancer.
Exercise
You registered an instance with register-targets, and describe-target-health shows it as unhealthy with the reason Target.ResponseCodeMismatch. The instance is running and reachable. What is the most likely cause, and where do you fix it?
Show answer
The health check request reaches the instance, but the response code is not the one the target group expects (200 by default). The health check path probably returns a redirect or an error such as 404. Fix it by changing the health check path or the success code matcher in the target group's health check settings, or by making the application return 200 on that path.