terraform route53 simplest example to create a dns record in hosted zone pointing to public IP of ec2 instance

Viewed 8289

I am trying to create a simplest example where in I can

  • create a DNS A record pointing to the public IP of an EC2 instance created in default VPC.

I have the following code for the same

hostedzone.tf

resource "aws_route53_zone" "devopslink-public-zone" {
  name = var.domain_mydevops_link
  comment = "${var.domain_mydevops_link} public zone"
  provider = aws
}

instance.tf

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}

route53-record.tf

resource "aws_route53_record" "server1-record" {
  zone_id = aws_route53_zone.devopslink-public-zone.zone_id
  name    = "server1.devops.link"
  type    = "A"
  ttl     = "300"
  records = [aws_instance.web.public_ip]
}
  • Terraform apply successfully give me this
Outputs:

devopslink-name-servers = [
  "ns-1363.awsdns-42.org",
  "ns-1707.awsdns-21.co.uk",
  "ns-466.awsdns-58.com",
  "ns-941.awsdns-53.net",
]
devopslink-public-zone-id = Z06137733F7SHPRF6K9Q3

And I can see the route53 record getting created in AWS console with the public IP of the instance on route53 console

server1.devops.link A   Simple  -   35.174.12.184

However when i do nslookup online or by using my PC its not resolving

 ✗ nslookup server1.devops.link
Server:         127.0.0.53
Address:        127.0.0.53#53

** server can't find server1.devops.link: NXDOMAIN

What am i doing wrong here? I do not own the domain devops.link. My assumption is that it should be taken care by the terraform code above.

2 Answers

Basically for this task we need the following

  • A domain like mydomain.link which you own. When we say own, it means the domain is registered with the domain registrar. When you do so you can either choose their (domain registrar's) default name servers or your custom name servers created in AWS (or any other cloud provider) in a hosted zone.
  • an EC2 instance with public IP in default VPC
  • A hosted zone
  • route53 dns record resolving to the public IP of our instance

With all the resources given in the question, we have created an EC2 instance, a hosted zone and a route53 record. So now we need to add the fqdn's of the nameservers generated by the terraform code in our domain-registrar's settings so that it uses our nameservers to resolve the record sets.

Link to Example - task link

A public hosted zone is one part of the solution for hosting your DNS configuration, it will hold your DNS records. From what I can see your public hosted zone and record are correct.

The second part involves you not being able to resolve the record, as you do not own the domain there is no mapping to the public hosted zone which prevents anyone resolving the domain to your DNS records.

You need to own the domain to have it resolve to the nameserver, once you own this you would set its name servers values equal to the name server records in the public hosted zone.

As of now no official support for the Route 53 Domains service exists in Terraform, this s the service to purchase and configure the domain.

Related