amazon-web-services - cloudformation 创建 cloudfront 后创建失效的最佳方法是什么?

标签 amazon-web-services aws-cloudformation

我正在创建一个完全无服务器的解决方案,它将创建一个 s3 存储桶和 CloudFront。使用来自 bitbucket 管道的云形成模板

我还想为 CloudFront 创建无效。

1) 是否有可能在云形成过程中造成失效?

2) 如果不是,那么如何从我的云结构中获取分发 ID,然后使用 aws cli 创建失效

CFDistribution:
Type: 'AWS::CloudFront::Distribution'
DependsOn: UIBucket
Properties:
  DistributionConfig:
    Aliases:
      - !Sub "${AppSubDomain}.${SSMDomain}"
    Origins:
      - DomainName: !GetAtt UIBucket.DomainName
        Id: S3BucketOrigin
        S3OriginConfig:
          OriginAccessIdentity: !Join
            - ''
            - - 'origin-access-identity/cloudfront/'
              - !Ref CFOriginAccessIdentity
    Comment: !Sub 'CloudFront origin for ${AppSubDomain}.${SSMDomain}'
    DefaultCacheBehavior:
      AllowedMethods:
        - GET
        - HEAD
        - OPTIONS
      TargetOriginId: S3BucketOrigin
      ForwardedValues:
        QueryString: 'false'
        Cookies:
          Forward: none
      ViewerProtocolPolicy: redirect-to-https
    DefaultRootObject: index.html
    Enabled: 'true'
    HttpVersion: http2
    PriceClass: PriceClass_All
    ViewerCertificate:
      AcmCertificateArn: !Ref SSMWildcardCertificateARN
      SslSupportMethod: sni-only
  Tags:
    - Key: "Type"
      Value: "Host"
    - Key: "Product"
      Value: !Ref Product
    - Key: "Environment"
      Value: !Ref SSMEnvironment

最佳答案

我也将 CloudFront 与 CloudFormation 结合使用,但没有找到使用 CloudFormation 创建失效的方法。如果您检查AWS文档,CloudFormation允许与CloudFront相关的3种类型

CloudFront
  AWS::CloudFront::CloudFrontOriginAccessIdentity
  AWS::CloudFront::Distribution
  AWS::CloudFront::StreamingDistribution

并且这些都不会造成失效。回答你的第一个问题:

1) 是否有可能在云形成过程中造成失效?

没有。

2) 如果不是,那么如何从我的云结构中获取分发 ID,然后使用 aws cli 创建失效

您可以将分发添加到 CloudFormation 模板输出:

Outputs:
  CloudFrontDistributionID:
    Description: 'CloudFront distribution ID'
    Value: !Ref CloudFrontDistribution
  CloudFrontURL:
    Description: 'CloudFront URL'
    Value:!GetAtt CloudFrontDistribution.DomainName

使用 bash 保存发行版 ID(检查此 question ):

$ distributionId=${aws cloudformation describe-stacks --stack-name MY_STACK --query "Stacks[0].Outputs[?OutputKey=='CloudFrontDistributionID'].OutputValue" --output text}

最后,创建 CloudFront invalidation :

$ aws cloudfront create-invalidation --distribution-id $distributionId --paths /index.html /error.html

关于amazon-web-services - cloudformation 创建 cloudfront 后创建失效的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033968/

相关文章:

amazon-web-services - EC2 实例不会保留弹性 IP

amazon-web-services - 如何删除不是由堆栈创建的AWS资源?

amazon-web-services - AWS 负载均衡器中的持续后端连接错误

python - Optimized_execution() 需要 1 个位置参数,但给出了 2 个

regex - CloudFormation 参数动态正则表达式

amazon-s3 - AWS S3 生命周期配置过滤器以禁用同一存储桶中某些文件夹的存储类冰川

security - 删除EC2默认安全组

amazon-web-services - YAML 文件中的变量,将其作为云形成中的标签

hadoop-yarn - 带有 yarn 调度程序的 aws emr

amazon-web-services - AWS Codepipeline - 一次在多个环境上部署