Add Conditions to Resources in CDK

大兔子大兔子 提交于 2020-04-14 08:44:34

问题


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.

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


回答1:


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


来源:https://stackoverflow.com/questions/59411734/add-conditions-to-resources-in-cdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!