【问题标题】:Invalidate Cloudfront cache with AWS CDK Pipelines使用 AWS CDK 管道使 Cloudfront 缓存无效
【发布时间】:2022-04-20 21:01:36
【问题描述】:

作为我在 CDK 中的 CodePipeline 的一部分,作为最后一步,我希望使 Cloudfront 缓存无效。

这是我当前的部署操作步骤:

{
  stageName: 'Deploy',
  actions: [
    new codepipelineActions.S3DeployAction({
      actionName: 'S3Deploy',
      bucket: frontendCodeBucket, // See bucket config below
      input: buildOutput, // Output from Build step
    }),
  ]
}

这是我的代码桶和 CF 分布:

const frontendCodeBucket = new s3.Bucket(this, 'FrontendBucketStaging', {
  websiteIndexDocument: 'index.html',
  encryption: s3.BucketEncryption.S3_MANAGED,
  blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
  bucketName: 'something',
  removalPolicy: RemovalPolicy.DESTROY,
});


const distribution = new cloudfront.CloudFrontWebDistribution(this, 'FrontendCloudfrontStaging', {
  originConfigs: [
    {
      s3OriginSource: {
        s3BucketSource: frontendCodeBucket,
        originAccessIdentity: oai,
      },
      behaviors : [ {isDefaultBehavior: true}]
    }
  ],

我找不到通过 S3DeployAction 使缓存失效的任何方法。 在使用静态网站和 Cloudfront 时,这似乎是最常见的事情之一。这根本不可能吗?

如果不是。有解决方法吗? 例如,在非管道流程中,这样的事情应该可以工作(我读过):

new s3deploy.BucketDeployment(this, 'DeployWithInvalidation', {
  sources: [<some assets>],
  destinationBucket: bucket,
  distribution,
  distributionPaths: ['/*'],
});

那么有没有办法在管道中添加这样一个步骤,而不是“Action”?

非常高兴获得任何帮助或指点。我对 CDK 很陌生,但这感觉就像有人想做的一件很常见的事情,所以我希望我只是在这里遗漏了一些东西。除了最后一步之外,管道运行良好。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-cloudfront aws-cdk


    【解决方案1】:

    我最终在 S3DeployAction 之后添加了另一个 CodeBuildAction 步骤,其唯一目的是运行此 AWS CLI 命令:

    aws cloudfront create-invalidation --distribution-id ${CLOUDFRONT_ID} --paths "/*"
    

    也许不是最漂亮的解决方案,但它有效 :) 如果失效是 S3DeployAction 中的一个选项,那就太好了

    参考:https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline_actions-readme.html#invalidating-the-cloudfront-cache-when-deploying-to-s3

    【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 2022-06-30
    • 2019-02-22
    • 1970-01-01
    • 2021-10-18
    • 2018-02-22
    • 2022-08-14
    • 2019-04-25
    相关资源
    最近更新 更多