Bootstrap FreeKB - Amazon Web Services (AWS) - Route 53 to a Network Load Balancer using Terraform
Amazon Web Services (AWS) - Route 53 to a Network Load Balancer using Terraform

Updated:   |  Amazon Web Services (AWS) articles

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

Take for example the following design, where a Network Load Balancer will be routing requests to EC2 instances is different subnets / availability zones. In this scenario, the Network Load Balancer will need an Elastic IP for each subnet / availability zone.

 

A Route 53 Alias Record can be used to map a domain name to your Network Load Balancer.

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

├── required_providers.tf
├── elastic_ips (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf
├── network_load_balancers (directory)
│   ├── data.tf
│   ├── listener.tf
│   ├── load_balancer.tf
│   ├── outputs.tf
│   ├── provider.tf
│   ├── remote_states.tf
│   ├── target_group.tf
├── route53 (directory)
│   ├── provider.tf
│   ├── records.tf
│   ├── remote_states.tf
│   ├── zones.tf

 

required_providers.tf will almost always have this.

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

 

Let's say provider.tf in the network_load_balancer directory 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"
}

 

In outputs.tf in the network_load_balancer directory you could create the following outputs as the Network Load Balancer DNS Name and Zone ID will be needed when creating or updating the Route 53 Alias Record.

output "network_load_balancer_dns_name" {
  value = aws_lb.my-network-load-balancer.dns_name
}

output "network_load_balancer_zone_id" {
  value = aws_lb.my-network-load-balancer.zone_id
}

 

Then in the remote_states.tf file in the route53 directory, you are making the elastic_ips and network_load_balancers outputs available in the route53 directory. Check out my article get output variables from terraform.tfstate using terraform_remote_state for more details on this.

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

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

 

And records.tf could have the following to create or modify an Alias Record to map www.example.com to the DNS name of your Network Load Balancer.

resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.example_com_zone.zone_id
  name    = "www.example.com"
  type    = "A"

  alias {
    name                   = data.terraform_remote_state.network_load_balancer.outputs.network_load_balancer_dns_name
    zone_id                = data.terraform_remote_state.network_load_balancer.outputs.network_load_balancer_zone_id
    evaluate_target_health = true
  }
}

 

If you have not yet initialized the route53 directory, issue the terraform init command.

terraform init

 

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

terraform plan

 

And terraform apply can be used to create or update the Route 53 Record.

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