Resource: aws_lb_target_group

Provides a Target Group resource for use with Load Balancer resources.

Example Usage

Instance Target Group

resource "aws_lb_target_group" "test" {
  name     = "tf-example-lb-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main.id
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

IP Target Group

resource "aws_lb_target_group" "ip-example" {
  name        = "tf-example-lb-tg"
  port        = 80
  protocol    = "HTTP"
  target_type = "ip"
  vpc_id      = aws_vpc.main.id
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

Lambda Target Group

resource "aws_lb_target_group" "lambda-example" {
  name        = "tf-example-lb-tg"
  target_type = "lambda"
}

ALB Target Group

resource "aws_lb_target_group" "alb-example" {
  name        = "tf-example-lb-alb-tg"
  target_type = "alb"
  port        = 80
  protocol    = "TCP"
  vpc_id      = aws_vpc.main.id
}

Target group with unhealthy connection termination disabled

resource "aws_lb_target_group" "tcp-example" {
  name     = "tf-example-lb-nlb-tg"
  port     = 25
  protocol = "TCP"
  vpc_id   = aws_vpc.main.id

  target_health_state {
    enable_unhealthy_connection_termination = false
  }
}

Argument Reference

This resource supports the following arguments:

health_check

stickiness

target_failover

target_health_state

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Target Groups using their ARN. For example:

import {
  to = aws_lb_target_group.app_front_end
  id = "arn:aws:elasticloadbalancing:us-west-2:187416307283:targetgroup/app-front-end/20cfe21448b66314"
}

Using terraform import, import Target Groups using their ARN. For example:

% terraform import aws_lb_target_group.app_front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:targetgroup/app-front-end/20cfe21448b66314