Skip to content

How do I use multiple values for individual parameters in an AWS CloudFormation template?

3 minute read
1

I want to use multiple values for individual parameters to create or update a stack from an AWS CloudFormation template.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To use multiple values for individual parameters in a CloudFormation template, take one of the following actions:

Use parameter types specific to AWS

Note: CloudFormation validates the input value that you select against existing values in your account.

In the following CloudFormation template examples, the parameter with the SecurityGroups key specifies a parameter type specific to AWS that can accept multiple values for SecurityGroupIds.

JSON template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "List<AWS::EC2::SecurityGroup::Id>",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML template:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups

To deploy the stack, run the create-stack AWS CLI command:

aws cloudformation create-stack --stack-name StackName --template-body file://TemplateFileName
--parameters ParameterKey=SecurityGroups,ParameterValue="sg-0123456789\,sg-2345678901"

Note: Replace StackName with the name of your stack and TemplateFileName with the name of your file. For ParameterValue, enter your security group ID.

Use CommaDelimitedList parameter types

In the following CloudFormation template examples, the parameter with the SecurityGroups key specifies a CommaDelimitedList type that can accept multiple values for SecurityGroupIds.

JSON template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "CommaDelimitedList",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)",
      "Default": "sg-a123fd85, sg-b456ge94"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML template:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: CommaDelimitedList
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
    Default: sg-a123fd85, sg-b456ge94
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups
AWS OFFICIALUpdated 9 months ago
2 Comments

Thanks, I think this is also the solution for such error data type taken from Cloud Formation Parameter, which expected string, but found JSONArray, because I set on Parameter as list.

Parameters:
  SecurityGroupID:
    Description: "Security group name"
    Type: List<AWS::EC2::SecurityGroup::Id>
    Default: "sg-0f254747e7"

Resources:
  # EC2 Instance Public
  EC2PublicInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ImageID
      SecurityGroupIds: !Ref SecurityGroupID

Stack Error Message: "[#/SecurityGroupIds/0: expected type: String, found: JSONArray]"

replied 9 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR
replied 9 months ago