如何解决“指定的原始访问身份不存在或无效”

时间:2019-04-03 13:13:02

标签: amazon-cloudfront serverless

我在serverless.yml文件中的这一行有问题。 我使用的是serverless-single-page-app-plugin。

# CustomOriginConfig:
              #  HTTPPort: 80
              #  HTTPSPort: 443
              # OriginProtocolPolicy: https-only
              ## In case you want to restrict the bucket access use S3OriginConfig and remove CustomOriginConfig
              S3OriginConfig:
                 OriginAccessIdentity: origin-access-identity/cloudfront/E127EXAMPLE51Z

我想使用s3OriginConfig并禁用通过s3存储桶的访问。我可以做这本手册。我想要获得如下图所示的效果

https://i.imgur.com/YiLziy7.png

1 个答案:

答案 0 :(得分:2)

您可能已经解决了,因为您很久以前就问过问题,但是如果您不这样做,这可能会有所帮助。我也面临同样的问题,在通过AWS文档进行了一些研究之后,我才知道如何使用所需的属性。关于您的问题,请考虑以下几点。

  1. 由于您的起源是Amazon S3存储桶,因此您应在Distribution中使用 S3OriginConfig
  2. 如果需要新的OAI,则必须创建 CloudFrontOriginAccessIdentity 资源,并将OAI和 S3CanonicalUserId 属性引用到 CloudFront分布和<分别是strong> S3BucketPolicy 资源。

请根据您的问题找到以下代码段。

WebAppDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - DomainName: 'passport-front.s3.amazonaws.com'
            Id: 'WebApp'
            S3OriginConfig:
              OriginAccessIdentity: !Join ['', ['origin-access-identity/cloudfront/', !Ref CloudFrontOAI]]
CloudFrontOAI:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: 'access-identity-passport-front.s3.amazonaws.com'
WebAppBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: "Retain"
    Properties:
      AccessControl: PublicRead
      BucketName: "passport-front"
WebAppBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref WebAppBucket
      PolicyDocument:
        Statement:
        - Action: s3:GetObject
          Effect: Allow
          Principal:
            CanonicalUser: !GetAtt CloudFrontOAI.S3CanonicalUserId
          Resource: !Join ['', ['arn:aws:s3:::', !Ref 'WebAppBucket', /*]]
  

参考:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-cloudfront.html

相关问题