Amazon cloudformation 将AWS::Route53::RecordSet DnsRecord添加到无服务器Cloudfront发行版

Amazon cloudformation 将AWS::Route53::RecordSet DnsRecord添加到无服务器Cloudfront发行版,amazon-cloudformation,amazon-route53,serverless,aws-serverless,Amazon Cloudformation,Amazon Route53,Serverless,Aws Serverless,我了解了如何将route53 dns记录与serverless.yml文件中的S3 bucket关联 我已经尝试将其应用于部署cloudfront发行版的情况 DnsRecord: Type: "AWS::Route53::RecordSet" Properties: AliasTarget: DNSName: <cloudfrontdistribution id> HostedZoneId: Z21DNDUVLTQW6Q Hosted

我了解了如何将route53 dns记录与serverless.yml文件中的S3 bucket关联

我已经尝试将其应用于部署cloudfront发行版的情况

DnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName: <cloudfrontdistribution id>
      HostedZoneId: Z21DNDUVLTQW6Q
    HostedZoneName: ${self:custom.appFQDN}.
    Name:
      Ref: WebAppCloudFrontDistribution
    Type: 'CNAME'
DnsRecord:
类型:“AWS::Route53::RecordSet”
特性:
别名目标:
DNSName:
HostedZoneId:Z21DNDUVLTQW6Q
HostedZoneName:${self:custom.appFQDN}。
姓名:
参考:WebAppCloudFrontDistribution
类型:“CNAME”
但是我正在努力解决如何将分发id作为ref而不是固定字符串来获取的问题


我将如何执行此操作?

要设置别名目标,实际上只需为DNSName参数提供CloudFront DNS名称,而不是分发ID。您可以通过以下方式执行此操作:

!GetAtt WebAppCloudFrontDistribution.DomainName
我假设WebAppCloudFrontDistribution是模板中AWS::CloudFront::Distribution资源的逻辑ID,而不是参数。如果这实际上是一个参数,只需将该参数的值设置为CloudFront的AWS控制台仪表板中为发行版列出的DNS名称

您还需要在模板中解决其他一些问题:

  • HostedZoneName应该是Route53托管区域的名称,而不是要使用的FQDN。就我个人而言,我更喜欢为AWS::Route53::RecordSet资源使用HostedZoneId属性,因为它更清楚这个属性的含义,但每个属性都有自己的含义。(注意:AWS::Route53::RecordSet资源的HostedZoneId属性应该是托管区域的HostedZoneId,而不是与AliasTarget HostedZoneId相同的值。)
  • Name应该是您希望成为CloudFront分发资源的CNAME的DNS名称
  • 我知道这有点奇怪,但对于别名目标,必须将类型设置为“a”(IPv4)或“AAAA”(IPv6)。我建议您同时执行这两项操作—您可以通过创建AWS::Route53::RecordSet资源的副本来执行此操作,但请将类型设置为“AAAA”而不是“a”

最后,请注意,为了使其正常工作,您还需要确保添加FQDN作为CloudFront分发资源的备用名称-您可以使用模板中分发资源的“DistributionConfig”属性的“Alias”属性进行设置,或者,如果您没有在此模板中创建资源,请在AWS控制台中为分发设置手动配置此配置。

这是无服务器模板中我的工作配置的外观:

DnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName:
        Fn::GetAtt:
          - CloudFrontDistribution
          - DomainName
      # Looks like it is always the same for CloudFront distribs.
      # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html
      # https://docs.aws.amazon.com/general/latest/gr/rande.html#cf_region
      HostedZoneId: ${self:custom.zoneId}
    HostedZoneName: ${self:custom.secondLevelDomain}.
    Name: ${self:custom.appFQDN}
    Type: 'A'

还有Tom McLaughlin的一个例子:

我努力创建了一个
AWS::Route53::RecordSet
,CloudFormation生成了“资源未能创建”类型的不具体、没有帮助的错误消息。对我来说,关键是使用
HostedZoneId
而不是
HostedZoneName
来指定父级“托管区域”。这就是我的结局:

  NaaaaaComDNSEntry: 
    Type: 'AWS::Route53::RecordSet'
    DependsOn: NaaaaaComCloudFront
    Properties: 
      AliasTarget:
        DNSName: !GetAtt NaaaaaComCloudFront.DomainName
        # For CloudFront, HostedZoneId is always Z2FDTNDATAQYW2, see:
        # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
        HostedZoneId:  Z2FDTNDATAQYW2
    # HostedZoneId is for ID for 'naaaaa.com.'; In theory its valid to use `HostedZoneName` OR `HostedZoneId`
    # but in practice the recordset always failed to create if I used `HostedZoneName`
    HostedZoneId: ZABCDEFGHIJK5M
    Name: 'www.naaaaa.com.'
    Type: 'A'
  NaaaaaComDNSEntry: 
    Type: 'AWS::Route53::RecordSet'
    DependsOn: NaaaaaComCloudFront
    Properties: 
      AliasTarget:
        DNSName: !GetAtt NaaaaaComCloudFront.DomainName
        # For CloudFront, HostedZoneId is always Z2FDTNDATAQYW2, see:
        # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
        HostedZoneId:  Z2FDTNDATAQYW2
    # HostedZoneId is for ID for 'naaaaa.com.'; In theory its valid to use `HostedZoneName` OR `HostedZoneId`
    # but in practice the recordset always failed to create if I used `HostedZoneName`
    HostedZoneId: ZABCDEFGHIJK5M
    Name: 'www.naaaaa.com.'
    Type: 'A'