0
0
AWScloud~5 mins

Target groups concept in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple servers to handle web traffic, you need a way to send users to the right server. Target groups help by grouping servers so a load balancer can send traffic to them evenly and keep your app working smoothly.
When you want to spread user requests across several servers to avoid overload on one.
When you need to check if servers are healthy before sending them traffic.
When you want to organize servers by the type of traffic they handle, like web or API requests.
When you want to add or remove servers without stopping your app.
When you want to use a load balancer to manage traffic for your app.
Config File - target-group.json
target-group.json
{
  "Name": "my-target-group",
  "Protocol": "HTTP",
  "Port": 80,
  "VpcId": "vpc-0abc123def456ghij",
  "HealthCheckProtocol": "HTTP",
  "HealthCheckPort": "traffic-port",
  "HealthCheckPath": "/health",
  "Matcher": {
    "HttpCode": "200"
  },
  "TargetType": "instance"
}

This JSON file defines a target group named my-target-group. It listens on HTTP port 80 inside a specific VPC. The health check uses HTTP requests to the /health path to make sure servers are working. Only servers responding with HTTP 200 are considered healthy. The target type is set to instance, meaning it sends traffic to EC2 instances.

Commands
This command creates a target group named 'my-target-group' that listens on HTTP port 80 in the specified VPC. It sets up health checks to verify server health by requesting the /health path and expecting a 200 response.
Terminal
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --vpc-id vpc-0abc123def456ghij --health-check-protocol HTTP --health-check-port traffic-port --health-check-path /health --matcher HttpCode=200 --target-type instance
Expected OutputExpected
{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/abcdef1234567890", "TargetGroupName": "my-target-group", "Protocol": "HTTP", "Port": 80, "VpcId": "vpc-0abc123def456ghij", "HealthCheckProtocol": "HTTP", "HealthCheckPort": "traffic-port", "HealthCheckPath": "/health", "Matcher": { "HttpCode": "200" }, "TargetType": "instance" } ] }
--name - Sets the name of the target group.
--vpc-id - Specifies the VPC where the target group exists.
--health-check-path - Defines the URL path used for health checks.
This command checks the details of the target group named 'my-target-group' to confirm it was created correctly.
Terminal
aws elbv2 describe-target-groups --names my-target-group
Expected OutputExpected
{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/abcdef1234567890", "TargetGroupName": "my-target-group", "Protocol": "HTTP", "Port": 80, "VpcId": "vpc-0abc123def456ghij", "HealthCheckProtocol": "HTTP", "HealthCheckPort": "traffic-port", "HealthCheckPath": "/health", "Matcher": { "HttpCode": "200" }, "TargetType": "instance" } ] }
--names - Specifies the target group name to describe.
This command adds two EC2 instances to the target group so they can receive traffic from the load balancer.
Terminal
aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/abcdef1234567890 --targets Id=i-0123456789abcdef0 Id=i-0fedcba9876543210
Expected OutputExpected
{}
--target-group-arn - Specifies the target group to add instances to.
--targets - Lists the instance IDs to register.
This command checks the health status of the instances registered in the target group to ensure they are ready to receive traffic.
Terminal
aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/abcdef1234567890
Expected OutputExpected
{ "TargetHealthDescriptions": [ { "Target": { "Id": "i-0123456789abcdef0" }, "TargetHealth": { "State": "healthy" } }, { "Target": { "Id": "i-0fedcba9876543210" }, "TargetHealth": { "State": "healthy" } } ] }
--target-group-arn - Specifies the target group to check health for.
Key Concept

If you remember nothing else from this pattern, remember: target groups organize servers so load balancers can send traffic only to healthy servers.

Common Mistakes
Not specifying the correct VPC ID when creating the target group.
The target group won't work because it must belong to the same VPC as the instances.
Always use the VPC ID where your instances run when creating the target group.
Forgetting to register instances to the target group after creating it.
No servers will receive traffic because the target group is empty.
Use the register-targets command to add your instances to the target group.
Ignoring health check settings or using incorrect health check paths.
The load balancer may mark healthy instances as unhealthy and stop sending traffic to them.
Set the health check path to a URL that returns HTTP 200 when the server is healthy.
Summary
Create a target group to group your servers and define how to check their health.
Register your servers (instances) to the target group so they can receive traffic.
Check the health of your servers in the target group to ensure smooth traffic flow.