20

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

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

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

4

2 回答 2

36

请参阅Terraform Route53 记录文档

您可以使用以下内容添加基本 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]
}

或者,如果您使用的是“顶点”域(例如 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
  }
}
于 2018-02-22T11:49:19.373 回答
1

是的,如果您使用but not ,则可以创建CNAME公共 DNS 名称${aws_lb.MYALB.dns_name}或使用 aws_route53_record 资源。aws_lb.MYALB.dns_namedomain with a subdomainapex domain(naked domain, root domain)

所以下面的代码Terraform(v0.15.0)适用CNAMEdomain which has a subdomain. * CNAMEwithapex 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)适用于A甚至适用于.AAAAapex 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
  }
}
于 2021-04-22T16:50:58.207 回答