通过CloudFormation将多个存储桶连接到CloudFront

问题描述

我已经创建了一个Yaml文档来创建一个S3存储桶,并将其附加到发行版上。



Resources:

  S3BucketContent:
    DeletionPolicy: 'Delete'
    Metadata:
      Comment: 'Bucket to store Content'
    Properties:
      AccessControl: 'Private'
      BucketName: !Sub '${AWS::StackName}-content-bucket'
    Type: 'AWS::S3::Bucket'

  S3BucketPolicy:
    Metadata:
      Comment: 'Bucket policy to allow cloudfront to access the data'
    Properties:
      Bucket: !Ref S3BucketContent
      PolicyDocument:
        Statement:
          - Action:
              - 's3:Getobject'
            Effect: 'Allow'
            Principal:
              CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
            Resource:
              - !Sub 'arn:aws:s3:::${S3BucketContent}/*'
    Type: 'AWS::S3::BucketPolicy'

  Cfdistribution:
    Metadata:
      Comment: 'A simple CloudFront distribution with an S3 origin'
    Properties:
      distributionConfig:
        Comment: 'A simple distribution with an S3 origin'
        DefaultCacheBehavior:
          AllowedMethods:
            - 'HEAD'
            - 'GET'
          CachedMethods:
            - 'HEAD'
            - 'GET'
          Compress: false
          DefaultTTL: 86400
          ForwardedValues:
            Cookies:
              Forward: 'none'
            Headers:
              - 'Origin'
            QueryString: false
          MaxTTL: 31536000
          MinTTL: 86400
          TargetoriginId: !Sub 's3-origin-${S3BucketContent}'
          TrustedSigners:
            - !Ref AWS::AccountId
          ViewerProtocolPolicy: 'allow-all'
        DefaultRootObject: 'index.html'
        Enabled: true
        HttpVersion: 'http1.1'
        IPV6Enabled: false
        Origins:
          - DomainName: !GetAtt S3BucketContent.RegionalDomainName # NOTE: you may want to replace this with !GetAtt S3Bucket.DomainName (the RegionalDomainName is just to get around the initial DNS propagation issue),more details here: https://stackoverflow.com/questions/38735306/aws-cloudfront-redirecting-to-s3-bucket
            Id: !Sub 's3-origin-${S3BucketContent}'
            OriginPath: ''
            S3OriginConfig:
              OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
        PriceClass: 'PriceClass_All'
    Type: 'AWS::CloudFront::distribution'

  CfOriginAccessIdentity:
    Metadata:
      Comment: 'Access S3 bucket content only through CloudFront'
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: 'Access S3 bucket content only through CloudFront'
    Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'

Outputs:
  S3BucketName:
    Description: 'Bucket name'
    Value: !Ref S3BucketContent
  CfdistributionId:
    Description: 'Id for our cloudfront distribution'
    Value: !Ref Cfdistribution
  CfdistributionDomainName:
    Description: 'Domain name for our cloudfront distribution'
    Value: !GetAtt Cfdistribution.DomainName

一旦POC工作,我想创建多个可以添加到同一Cloudfront发行版的存储桶,但是我收到一条错误消息Template format error: Unresolved resource dependencies [S3BucketDWC] in the Resources block of the template,但未指定确切的错误。这是2个存储桶的更新Yaml。有人可以让我知道该怎么做吗?



Resources:

  S3BucketContent:
    DeletionPolicy: 'Delete'
    Metadata:
      Comment: 'Bucket to store  Content'
    Properties:
      AccessControl: 'Private'
      BucketName: !Sub '${AWS::StackName}-content-bucket'
    Type: 'AWS::S3::Bucket'

  S3BucketPolicy:
    Metadata:
      Comment: 'Bucket policy to allow cloudfront to access the data'
    Properties:
      Bucket: !Ref S3BucketContent
      PolicyDocument:
        Statement:
          - Action:
              - 's3:Getobject'
            Effect: 'Allow'
            Principal:
              CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
            Resource:
              - !Sub 'arn:aws:s3:::${S3BucketContent}/*'
    Type: 'AWS::S3::BucketPolicy'
    
    S3BucketDWC:
    DeletionPolicy: 'Delete'
    Metadata:
      Comment: 'Bucket to store dwc'
    Properties:
      AccessControl: 'Private'
      BucketName: !Sub '${AWS::StackName}-dwc-bucket'
    Type: 'AWS::S3::Bucket'

  S3BucketPolicy:
    Metadata:
      Comment: 'Bucket policy to allow cloudfront to access the data'
    Properties:
      Bucket: !Ref S3BucketDWC
      PolicyDocument:
        Statement:
          - Action:
              - 's3:Getobject'
            Effect: 'Allow'
            Principal:
              CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
            Resource:
              - !Sub 'arn:aws:s3:::${S3BucketDWC}/*'
    Type: 'AWS::S3::BucketPolicy'

  Cfdistribution:
    Metadata:
      Comment: 'A simple CloudFront distribution with an S3 origin'
    Properties:
      distributionConfig:
        Comment: 'A simple distribution with an S3 origin'
        DefaultCacheBehavior:
          AllowedMethods:
            - 'HEAD'
            - 'GET'
          CachedMethods:
            - 'HEAD'
            - 'GET'
          Compress: false
          DefaultTTL: 86400
          ForwardedValues:
            Cookies:
              Forward: 'none'
            Headers:
              - 'Origin'
            QueryString: false
          MaxTTL: 31536000
          MinTTL: 86400
          TargetoriginId: !Sub 's3-origin-${S3BucketContent}'
          TrustedSigners:
            - !Ref AWS::AccountId
          ViewerProtocolPolicy: 'allow-all'
        DefaultRootObject: 'index.html'
        Enabled: true
        HttpVersion: 'http1.1'
        IPV6Enabled: false
        Origins:
          - DomainName: !GetAtt S3BucketContent.RegionalDomainName 
            Id: !Sub 's3-origin-${S3BucketContent}'
            OriginPath: ''
            S3OriginConfig:
              OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
              
            DomainName: !GetAtt S3BucketContent.RegionalDomainName 
            Id: !Sub 's3-origin-${S3BucketDWC}'
            OriginPath: ''
            S3OriginConfig:
              OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
              
        PriceClass: 'PriceClass_All'
    Type: 'AWS::CloudFront::distribution'

  CfOriginAccessIdentity:
    Metadata:
      Comment: 'Access S3 bucket content only through CloudFront'
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: 'Access S3 bucket content only through CloudFront'
    Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'

Outputs:
  S3BucketName:
    Description: 'Bucket name'
    Value: !Ref S3BucketContent
  CfdistributionId:
    Description: 'Id for our cloudfront distribution'
    Value: !Ref Cfdistribution
  CfdistributionDomainName:
    Description: 'Domain name for our cloudfront distribution'
    Value: !GetAtt Cfdistribution.DomainName

解决方法

至少一个明显的问题是您使用的是缩进错误

    S3BucketDWC:
    DeletionPolicy: 'Delete'
    Metadata:
      Comment: 'Bucket to store dwc'
    Properties:
      AccessControl: 'Private'
      BucketName: !Sub '${AWS::StackName}-dwc-bucket'
    Type: 'AWS::S3::Bucket'

这应该是:

  S3BucketDWC:
    DeletionPolicy: 'Delete'
    Metadata:
      Comment: 'Bucket to store dwc'
    Properties:
      AccessControl: 'Private'
      BucketName: !Sub '${AWS::StackName}-dwc-bucket'
    Type: 'AWS::S3::Bucket'

第二个问题是Origins应该是一个列表(您丢失的-):

        Origins:
          - DomainName: !GetAtt S3BucketContent.RegionalDomainName 
            Id: !Sub 's3-origin-${S3BucketContent}'
            OriginPath: ''
            S3OriginConfig:
              OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
              
          - DomainName: !GetAtt S3BucketContent.RegionalDomainName 
            Id: !Sub 's3-origin-${S3BucketDWC}'
            OriginPath: ''
            S3OriginConfig:
              OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'