CloudFormation/Serverless で AWS::S3::RecordSet を作成するときに CloudFront ドメイン名を参照するにはどうすればよいですか?

okwaves2024-01-25  395

バケットからデータを提供するためのクラウドフロント ディストリビューションを持つプロジェクトがあります。私はサーバーレス フレームワークを使用していますが、これは主に CloudFormation に関する質問だと思います。

Route53 でホストされているドメイン (重要な場合は第 3 レベルのドメイン) に A レコードを作成したいと考えています。つまり、dashboard.domain.com は Route53 を指しており、.dashboard.domain.com を追加しようとしています) .

CloudFront リソースからの出力を参照する方法がわかりません。

これは私が現在持っているもので、すべて静的であるため機能します。ただし、別のリソースによって作成される正しいクラウド フロント ドメインを自動的に入力する必要があります。これらは実行できる GetAttr の一種だと思いますが、機能させることができません。

    DNSRecords: 
      Type: AWS::Route53::RecordSetGroup
      Properties:
        HostedZoneId: Z09193931V4YGJEPVMLG1
        RecordSets:
          - Name: prod.dashboard.domain.com
            Type: A
            AliasTarget:
              HostedZoneId: Z2FDTNDATAQYW2
              DNSName: someid.cloudfront.net

    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        DistributionConfig:
          Origins:
            - DomainName:
                Fn::Join: [
                  "", [
                    { "Ref": "WebAppS3Bucket" },
                    ".s3.amazonaws.com"
                  ]
                ]
              ## An identifier for the origin which must be unique within the distribution
              Id: WebApp
              CustomOriginConfig:
                HTTPPort: 80
                HTTPSPort: 443
                OriginProtocolPolicy: https-only
          Enabled: 'true'
          ## Uncomment the following section in case you are using a custom domain
          Aliases:
            - ${self:provider.stage}.dashboard.domain.com
          DefaultRootObject: index.html
          ## Since the Single Page App is taking care of the routing we need to make sure ever path is served with index.html
          ## The only exception are files that actually exist e.h. app.js, reset.css
          CustomErrorResponses:
            - ErrorCode: 404
              ResponseCode: 200
              ResponsePagePath: /index.html
          DefaultCacheBehavior:
            AllowedMethods:
              - DELETE
              - GET
              - HEAD
              - OPTIONS
              - PATCH
              - POST
              - PUT
            ## The origin id defined above
            TargetOriginId: WebApp
            ## Defining if and how the QueryString and Cookies are forwarded to the origin which in this case is S3
            ForwardedValues:
              QueryString: 'false'
              Cookies:
                Forward: none
            ## The protocol that users can use to access the files in the origin. To allow HTTP use `allow-all`
            ViewerProtocolPolicy: redirect-to-https
          ## The certificate to use when viewers use HTTPS to request objects.
          ViewerCertificate:
            AcmCertificateArn:
              Ref: SSLCertificate
            SslSupportMethod: sni-only
            MinimumProtocolVersion: TLSv1

編集: に更新されましたWebAppCloudFrontDistribution を含める



------------------------

AWS::CloudFront::Distribution リソース定義が提供されていないため、例に基づいて説明することしかできません。

MyCloudFrontDistro:
  Type: AWS::CloudFront::Distribution
  Properties:
    # some properties

その後、DNSRecord を変更できます

    DNSRecords: 
      Type: AWS::Route53::RecordSetGroup
      Properties:
        HostedZoneId: Z09193931V4YGJEPVMLG1
        RecordSets:
          - Name: prod.dashboard.domain.com
            Type: A
            AliasTarget:
              HostedZoneId: !Ref MyCloudFrontDistro
              DNSName: !GetAtt MyCloudFrontDistro.DomainName

0



------------------------

    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        DistributionConfig:
          Origins:
            - DomainName:
                Fn::Join: [
                  "", [
                    { "Ref": "WebAppS3Bucket" },
                    ".s3.amazonaws.com"
                  ]
                ]
              ## An identifier for the origin which must be unique within the distribution
              Id: WebApp
              CustomOriginConfig:
                HTTPPort: 80
                HTTPSPort: 443
                OriginProtocolPolicy: https-only
          Enabled: 'true'
          Aliases:
            - ${self:provider.stage}.dashboard.domain.com
          DefaultRootObject: index.html
          CustomErrorResponses:
            - ErrorCode: 404
              ResponseCode: 200
              ResponsePagePath: /index.html
          DefaultCacheBehavior:
            AllowedMethods:
              - DELETE
              - GET
              - HEAD
              - OPTIONS
              - PATCH
              - POST
              - PUT
            TargetOriginId: WebApp
            ForwardedValues:
              QueryString: 'false'
              Cookies:
                Forward: none
            ## The protocol that users can use to access the files in the origin. To allow HTTP use `allow-all`
            ViewerProtocolPolicy: redirect-to-https
          ## The certificate to use when viewers use HTTPS to request objects.
          ViewerCertificate:
            AcmCertificateArn:
              Ref: SSLCertificate
            SslSupportMethod: sni-only
            MinimumProtocolVersion: TLSv1
            
          ## Uncomment the following section in case you want to enable logging for CloudFront requests
          # Logging:
          #   IncludeCookies: 'false'
          #   Bucket: mylogs.s3.amazonaws.com
          #   Prefix: myprefix

Resources:
    DNSRecords: 
      Type: AWS::Route53::RecordSetGroup
      Properties:
        HostedZoneName: dashboard.domain.com.
        RecordSets:
          - Name: ${self:provider.stage}.dashboard.domain.com
            Type: A
            AliasTarget:
              HostedZoneId: Z2FDTNDATAQYW2
              DNSName: !GetAtt WebAppCloudFrontDistribution.DomainName

これが私にとって有効な解決策です。いくつかの点に注意してください。

Z2FDTNDATAQYW2 の HostedZoneId は、クラウドフロント ドメインにとって特別なものです。以下の場合に使用する必要があります。クラウド フロント リソースを参照します。 HostedZoneName には末尾のスペースを含める必要があります (HostedZoneId と比較して使用する場合)。私の場合、クラウド形成の前にドメインをセットアップしました。

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。