问题
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 CfnResource
s, 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