【问题标题】:How can I create a Route 53 Record to an ALB? (AWS)如何创建到 ALB 的 Route 53 记录? (AWS)
【发布时间】:2018-08-01 19:48:45
【问题描述】:

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

我知道我有 DNS 名称:${aws_lb.MYALB.dns_name}

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

【问题讨论】:

    标签: amazon-web-services dns terraform amazon-route53 aws-application-load-balancer


    【解决方案1】:

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

    或者,如果您使用的是“顶级”域(例如 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
      }
    }
    

    【讨论】:

    • 值得指出的是,无论如何,ALIAS A 记录比 CNAME 更好,因为它节省了一次 DNS 查找并且也是免费的。
    • 很好的解释,谢谢。你的 Apex 场景是我的赢家。
    【解决方案2】:

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

    所以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) 中的以下代码适用于 AAAAAapex 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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 1970-01-01
      • 2014-11-11
      • 2016-06-11
      • 2018-03-14
      • 1970-01-01
      • 2023-01-19
      • 2014-12-23
      • 2021-04-12
      相关资源
      最近更新 更多