Bootstrap FreeKB - Amazon Web Services (AWS) - List Elastic Load Balancer Target Groups using Terraform
Amazon Web Services (AWS) - List Elastic Load Balancer Target Groups using Terraform


An Elastic Load Balancer (ELB) is typically used to load balance requests across two (or more) different EC2 instances. 

 

This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

There are a few different types of load balancers.

  • Application Load Balancers (e.g. you have a web app that you want to load balance)
  • Network Load Balancers (e.g. you have SQL databases that you want to load balance)
  • Gateway Load Balancers
  • Classic Load Balancers (deprecated)

When creating an Application Load Balancer, you will need a Target Group. A Target Group is:

  • One or more EC2 instances using the EC2 instances ID
  • One or more EC2 instances via IP address
  • One or more EC2 instances via Lambda
  • One or more Application Load Balancers (in other words, an group of Application Load Balancers)

Before creating the target group, you will need to get the Virtual Private Cloud (VPC) ID that the Target Group will reside in. In this example, the aws ec2 describe-vpcs command is used to list the Virtual Private Clouds (VPC).

This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform. Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── target_groups (directory, child module)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

data.tf could have something like this.

data "aws_lb_target_group" "my-target-group" {
  name = "my-target-group"
}

 

outputs.tf could then have the following.

output "my_target_group" {
  value = data.aws_lb_target_group.my-target-group
}

 

The terraform refresh command should then output something like this.

my-target-group = {
  "arn" = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/cbcc3e9dace0fa63"
  "arn_suffix" = "targetgroup/my-target-group/cbcc3e9dace0fa63"
  "connection_termination" = tobool(null)
  "deregistration_delay" = 300
  "health_check" = tolist([
    {
      "enabled" = true
      "healthy_threshold" = 5
      "interval" = 30
      "matcher" = "200"
      "path" = "/"
      "port" = "traffic-port"
      "protocol" = "HTTP"
      "timeout" = 5
      "unhealthy_threshold" = 2
    },
  ])
  "id" = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/cbcc3e9dace0fa63"
  "lambda_multi_value_headers_enabled" = tobool(null)
  "load_balancing_algorithm_type" = "round_robin"
  "name" = "my-target-group"
  "port" = 80
  "preserve_client_ip" = tostring(null)
  "protocol" = "HTTP"
  "protocol_version" = "HTTP1"
  "proxy_protocol_v2" = tobool(null)
  "slow_start" = 0
  "stickiness" = tolist([
    {
      "cookie_duration" = 86400
      "cookie_name" = ""
      "enabled" = false
      "type" = "lb_cookie"
    },
  ])
  "tags" = tomap({})
  "target_type" = "instance"
  "timeouts" = null /* object */
  "vpc_id" = "vpc-0123456fa33abcdef"
}

 

Oten, you'll want just the Amazon Resource Number (ARN). And outputs.tf in the same directory as the main root module (main.tf) could have something like this.

output "my-target-group-arn" {
  value = module.target_groups.my_target_group.id
}

 

The terraform refresh command should then output something like this.

my-target-group-arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/cbcc3e9dace0fa63"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 66a229 in the box below so that we can be sure you are a human.