How to conditionalize nested stack creation via CommaDelimitedList parameter

684 Views Asked by At

in my AWS CloudFormation Template, I would like to conditionalize nested stack creation via a CommaDelimitedList parameter.

I would have done it this way:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "DeployNestedStacks": {
      "Description": "List of nested stack resources to deploy",
      "Type": "CommaDelimitedList",
      "AllowedValues": [
        "nestedStack1",
        "nestedStack2"
      ]
    }
  },
  "Conditions": {
    "DeployNestedStack1": { "Fn::Contains": [ { "Ref": "DeployNestedStacks" } , "nestedStack1" ] }
    "DeployNestedStack2": { "Fn::Contains": [ { "Ref": "DeployNestedStacks" } , "nestedStack2" ] }
  },
  "Resources": {
    "MyNestedStack1": {
      "Condition": "DeployNestedStack1",
      "Type": "AWS::CloudFormation::Stack",
      ... 
    },
    "MyNestedStack2": {
      "Condition": "DeployNestedStack2",
      "Type": "AWS::CloudFormation::Stack",
      ... 
    }
  }
}

unfortunately Fn::Contains is not a valide Fn.

Still, I think it illustrates well what I would like to achieve.

How would you achieve this, please?

1

There are 1 best solutions below

0
Marcin On

Unfortunately, there is no such functionality in plain CloudFormation (CFN). CFN is not a programming language and it does not have loops, searches though lists and many other things found in regular programming language.

However, there some possible alternatives to consider:

  • Use of macros
  • Use of custom resources

  • Using CDK, instead of CFN.

  • Redesigning your template to have Conditions based on regular parameter values, not lists. For example using Select to do index based comparison.