Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Conditions to Resources in CDK

Tags:

aws-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.

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

like image 348
Lukas Avatar asked Dec 19 '19 14:12

Lukas


1 Answers

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
like image 56
jogold Avatar answered Sep 20 '22 12:09

jogold