如何解决"specified origin access identity does not exist or is not valid"

How to resolve "specified origin access identity does not exist or is not valid"

我的 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 存储桶的访问。我可以手动执行此操作。但是我想得到如下图的效果:

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

  1. 由于您的来源是 Amazon S3 存储桶,因此您应该在 Distribution 中使用 S3OriginConfig
  2. 如果需要新的 OAI,那么您必须创建 CloudFrontOriginAccessIdentity 资源并将 OAI 和 S3CanonicalUserId 属性引用到 CloudFront DistributionS3BucketPolicy 资源。

请找到以下片段来回答您的问题。

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', /*]]

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