Terraform AWS Target Group Resource Configuration and Traffic Routing

Target groups are a powerful way to manage traffic to your AWS applications. They allow you to route traffic to different instances based on criteria such as the client’s IP address, the request’s port, or the request’s protocol. This can be useful for load balancing, fault tolerance, and more. The article will show how to create a target group using Terraform. The basics of target groups are covered, and the steps of creating a target group in Terraform are walked through. Tips on how to use target groups effectively are provided. By the end of this article, a solid understanding of how to use target groups in Terraform is achieved. Target groups can be created to meet specific needs, and they can be used to improve the performance and reliability of AWS applications.

A target group is a logical grouping of EC2 instances that you can use to distribute traffic across multiple instances or to send traffic to a specific instance. A Terraform AWS target group is a resource that defines a collection of EC2 instances that can receive traffic from an Application Load Balancer. Target groups can be used to distribute traffic evenly across multiple EC2 instances, or to route traffic to specific instances based on criteria such as the instance’s health status or the request’s source IP address. Target groups are used with load balancers to ensure that traffic is distributed evenly across instances and to ensure that healthy instances are used to serve traffic. Target groups can be configured with different health checks to ensure that only healthy instances are used to serve traffic. Target groups can also be configured with different weights to control how traffic is distributed across instances. Target groups are a powerful tool for managing EC2 instances and for ensuring that applications are available and reliable.

Target groups are the bridge between your load balancer and your backend resources. They define where traffic goes, how health is checked, and how connections are managed. Whether running EC2 instances, containers in ECS, Lambda functions, or IP-based targets, target groups are needed to tie everything together. This guide covers the common target group types and configurations available in Terraform.

Core Properties of a Terraform AWS Target Group

The name of the target group, the port on which the target group listens, and the protocol for which the target group is configured are fundamental properties.

Name Definition

The name of the target group is a required identifier.

targetgroupname Description Example
The name of the target group. resource “awslbtarget_group” “example” { name = “my-target-group”}

The name determines how the target group is referenced within Terraform state and in the AWS console. The name impacts the real-world consequence for the user because a consistent naming scheme enables identification of the target group across multiple environments and prevents accidental modification of production resources. The name connects to the resource block aws_lb_target_group which is the Terraform resource used to create a target group for an HTTP load balancer.

Port Configuration

The port on which the target group listens defines the listening endpoint.

| port | The port on which the target group listens. | resource “awslbtarget_group” “example” { port = 80} |

The port selection determines which traffic the load balancer forwards to the backend. Choosing port 80 for HTTP means the target group will accept unencrypted HTTP traffic from the load balancer. The port works in conjunction with protocol to establish the communication channel between the load balancer and the instances.

Protocol Configuration

The protocol for which the target group is configured controls the traffic type.

| protocol | The protocol for which the target group is configured. | resource “awslbtarget_group” “example” { protocol = “HTTP”} |

Protocol selection determines whether the load balancer terminates HTTP, HTTPS, TCP, or TLS. The protocol choice impacts health check behavior and listener configuration. The protocol is also used by the listener resource that references the target group.

Target Group Types in Terraform

Application Load Balancers support three target types, each suited to different architectures.

  • instance - Routes to EC2 instances by instance ID. The ALB uses the instance's primary private IP.
  • ip - Routes to specific private IP addresses. Works with containers, cross-VPC targets, or on-premises resources via Direct Connect or Site-to-Site VPN.
  • lambda - Routes to Lambda functions. Only supported with ALBs.

Instance Target Group is the most common type. A target group is a logical grouping of resources that can be used to distribute traffic across multiple instances. Target groups can be used with load balancers to distribute traffic evenly across multiple instances.

Creating a Target Group with Terraform

To create a Terraform AWS target group, the following steps can be used.

  • Create a Terraform configuration file that defines the target group’s properties.
  • Initialize Terraform and apply the configuration.

Here is an example of a Terraform configuration file for a target group:

resource “aws_lb_target_group” “my-target-group” { name = “my-target-group” port = 80 protocol = “HTTP” health_check_protocol = “HTTP” health_check_port = 80 health_check_path = “/” healthy_threshold = 2 unhealthy_threshold = 3 timeout = 5 }

The example creates a target group named my-target-group that is configured for HTTP traffic and uses the HTTP protocol for health checks. The target group will be associated with all instances in the default VPC that have the port 80 open.

An alternative example with explicit targettype and healthcheck block is:

resource “aws_lb_target_group” “my-target-group” { name = “my-target-group” port = 80 protocol = “HTTP” target_type = “instance” health_check { protocol = “HTTP” port = 80 path = “/” } }

To create a target group in Terraform, the aws_lb_target_group resource can be used. The following example creates a target group for an HTTP load balancer.

The aws_lb_target_group resource requires specification of the name of the target group, the protocol that the target group will use, the port that the target group will use, and the health check settings for the target group.

Health Check Settings

Health check settings determine whether instances are considered healthy and eligible for traffic.

The health check protocol, health check port, health check path, healthy threshold, unhealthy threshold, and timeout are configurable.

A configuration example includes:

resource “aws_lb_target_group” “my-target-group” { name = “my-target-group” port = 80 protocol = “HTTP” health_check_protocol = “HTTP” health_check_port = 80 health_check_path = “/” healthy_threshold = 2 unhealthy_threshold = 3 timeout = 5 }

Healthy threshold of 2 means the instance must pass two consecutive health checks to be marked healthy. Unhealthy threshold of 3 means the instance must fail three consecutive checks to be marked unhealthy. Timeout of 5 seconds defines the maximum time to wait for a health check response. The health check path of / is the endpoint queried by the load balancer.

Target groups can be configured with different health checks to ensure that only healthy instances are used to serve traffic.

Attaching Resources to a Target Group

Adding EC2 instances to a Terraform AWS target group requires updating the Terraform configuration file to include the IDs of the EC2 instances that you want to add.

The steps are:

  • Update the Terraform configuration file to include the IDs of the EC2 instances that you want to add.
  • Initialize Terraform and apply the configuration.

To remove instances, the configuration file is updated to remove the IDs of the EC2 instances that you want to remove.

An example of how to update the Terraform configuration file to remove an EC2 instance from a target group is:

resource “aws_lb_target_group” “my-target-group” { name = “my-target-group” port = 80 protocol = “HTTP” health_check_protocol = “HTTP” health_check_port = 80 health_check_path = “/” healthy_threshold = 2 unhealthy_threshold = 3 timeout = 5 targets { id = “${aws_instance.my-instance.id}” } }

The instance resource example is:

resource “aws_instance” “my-instance” { instance_type = “t2.micro” ami = “ami-0123456789abcdef0” subnet_id = “subnet-0123456789abcdef0” security_group_ids = [“sg-0123456789abcdef0”] }

The aws_lb_target_group_attachment resource can also be used to attach instances to a target group.

Listener Integration

The following example creates a listener for an HTTP load balancer that uses the target group created in the previous example:

resource “aws_lb_listener” “my-listener” { load_balancer_arn = aws_lb.my-load-balancer.arn port = 80 protocol = “HTTP” default

The listener references the load balancer ARN and forwards traffic on port 80 using HTTP protocol to the default target group. The listener and target group together form the complete request path from client to backend.

Troubleshooting and Management

Target groups can be used to distribute traffic evenly across multiple instances.

Troubleshooting target groups can be performed using:

  • The AWS Management Console
  • The AWS CLI
  • The AWS API

Key takeaways include:

  • A target group is a logical grouping of resources that can be used to distribute traffic across multiple instances.
  • Target groups can be used with load balancers to distribute traffic evenly across multiple instances.
  • Terraform can be used to create target groups in AWS.
  • To create a target group using Terraform, you need to specify the following information:
    • The name of the target group
    • The protocol that the target group will use
    • The port that the target group will use
    • The health check settings for the target group

We covered the basics of target groups, including what they are and how they work. We then showed how to create a target group using Terraform, and we provided some tips for troubleshooting. The blog post has been helpful. Questions can be left in the comments below.

Conclusion

The Terraform AWS target group resource provides a declarative way to define how traffic is routed from an Application Load Balancer to backend EC2 instances, IP addresses, or Lambda functions. The name, port, protocol, and health check attributes form the core of the resource definition. The targettype attribute controls whether routing is performed by instance ID, IP address, or Lambda invocation. Health check settings such as healthcheckprotocol, healthcheckport, healthcheckpath, healthythreshold, unhealthy_threshold, and timeout determine instance eligibility for traffic. Attachment via aws_lb_target_group_attachment or inline targets allows incremental management of backend resources. Listener resources reference the target group ARN to complete the load balancing configuration. Target groups can be created using Terraform by defining properties, initializing Terraform, and applying the configuration. This enables consistent, repeatable provisioning of load balanced architectures with fault tolerance and even distribution.

Sources

  1. Source Name
  2. Source Name

Related Posts