在 CloudFormation 或 serverless.yml 中提供 OriginAccessIdentity 参考

ven*_*nge 3 amazon-s3 aws-cloudformation serverless-framework serverless

我想要一个可以访问私有 S3 存储桶的 CloudFront 分配。为此,我必须创建一个原始访问身份。手动,我可以使用 AWS 控制台来做到这一点,但我想通过 CloudFormation 脚本或无服务器(使用serverless.yml)来创建它。在执行此操作时,我可以将原始访问身份的物理 ID 添加到我的 CloudFront 分配(使用一个脚本)。

相关文档:https : //docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-cloudfront.html

我试过这个:

myDistribution:
  Type: AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      Origins:
      - DomainName:bucket.s3.amazonaws.com
        Id: myS3Origin
        S3OriginConfig: {
          OriginAccessIdentity:origin-access-identity/cloudfront/ !Ref cloudfrontoriginaccessidentity
        }
      Enabled: 'true'
      Comment: Some comment
      DefaultCacheBehavior:
        ForwardedValues:
          QueryString: 'false'
          Cookies:
            Forward: none
        AllowedMethods:
        - GET
        - HEAD
        - OPTIONS
        TargetOriginId: myS3Origin
        ViewerProtocolPolicy: redirect-to-https
      PriceClass: PriceClass_200
      ViewerCertificate:
        CloudFrontDefaultCertificate: 'true'
cloudfrontoriginaccessidentity:
  Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
  Properties:
    CloudFrontOriginAccessIdentityConfig:
      Comment: "some comment"
Run Code Online (Sandbox Code Playgroud)

我必须创建一个原始访问身份和一个具有此身份的 CloudFront 分配。我们可以在一个 CloudFormation 脚本中或使用无服务器(使用serverless.yml)来完成这两项工作吗?

Ale*_*lex 5

您绝对可以在同一个serverless.yml.

我已经修改了您的场景并将其更改OriginAccessIdentity为使用Fn::Join.

myDistribution:
  Type: AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      Origins:
      - DomainName:bucket.s3.amazonaws.com
        Id: myS3Origin
        S3OriginConfig:
          OriginAccessIdentity:
            Fn::Join:
              - ''
              -
                - 'origin-access-identity/cloudfront/'
                - Ref: cloudfrontoriginaccessidentity
      Enabled: 'true'
      Comment: Some comment
      DefaultCacheBehavior:
        ForwardedValues:
          QueryString: 'false'
          Cookies:
            Forward: none
        AllowedMethods:
        - GET
        - HEAD
        - OPTIONS
        TargetOriginId: myS3Origin
        ViewerProtocolPolicy: redirect-to-https
      PriceClass: PriceClass_200
      ViewerCertificate:
        CloudFrontDefaultCertificate: 'true'

cloudfrontoriginaccessidentity:
  Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
  Properties:
    CloudFrontOriginAccessIdentityConfig:
      Comment: "some comment"
Run Code Online (Sandbox Code Playgroud)

无服务器示例存储库也有一个很好的例子:https : //github.com/serverless/examples/blob/master/aws-node-single-page-app-via-cloudfront/serverless.yml