Ned
Ned Hello, I'm Nedim, a Cloud Engineer who enjoys writing about technology, particularly focusing on Linux and DevOps. Recently, I've been delving into topics like digital marketing, online presence, and startup culture.

Stand-alone subdomains on AWS DNS with Terraform

Stand-alone subdomains on AWS DNS with Terraform

Often I find myself in a situation where I need to create a root DNS zone which is a subdomain. This can be the case when the actual root zone is hosted on a different account. It is a common scenario when it is a requirement to host production and staging environments in different AWS accounts. Therefore we create the staging subdomain in the staging account and create a record in the production account that points to the staging subdomain’s Name Servers.

The Staging Account

AWS DNS

Creating the Subdomain in Route53

The first step is to create the staging subdomain in Terraform,

resource "aws_route53_zone" "staging_my_domain_org" {
  name     = "staging.my-domain.org"
  tags     = {}
  tags_all = {}
}

Output the Staging Name Servers

Following Terraform code will output the Name Servers of the newly created subdomain

output "staging_my_domain_org_nameservers" {
  value = aws_route53_zone.staging_my_domain_org.name_servers
}

Get the Staging Name Servers

Run Terraform and apply the changes. After the records have been created the Name Servers of the newly created subdomain will be output on the screen. This is an example below.

1
2
3
4
5
6
staging_my_domain_org_nameservers = tolist([
  "ns-1177.awsdns-11.org",
  "ns-18652.awsdns-40.co.uk",
  "ns-3610.awsdns-45.com",
  "ns-9538.awsdns-53.net",
])

The Production Account

Now that we have the Name Servers we need to point the subdomain to them but this time on the production account.

Create the NS Record

We are gonna create a staging.my-domain.org NS record under the my-domain.org zone. We will do this using the web console as in my case that zone is not managed by Terraform.

AWS DNS

Click Create records and you are done.

Conclusion

This is a valuable techie tip that served me well many times. I hope it will help my fellow techies with AWS. Feel free to come back for more.

Rating: