Skip to content

The route53.tf File

Filename Location Group Project/Repository
route53.tf ./route53.tf infrastructure terraform

Why?

Without DNS records users will have to access our application via the AWS ALB's DNS hostname or its IP (which might change.) This isn't ideal as neither option is very easy to remember or use.

Instead we'll create some DNS records inside of an existing Route53 Zone so that we can redirect traffic to the correct AWS ALB, making it easier for our users to use our application.

Breakdown

Because we're using a pre-existing Route53 Zone the first thing we need to do is fetch the ID of it. This is the first time we're using a data {} block in our code:

1
2
3
data "aws_route53_zone" "httpcats" {
  name = "httpcats.net"
}

Using the name attribute we're able to search for a particular DNS zone. We could find the zone ID manually in the AWS UI and hard code it, but this is a better option in the long term.

Now we create some DNS records against this zone ID:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "www"

  alias {
    name                   = aws_alb.httpcats.dns_name
    zone_id                = aws_alb.httpcats.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "ssh-1" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "ssh-1"
  ttl     = "300"

  records = [
    aws_eip.meow_1.public_ip
  ]
}

resource "aws_route53_record" "ssh-2" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "ssh-2"
  ttl     = "300"

  records = [
    aws_eip.meow_2.public_ip
  ]
}

We create three records: one for our ALB, a two additional records that point directly at our EC2 Instance's EIPs.

The Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
data "aws_route53_zone" "httpcats" {
  name = "httpcats.net"
}

resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "www"

  alias {
    name                   = aws_alb.httpcats.dns_name
    zone_id                = aws_alb.httpcats.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "ssh-1" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "ssh-1"
  ttl     = "300"

  records = [
    aws_eip.meow_1.public_ip
  ]
}

resource "aws_route53_record" "ssh-2" {
  zone_id = data.aws_route53_zone.httpcats.zone_id
  type    = "A"
  name    = "ssh-2"
  ttl     = "300"

  records = [
    aws_eip.meow_2.public_ip
  ]
}

Terraform Documentation

Type Documentation
aws_route53_zone (data) Terraform AWS Provider
aws_route53_record Terraform AWS Provider

Committing the Code

  1. Set your working directory to the infrastructure/terraform repository
  2. Save the file as route53.tf and use git add route53.tf to add it to the Git staging area
  3. Use git commit -am 'setting up DNS record for our application' to commit the file to our repository
  4. Push the code to GitLab.com: git push

Last update: August 25, 2021