How can I create a Route 53 Record to an ALB? (AWS)
I

3

33

I want to create a new alb and a route53 record that points to it.

I see I have the DNS name: ${aws_lb.MYALB.dns_name}

Is it possible to create a cname to the public DNS name with aws_route53_record resource?

Import answered 22/2, 2018 at 3:45 Comment(0)
H
60

See the Terraform Route53 Record docs

You can add a basic CNAME entry with the following:

resource "aws_route53_record" "cname_route53_record" {
  zone_id = aws_route53_zone.primary.zone_id # Replace with your zone ID
  name    = "www.example.com" # Replace with your subdomain, Note: not valid with "apex" domains, e.g. example.com
  type    = "CNAME"
  ttl     = "60"
  records = [aws_lb.MYALB.dns_name]
}

Or if you're are using an "apex" domain (e.g. example.com) consider using an Alias (AWS Alias Docs):

resource "aws_route53_record" "alias_route53_record" {
  zone_id = aws_route53_zone.primary.zone_id # Replace with your zone ID
  name    = "example.com" # Replace with your name/domain/subdomain
  type    = "A"

  alias {
    name                   = aws_lb.MYALB.dns_name
    zone_id                = aws_lb.MYALB.zone_id
    evaluate_target_health = true
  }
}
Harquebus answered 22/2, 2018 at 11:49 Comment(2)
It's probably worth pointing out that an ALIAS A record is a better idea than a CNAME anyway as it saves one more DNS lookup and is also free.Plains
Excellent explanation, thank you. Your Apex scenario was the winner for me.Hallock
T
5

Yes, it is possible to create CNAME to the public DNS name ${aws_lb.MYALB.dns_name} or aws_lb.MYALB.dns_name with aws_route53_record resource if you use the domain with a subdomain but not apex domain(naked domain, root domain).

So the code below in Terraform(v0.15.0) works properly for CNAME with the domain which has a subdomain. *CNAME with apex domain(naked domain, root domain) causes error.

resource "aws_route53_zone" "myZone" {
  name = "example.com"
}

resource "aws_route53_record" "myRecord" {
  zone_id = aws_route53_zone.myZone.zone_id
  name    = "www.example.com"
  type    = "CNAME"
  ttl     = 60
  records = [aws_lb.MYALB.dns_name]
}

In addition, the code below in Terraform(v0.15.0) works properly for A or AAAA with apex domain(naked domain, root domain) even for the domain with a subdomain.

resource "aws_route53_zone" "myZone" {
  name = "example.com"
}

resource "aws_route53_record" "myRecord" {
  zone_id = aws_route53_zone.myZone.zone_id
  name    = "example.com" # OR "www.example.com"
  type    = "A" # OR "AAAA"

  alias {
      name                   = aws_lb.MYALB.dns_name
      zone_id                = aws_lb.MYALB.zone_id
      evaluate_target_health = true
  }
}
Tallahassee answered 22/4, 2021 at 16:50 Comment(0)
P
0

To create a Route 53 record that points to an Application Load Balancer (ALB) using Terraform, you can follow these steps:

Ensure ALB is created: Make sure your ALB resource is defined in your Terraform configuration and already deployed. This typically involves setting up listeners and target groups.

Create Route 53 Record: Use Terraform's aws_route53_record resource to define the DNS record in your Route 53 hosted zone.

Here's a basic example of how you can define this in your Terraform configuration:

provider "aws" {
  region = "your_aws_region"
}

resource "aws_route53_record" "alb_record" {
  zone_id = "your_route53_zone_id"
  name    = "your.domain.com"
  type    = "A"

  alias {
    name                   = aws_lb.your_alb.dns_name
    zone_id                = aws_lb.your_alb.zone_id
    evaluate_target_health = true
  }
}

resource "aws_lb" "your_alb" {
  name               = "your-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.your_sg.id]
  subnets            = [aws_subnet.your_subnet.id, aws_subnet.your_other_subnet.id]

  enable_deletion_protection = false

  enable_http2 = true
  enable_cross_zone_load_balancing = true
}
Potaufeu answered 10/7 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.