How do I pass CommaDelimitedList parameters to nested stacks in CloudFormation?

3 minute read
0

I want to pass CommaDelimitedList parameters to nested stacks in AWS CloudFormation.

Resolution

You can't pass CommaDelimitedList type values to a nested stack. Instead, use the Fn::Join intrinsic function in your parent stack to convert type CommaDelimitedList to type String.

To pass a list of SecurityGroupIds from a parent stack to a nested stack, complete the following steps:

  1. Open the JSON or YAML file of your parent stack.
  2. For SecurityGroupIds, set Type to CommaDelimitedList.
  3. In the JSON and YAML files, the combined string converts the SecurityGroupIds parameter type from CommaDelimitedList to String.
    For a JSON file: Under Resources, the Fn::Join function returns the combined string. For example:
    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Parameters": {
        "SubnetId": {
          "Type": "AWS::EC2::Subnet::Id"
        },
        "SecurityGroupIds": {
          "Type": "List<AWS::EC2::SecurityGroup::Id>"
        },
        "KeyName": {
          "Type": "AWS::EC2::KeyPair::KeyName"
        },
        "ImageId": {
          "Type": "String"
        }
      },
      "Resources": {
        "Instance": {
          "Type": "AWS::CloudFormation::Stack",
          "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml",
            "Parameters": {
              "SubnetId": {
                "Ref": "SubnetId"
              },
              "SecurityGroupIds": {
                "Fn::Join": [
                  ",",
                  {
                    "Ref": "SecurityGroupIds"
                  }
                ]
              },
              "KeyName": {
                "Ref": "KeyName"
              },
              "ImageId": {
                "Ref": "ImageId"
              }
            }
          }
        }
      }
    }
    For a YAML file: Under Resources, the !Join function returns the combined string. For example:
    AWSTemplateFormatVersion: 2010-09-09
    Parameters:
      SubnetId:
        Type: 'AWS::EC2::Subnet::Id'
      SecurityGroupIds:
        Type: 'List<AWS::EC2::SecurityGroup::Id>'
      KeyName:
        Type: 'AWS::EC2::KeyPair::KeyName'
      ImageId:
        Type: String
    Resources:
      Instance:
        Type: 'AWS::CloudFormation::Stack'
        Properties:
          TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml'
          Parameters:
            SubnetId: !Ref SubnetId
            SecurityGroupIds: !Join 
              - ','
              - !Ref SecurityGroupIds
            KeyName: !Ref KeyName
            ImageId: !Ref ImageId
    Note: If you pass two subnets, such as ["subnet-aaaa, subnet-bbbb"], then the output of Fn::Join is {"subnet-aaaa, subnet-bbbb"}.
  4. In the JSON or YAML file of your nested stack, for SecurityGroupIds, set the Type to CommaDelimitedList.
    Example nested JSON file:
    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Parameters": {
        "SubnetId": {
          "Type": "String"
        },
        "SecurityGroupIds": {
          "Type": "CommaDelimitedList"
        },
        "KeyName": {
          "Type": "String"
        },
        "ImageId": {
          "Type": "String"
        }
      },
      "Resources": {
        "Ec2instance": {
          "Type": "AWS::EC2::Instance",
          "Properties": {
            "ImageId": {
              "Ref": "ImageId"
            },
            "KeyName": {
              "Ref": "KeyName"
            },
            "SecurityGroupIds": {
              "Ref": "SecurityGroupIds"
            },
            "SubnetId": {
              "Ref": "SubnetId"
            }
          }
        }
      }
    }
    Example nested YAML file:
    AWSTemplateFormatVersion: 2010-09-09
    Parameters:
      SubnetId:
        Type: String
      SecurityGroupIds:
        Type: CommaDelimitedList
      KeyName:
        Type: String
      ImageId:
        Type: String
    Resources:
      Ec2instance:
        Type: 'AWS::EC2::Instance'
        Properties:
          ImageId: !Ref ImageId
          KeyName: !Ref KeyName
          SecurityGroupIds: !Ref SecurityGroupIds
          SubnetId: !Ref SubnetId
    Note: In the nested stack, the combined string from the parent stack is passed to SecurityGroupIds as CommaDelimitedList. For example, the {"sg-aaaaa, sg-bbbbb"} value is converted back to ["sg-aaaaa", "sg-bbbbb"]. SecurityGroupIds: !Ref SecurityGroupIds must directly reference SecurityGroupIds, not as a list of strings.

Related information

Embed stacks within other stacks using nested stacks

AWS::CloudFormation::Stack

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago