Bootstrap FreeKB - Amazon Web Services (AWS) - Register Targets in a Target Group using Terraform
Amazon Web Services (AWS) - Register Targets in a Target Group using Terraform


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

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)

This assumes you have setup Terraform with the Amazon Web Services (AWS) provider. If not, check out my article Amazon Web Services (AWS) Getting Started with Terraform.

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── application_load_balancers (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf
│   ├── listener.tf
│   ├── load_balancer.tf
│   ├── register_targets.tf
│   ├── remote_state.tf
│   ├── target_group.tf
├── ec2_instances (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf
├── security_groups (directory)
│   ├── 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"
}

 

You will be setting up the Application Load Balancer to forward requests onto targets (typically EC2 instances) thus the subnets being used by the Application Load Balancer and Targets (EC2 instances) will need to be in the same Availability Zone. Let's say data.tf in the ec2_instances directory contains something like this.

]$ cat data.tf
data "aws_instance" "my-docker1-instance" {
  filter {
    name = "tag:Name"
    values = ["docker1"]
  }
}

data "aws_instance" "my-docker2-instance" {
  filter {
    name = "tag:Name"
    values = ["docker2"]
  }
}

 

And outputs.tf in the ec2_instances directory contains something like.

output "docker1-subnet-id" {
  value = data.aws_instance.my-docker1-instance
}

output "docker2-subnet-id" {
  value = data.aws_instance.my-docker2-instance
}

 

The terraform refresh command should return a dictionary that includes the ID of each EC2 instance.

my_docker1_instance = {
   "id" = "i-00011122233344455"
my_docker2_instance = {
   "id" = "i-11122233344455666"
}

 

Then remote_state.tf in the application_load_balancer directory could have the following so that the ec2_instances outputs can be used in the application_load_balancer directory.

data "terraform_remote_state" "ec2_instances" {
  backend = "local"
  config = {
    path = "/usr/local/terraform/aws/ec2_instances/terraform.tfstate"
  }
}

 

And load_balancer.tf in your application_load_balancers directory could have the following to register the EC2 instances in the Target Group.

port is important here. This is the port of a service running in the EC2 instance is listening on. For example:

  • port 80 is commonly used if an HTTP web server such as nginx is running in the EC2 instance
  • port 3306 is commonly used if a mySQL or MariaDB database is running in the EC2 instance
resource "aws_lb_target_group_attachment" "my-registered-target" {

  for_each = toset([data.terraform_remote_state.ec2_instances.outputs.my_docker1_instance.id, data.terraform_remote_state.ec2_instances.outputs.my_docker2_instance.id])

  target_group_arn = aws_lb_target_group.my-application-load-balancer-target-group.arn
  target_id        = each.key
  port             = 80
}

 

The terraform plan command can be used to see what Terraform will try to do.

terraform plan

 

And the terraform apply command can be used to create the Elastic Load balancer.

terraform apply

 




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 7752cc in the box below so that we can be sure you are a human.