Search code examples
amazon-web-servicesterraformamazon-route53amazon-elbterraform-provider-aws

Fetch zone_id of hosted domain on route53 using terraform


Hi I am creating route53 record with terraform, I already have a hosted domain (public) lets say example.com how to fetch its zone_id and attach to record. How can I fetch the zone_id of existing route53 hosted zone. I have written a file but what it does it creates another hosted zone example.com rather then fetching existing example.com

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

resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.selected.zone_id 
  name    = "dev.${data.aws_route53_zone.selected.name}"
  type    = "A"
  alias {
    name                   = var.alb_dns
    zone_id                = var.zone_id
    evaluate_target_health = false
  }
}

Solution

  • You need to add private_zone = false too

    data "aws_route53_zone" "selected" {
      name         = "test.com."
    private_zone = false
    }
    
    resource "aws_route53_record" "www" {
      zone_id = data.aws_route53_zone.selected.zone_id 
      name    = "dev.${data.aws_route53_zone.selected.name}"
      type    = "A"
      alias {
        name                   = var.alb_dns
        zone_id                = var.zone_id
        evaluate_target_health = false
      }
    }