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

2 minute read
0

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

Short description

You can pass multiple values for individual parameters in an AWS CloudFormation template using one of these ways:

Resolution

Use AWS-specific parameter types to select values from a pre-populated list of existing AWS values from an AWS account

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

In these example AWS CloudFormation templates, the parameter with the SecurityGroups key specifies an AWS-specific parameter type 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 using the AWS CLI, use the following command:

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

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

Use CommaDelimitedList parameter types to enter input values

In the following AWS 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 OFFICIAL
AWS OFFICIALUpdated a year ago