You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Serverless中引用CloudFront分发ID配置Route53 RecordSet?

How to reference CloudFront Distribution details for Route53 RecordSet in serverless.yml?

I've been working on setting up a Route53 DNS record to point to a CloudFront distribution using serverless.yml. I adapted some existing config for S3 buckets, but my current setup uses a hardcoded CloudFront ID for the DNSName field, and I want to replace that with a dynamic reference instead.

Here's my current (incomplete) config:

DnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName: <cloudfrontdistribution id>
      HostedZoneId: Z21DNDUVLTQW6Q
    HostedZoneName: ${self:custom.appFQDN}.
    Name: Ref: WebAppCloudFrontDistribution
    Type: 'CNAME'

How can I properly reference my CloudFront distribution's details (instead of using a fixed string) to populate the Route53 record correctly?


Great question! Let's fix this up—there are a couple of key adjustments needed to reference your CloudFront distribution properly and set up the Route53 record correctly:

1. Reference the CloudFront Domain Name (not the ID)

First, your DNSName in the AliasTarget shouldn't be the CloudFront distribution ID—it needs to be the distribution's actual domain name (e.g., d123abcdefghij.cloudfront.net). To get this dynamically, use !GetAtt instead of Ref on your CloudFront resource.

2. Use the Correct Record Type

When using Route53's AliasTarget, you can't use a CNAME record—you need to use either A (for IPv4) or AAAA (for IPv6) records. Alias records are designed to work with these types, not CNAMEs.

3. Full Working Config Example

Assuming you've already defined your CloudFront distribution in your serverless.yml with the logical ID WebAppCloudFrontDistribution, here's the corrected Route53 record config:

resources:
  Resources:
    # Your existing CloudFront distribution definition
    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        # Your CloudFront config (origins, cache behaviors, etc.)

    # The corrected Route53 RecordSet
    WebAppDnsRecord:
      Type: "AWS::Route53::RecordSet"
      Properties:
        AliasTarget:
          # Get the CloudFront distribution's domain name dynamically
          DNSName: !GetAtt WebAppCloudFrontDistribution.DomainName
          # CloudFront's global HostedZoneId is always Z21DNDUVLTQW6Q
          HostedZoneId: Z21DNDUVLTQW6Q
        # Reference your hosted zone (ensure the trailing dot is included)
        HostedZoneName: "${self:custom.appFQDN}."
        # The domain name you want to point to CloudFront
        Name: "${self:custom.appFQDN}"
        # Use A record for IPv4 (or AAAA for IPv6)
        Type: 'A'

Key Notes:

  • !GetAtt WebAppCloudFrontDistribution.DomainName will pull the auto-generated domain name of your CloudFront distribution, no hardcoding needed.
  • The HostedZoneId for CloudFront is always Z21DNDUVLTQW6Q—this is a global value, it doesn't change based on your AWS region.
  • Make sure your HostedZoneName includes the trailing dot (.) at the end, as required by Route53.

内容的提问来源于stack exchange,提问作者bebbi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:47:03