I see examples of creating resources conditionally in terraform as:
count = var.day != "Sunday" ? 1 : 0
How does one achieve this in CDK for TF? I don’t see the ternary as an Fn operator. I am generating a TF module from my CDK so I need this encoded in the output.
This is not supported in 0.15 (we will ship support for count in 0.16, but the ternary is still missing), but you can use an override.
myConditionalResource.addOverride("count", `\${${myDayVar.stringValue} != "Sunday" ? 1 : 0}`)
We are basically writing your TF expression into the count field directly (therefore we need to wrap it in ${}
to indicate its a TF expression. We can use your variable with the stringValue accessor to do the var.day
portion, this way it will change if you change the variable name.
Why don’t you use a simply if
statement?
if enable_resource:
MyResource(...)