【问题标题】:Cloudformation template - S3 bucket website with cloudfront distribution - distribution can't access originCloudformation 模板 - 具有云端分发的 S3 存储桶网站 - 分发无法访问源
【发布时间】:2023-01-24 00:35:45
【问题描述】:

我只是想在 S3 存储桶上获取一个静态站点,并且只能通过 CloudFront 分发访问它,但是缺少了一些东西,我无法弄清楚是什么。

目前我的堆栈有

  • 用于站点托管的 S3 存储桶
  • 为站点提供服务的云端分布
  • 只允许分配访问存储桶的存储桶策略
  • 分配的默认缓存策略

当尝试直接从存储桶网站 url 访问该网站时,我收到 403(禁止访问,拒绝访问),没关系。
当尝试从分发域访问它时,我得到一个带有消息 Failed to contact the origin. 的一般错误页面
当尝试从我的注册域访问它时,我收到一个 403 错误页面,其中包含消息 The request could not be satisfied.,然后是关于如何修复它的通用提示(稍后登录、联系网站所有者、检查文档等)

因为我使用的是来自我的 cli 的 CloudFormation 模板,所以每个资源都在同一个区域,其他一切对我来说也都是正确的,但显然有些地方不对劲。

CloudFormation 模板

Resources:

  BucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    DependsOn:
      - AppBucket
      - CloudFrontDistribution
    Properties:
      Bucket: !Ref AppBucket
      PolicyDocument:
        Id: MyPolicy
        Version: '2012-10-17'
        Statement:
          - Sid: PolicyForCloudFrontPrivateContent
            Action: s3:GetObject
            Effect: Allow
            Principal:
              Service: cloudfront.amazonaws.com
            Condition:
              StringLike:
                aws:Referer: !Sub 'https://*.${CloudFrontDistribution}.cloudfront.net/*'
            Resource: !Sub arn:aws:s3:::${AppBucket}/*

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    DependsOn:
      - AppBucket
      - DefaultCachePolicy
    Properties:
      DistributionConfig:
        Enabled: true
        Origins:
          - Id: AppBucket
            DomainName: !GetAtt AppBucket.DomainName
            OriginPath: /*
            S3OriginConfig: {}
        DefaultCacheBehavior:
          ViewerProtocolPolicy: redirect-to-https
          TargetOriginId: AppBucket
          CachePolicyId: !Ref DefaultCachePolicy

  AppBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: 'test-spa-stack-bucket-app'
      PublicAccessBlockConfiguration:
        BlockPublicAcls : false
        BlockPublicPolicy : false
        IgnorePublicAcls : false
        RestrictPublicBuckets : false
      VersioningConfiguration:
        Status: Enabled
      BucketEncryption:
        ServerSideEncryptionConfiguration:
        - ServerSideEncryptionByDefault:
            SSEAlgorithm: 'AES256'
      WebsiteConfiguration:
        IndexDocument: index.html
              
  DefaultCachePolicy:
    Type: AWS::CloudFront::CachePolicy
    Properties: 
      CachePolicyConfig: 
        Name: test-cache-policy
        DefaultTTL: 10
        MaxTTL: 10
        MinTTL: 1
        ParametersInCacheKeyAndForwardedToOrigin: 
            CookiesConfig: 
              CookieBehavior: none
            EnableAcceptEncodingBrotli: true
            EnableAcceptEncodingGzip: true
            HeadersConfig: 
              HeaderBehavior: none
            QueryStringsConfig: 
              QueryStringBehavior: none

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-cloudformation amazon-cloudfront


    【解决方案1】:

    您的存储桶政策似乎有误。

    您需要为您的 CloudFront 分配创建一个 Origin 访问身份,并允许该身份访问 S3 存储桶。

    {
        "Version": "2012-10-17",
        "Statement": {
            "Sid": "AllowCloudFrontServicePrincipalReadOnly",
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudfront.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*",
            "Condition": {
                "StringEquals": {
                    "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE"
                }
            }
        }
    }
    

    Restricting access to an Amazon S3 origin

    【讨论】:

      猜你喜欢
      • 2023-01-08
      • 2020-10-10
      • 2019-12-19
      • 2013-10-06
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      相关资源
      最近更新 更多