Reputation: 198
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?
Upvotes: 11
Views: 18510
Reputation: 1919
Here is a example on how to achieve this for a iam.Role
:
const role = new iam.Role(this, "TestRole", {...});
const conditionKey = "AssumeRolePolicyDocument.Statement.0.Condition.ForAnyValue:StringLike";
const conditionValue = {
"aws:userid": [
"[email protected]",
"[email protected]",
],
};
const roleRef = role.node.defaultChild as iam.CfnRole;
roleRef.addPropertyOverride(conditionKey, conditionValue);
Upvotes: 0
Reputation: 7407
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
Upvotes: 14