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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With