本文介绍了向CDK中的资源添加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CDK堆栈,该堆栈将部署在多个区域中.其中一种构造只能部署在一个区域中.在Cloudformation中,我只是向资源中添加一个条件,但是还没有找到一种对CDK构造执行类似操作的方法.

I have created a CDK stack that will be deployed in multiple regions. One of the constructs shall only be deployed in one region. In Cloudformation I'd simply add a Condition to the resource, but I haven't found a way to do something similar with CDK constructs.

可以定义 CfnCondition 并将其添加到 CfnResource s中,但是我如何为lambda函数之类的构造添加条件?

It is possible to define a CfnCondition and add it to CfnResources, but I how do I add conditions to constructs like lambda functions?

推荐答案

以下是有关如何为 iam.User 实现此目标的示例:

Here is a example on how to achieve this for a iam.User:

// Create a CloudFormation condition on the region
const regionCondition = new cdk.CfnCondition(this, 'RegionCondition', {
  expression: cdk.Fn.conditionEquals(cdk.Stack.of(this).region, 'eu-west-1'),
});

// Create the user using the L2 construct
const user = new iam.User(this, 'User');

// Add the condition on the underlying AWS::IAM::User
(user.node.defaultChild as iam.CfnUser).cfnOptions.condition = regionCondition

这篇关于向CDK中的资源添加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:09