Bootstrap FreeKB - Amazon Web Services (AWS) - List DNS Route 53 Hosted Zones using Terraform
Amazon Web Services (AWS) - List DNS Route 53 Hosted Zones using Terraform


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

Route 53 is Amazon Web Services DNS. For example, Route 53 can be used to create an A record to associate a hostname such as www.example.com to a public IP address.

In Route 53, a Hosted Zone contains the DNS records for your domain, such as example.com. You can have different Hosted Zones, such as one for example.com and another for sample.com.

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

├── required_providers.tf
├── route53 (directory)
│   ├── provider.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 has the following.

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

 

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

 

If the Hosted Zone exists, something like this should be returned.

data.aws_route53_zone.example_com_zone: Reading...
data.aws_route53_zone.example_com_zone: Read complete after 1s [id=123456789ABCDEFG]

 




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