将 CDK 管理的 CloudFormation 分发从 CloudFrontWebDistribution 迁移到分发 API
Migrate a CDK managed CloudFormation distribution from the CloudFrontWebDistribution to the Distribution API
我有一个现有的 CDK 设置,其中使用已弃用的 CloudFrontWebDistribution API 配置了 CloudFormation 分发,现在我需要配置 OriginRequestPolicy,因此在谷歌搜索后,切换到分发 API (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudfront-readme.html) 并重复使用相同的“id”-
Distribution distribution = Distribution.Builder.create(this, "CFDistribution")
当我合成堆栈时,我已经在 yaml 中看到 ID - 例如CloudFrontCFDistribution12345689 - 与之前的不同。
尝试部署时会失败,因为 HTTP 来源 CNAME 已与现有分发相关联。 (“提供的请求无效:您提供的一个或多个 CNAME 已与其他资源相关联。(服务:CloudFront,状态代码:409,请求 ID:123457657,扩展请求 ID:null)”
有没有办法将 OriginRequestPolicy(我只想传输额外的 header)添加到 CloudFrontWebDistribution 或者使用新的 Distribution API 同时维护现有的分布创建一个新的?
(在 AWS 控制台中,相同的操作大约需要点击 3 次)。
您可以使用以下技巧自行分配逻辑 ID,而不是依赖自动生成的逻辑 ID。另一种选择是分两步执行,首先在没有附加 CNAME 的情况下更新它,然后使用附加 CNAME 进行第二次更新。
const cfDistro = new Distribution(this, 'distro', {...});
cfDistro.node.defaultChild.overrideLogicalId('CloudfrontDistribution');
这将导致以下堆栈:
CloudfrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
...
小编辑解释为什么会这样:
由于您要切换到新构造,因此您也会获得一个新的逻辑 ID。为了确保回滚成为可能,CloudFormation 将首先创建所有新资源并创建需要重新创建的更新资源。只有当创建和更新一切都完成后,它才会通过删除旧资源来清理。这也是为什么在更改资源的逻辑 ID 或通过确保相同的逻辑 ID 强制进行正常更新时采用两步法的原因。
非常感谢@stijndepestel - 简单地分配现有的逻辑 ID 在第一次尝试时就成功了。
这是答案中代码的 Java 变体
import software.amazon.awscdk.services.cloudfront.CfnDistribution;
...
((CfnDistribution) distribution.getNode().getDefaultChild()).overrideLogicalId("CloudfrontDistribution");
我有一个现有的 CDK 设置,其中使用已弃用的 CloudFrontWebDistribution API 配置了 CloudFormation 分发,现在我需要配置 OriginRequestPolicy,因此在谷歌搜索后,切换到分发 API (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudfront-readme.html) 并重复使用相同的“id”-
Distribution distribution = Distribution.Builder.create(this, "CFDistribution")
当我合成堆栈时,我已经在 yaml 中看到 ID - 例如CloudFrontCFDistribution12345689 - 与之前的不同。
尝试部署时会失败,因为 HTTP 来源 CNAME 已与现有分发相关联。 (“提供的请求无效:您提供的一个或多个 CNAME 已与其他资源相关联。(服务:CloudFront,状态代码:409,请求 ID:123457657,扩展请求 ID:null)”
有没有办法将 OriginRequestPolicy(我只想传输额外的 header)添加到 CloudFrontWebDistribution 或者使用新的 Distribution API 同时维护现有的分布创建一个新的? (在 AWS 控制台中,相同的操作大约需要点击 3 次)。
您可以使用以下技巧自行分配逻辑 ID,而不是依赖自动生成的逻辑 ID。另一种选择是分两步执行,首先在没有附加 CNAME 的情况下更新它,然后使用附加 CNAME 进行第二次更新。
const cfDistro = new Distribution(this, 'distro', {...});
cfDistro.node.defaultChild.overrideLogicalId('CloudfrontDistribution');
这将导致以下堆栈:
CloudfrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
...
小编辑解释为什么会这样:
由于您要切换到新构造,因此您也会获得一个新的逻辑 ID。为了确保回滚成为可能,CloudFormation 将首先创建所有新资源并创建需要重新创建的更新资源。只有当创建和更新一切都完成后,它才会通过删除旧资源来清理。这也是为什么在更改资源的逻辑 ID 或通过确保相同的逻辑 ID 强制进行正常更新时采用两步法的原因。
非常感谢@stijndepestel - 简单地分配现有的逻辑 ID 在第一次尝试时就成功了。
这是答案中代码的 Java 变体
import software.amazon.awscdk.services.cloudfront.CfnDistribution;
...
((CfnDistribution) distribution.getNode().getDefaultChild()).overrideLogicalId("CloudfrontDistribution");