如何为alb创建route53记录?

时间:2018-02-22 03:45:52

标签: terraform

我想创建一个新的alb和指向它的route53记录。

我看到我有DNS名称:${aws_lb.MYALB.dns_name}

是否可以使用aws_route53_record资源为公有DNS名称创建cname?

2 个答案:

答案 0 :(得分:8)

请参阅Terraform Route53 Record docs

您可以添加以下基本CNAME条目:

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}"]
}

或者如果你正在使用" apex"域(例如example.com)考虑使用别名(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
  }
}

答案 1 :(得分:0)

是的,如果您使用 CNAME 而不是 ${aws_lb.MYALB.dns_name},则可以使用 aws_route53_record 资源为公共 DNS 名称 aws_lb.MYALB.dns_namedomain with a subdomain 创建 apex domain(naked domain, root domain) .

因此 Terraform(v0.15.0) 中的以下代码适用于带有 CNAMEdomain which has a subdomain。 *CNAMEapex domain(naked domain, root domain) 会导致错误。

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]
}

此外,Terraform(v0.15.0) 中的以下代码适用于 AAAAA,即使对于 apex domain(naked domain, root domain) 也适用于 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
  }
}