首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CloudFormation或serverless.yml中提供OriginAccessIdentity引用

在CloudFormation或serverless.yml中提供OriginAccessIdentity引用
EN

Stack Overflow用户
提问于 2019-02-15 22:19:40
回答 3查看 2.6K关注 0票数 3

我想要一个可以访问私有S3存储桶的CloudFront发行版。为此,我必须创建一个源访问标识。手动地,我可以使用亚马逊网络服务控制台,但我想通过一个CloudFormation脚本或使用无服务器(使用serverless.yml)来创建它。在这样做的同时,我能够将源访问标识的物理Id添加到我的CloudFront分发版中(使用一个脚本)。

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

我试过这个:

代码语言:javascript
复制
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"

我必须创建一个源访问标识和一个具有该标识的CloudFront分发。我们可以在一个CloudFormation脚本中或使用无服务器(使用serverless.yml)来完成这两件事吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-18 15:57:13

您绝对可以在同一个serverless.yml中创建源访问标识和CloudFront分发。

我已经修改了您的方案,并将OriginAccessIdentity更改为使用Fn::Join

代码语言:javascript
复制
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"

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

票数 5
EN

Stack Overflow用户

发布于 2019-02-15 23:14:10

可以,您可以在同一个CloudFormation模板中同时创建这两个文件。cloudfrontoriginaccessidentity是一个单独的资源,因此需要从myDistribution下移出。

代码语言:javascript
复制
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: "toyoguard-acces-identity"
票数 2
EN

Stack Overflow用户

发布于 2019-09-27 03:15:17

不要忘记将s3策略和存储桶添加到您的dependsOn列表中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54711268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档