Bootstrap FreeKB - Amazon Web Services (AWS) - Modify Route 53 DNS Records using Terraform
Amazon Web Services (AWS) - Modify Route 53 DNS Records using Terraform

Updated:   |  Amazon Web Services (AWS) articles

You will typically have Route 53 Records that are used to:

  • Map a DNS name to an Elastic IP address
  • Map a DNS name to the Elastic IP address being used by a Network Load Balancer

So first, you need:

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

├── required_providers.tf
├── route53 (directory)
│   ├── provider.tf
│   ├── records.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"
}

 

And zones.tf could have the following.

data "aws_route53_zone" "example_com_zone" {
  name = "example.com."
}

 

And records.tf could have the following, to create or modify the Route 53 Records. In this example, an A record is created or modified to map www.example.com to an Elastic IP address.

resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.example_com_zone.zone_id
  name    = "www.example.com"
  type    = "A"
  ttl     = 300
  records = [aws_eip.eip.public_ip]
}

 

In this example, a CNAME record.

resource "aws_route53_record" "www-dev" {
  zone_id = data.aws_route53_zone.example_com_zone.zone_id
  name    = "www"
  type    = "CNAME"
  ttl     = 5

  weighted_routing_policy {
    weight = 10
  }

  set_identifier = "dev"
  records        = ["dev.example.com"]
}

 

Often, you'll be using Route 53 in conjunction with other AWS resources, such as Elastic IPs and Load Balancers.

├── required_providers.tf
├── elastic_ips (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf
├── elastic_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

 

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"
  }
}

 

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
}

 

Or to create or modify an Alias Record to map foo.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    = "foo.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 a195ad in the box below so that we can be sure you are a human.