I need to create an AWS CloudFormation stack from a CloudFormation template using multiple values for individual parameters. How do I do that?
CloudFormation includes CommaDelimitedList parameter types that can be used with AWS-Specific Parameter Types to specify resource ID values in your account. Use AWS-specific parameter types with the CloudFormation console to avoid invalid values.
When using the AWS Command Line Interface (AWS CLI) CloudFormation command create-stack, you must pass parameter values for AWS-specific list parameter types as a comma-delimited string.
For information on creating a CloudFormation template, see Getting Started with AWS CloudFormation.
Add a list parameter type to your CloudFormation template. With these sample CloudFormation templates, the parameter with the key Subnets specifies a list parameter type that can accept multiple distinct subnet ID values:
JSON
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters" : {
"VpcId" : {
"Type" : "AWS::EC2::VPC::Id",
"Description" : "VpcId of your existing Virtual Private Cloud (VPC)",
"ConstraintDescription" : "must be the VPC Id of an existing Virtual Private Cloud."
},
"Subnets" : {
"Type" : "List<AWS::EC2::Subnet::Id>",
"Description" : "The list of SubnetIds in your Virtual Private Cloud (VPC)",
"ConstraintDescription" : "must be a list of at least two existing subnets associated with at least two different availability zones. They should be residing in the selected Virtual Private Cloud."
},
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "t2.small",
"AllowedValues" : [ "t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large"],
"ConstraintDescription" : "must be a T2 instance type."
},
"KeyName" : {
"Description" : "The EC2 Key Pair to allow SSH access to the instances",
"Type" : "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
},
"SSHLocation" : {
"Description" : "The IP address range that can be used to SSH to the EC2 instances",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
}
} ,
"Mappings": {
"AWSRegionAMIEC2": {
"ap-northeast-1": {
"AMZLN": "ami-ceafcba8"
},
"ap-southeast-1": {
"AMZLN": "ami-68097514"
},
"ap-southeast-2": {
"AMZLN": "ami-942dd1f6"
},
"eu-central-1": {
"AMZLN": "ami-5652ce39"
},
"eu-west-1": {
"AMZLN": "ami-d834aba1"
},
"us-east-1": {
"AMZLN": "ami-97785bed"
},
"us-west-1": {
"AMZLN": "ami-824c4ee2"
},
"us-west-2": {
"AMZLN": "ami-f2d3638a"
}
}
},
"Resources" : {
"WebServerGroup" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"VPCZoneIdentifier" : { "Ref" : "Subnets" },
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MinSize" : "1",
"MaxSize" : "3"
},
"CreationPolicy" : {
"ResourceSignal" : {
"Timeout" : "PT15M",
"Count" : "1"
}
},
"UpdatePolicy": {
"AutoScalingRollingUpdate": {
"MinInstancesInService": "1",
"MaxBatchSize": "1",
"PauseTime" : "PT15M",
"WaitOnResourceSignals": "true"
}
}
},
"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"Comment" : "Install httpd",
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"yum" : {
"httpd" : []
}
}
}
}
},
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"ImageId": {
"Fn::FindInMap": [
"AWSRegionAMIEC2",
{
"Ref": "AWS::Region"
},
"AMZLN"
]
},
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"InstanceType" : { "Ref" : "InstanceType" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource LaunchConfig ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource WebServerGroup ",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}
}
},
"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access ",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : { "Ref" : "SSHLocation"}
} ],
"VpcId" : { "Ref" : "VpcId" }
}
}
}
}
YAML
AWSTemplateFormatVersion: 2010-09-09
Parameters:
VpcId:
Type : 'AWS::EC2::VPC::Id'
Description : VpcId of your existing Virtual Private Cloud (VPC)
ConstraintDescription : >-
must be the VPC Id of an existing Virtual Private Cloud
Subnets:
Type: 'List<AWS::EC2::Subnet::Id>'
Description: The list of SubnetIds in your Virtual Private Cloud (VPC)
ConstraintDescription: >-
must be a list of at least two existing subnets associated with at least
two different availability zones. They should be residing in the selected
Virtual Private Cloud.
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.small
AllowedValues:
- t2.nano
- t2.micro
- t2.small
- t2.medium
- t2.large
ConstraintDescription: must be a T2 instance type.
KeyName:
Description: The EC2 Key Pair to allow SSH access to the instances
Type: 'AWS::EC2::KeyPair::KeyName'
ConstraintDescription: must be the name of an existing EC2 KeyPair.
SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Mappings:
AWSRegionAMIEC2:
ap-northeast-1:
AMZLN: ami-ceafcba8
ap-southeast-1:
AMZLN: ami-68097514
ap-southeast-2:
AMZLN: ami-942dd1f6
eu-central-1:
AMZLN: ami-5652ce39
eu-west-1:
AMZLN: ami-d834aba1
us-east-1:
AMZLN: ami-97785bed
us-west-1:
AMZLN: ami-824c4ee2
us-west-2:
AMZLN: ami-f2d3638a
Resources:
WebServerGroup:
Type: 'AWS::AutoScaling::AutoScalingGroup'
Properties:
VPCZoneIdentifier: !Ref Subnets
LaunchConfigurationName: !Ref LaunchConfig
MinSize: '1'
MaxSize: '3'
CreationPolicy:
ResourceSignal:
Timeout: PT15M
Count: '1'
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: '1'
MaxBatchSize: '1'
PauseTime: PT15M
WaitOnResourceSignals: 'true'
LaunchConfig:
Type: 'AWS::AutoScaling::LaunchConfiguration'
Metadata:
Comment: Install httpd
'AWS::CloudFormation::Init':
config:
packages:
yum:
httpd: []
Properties:
KeyName: !Ref KeyName
ImageId: !FindInMap
- AWSRegionAMIEC2
- !Ref 'AWS::Region'
- AMZLN
SecurityGroups:
- !Ref InstanceSecurityGroup
InstanceType: !Ref InstanceType
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource LaunchConfig '
- ' --region '
- !Ref 'AWS::Region'
- |+
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource WebServerGroup '
- ' --region '
- !Ref 'AWS::Region'
- |+
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'Enable SSH access '
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref SSHLocation
VpcId: !Ref VpcId
Did this page help you? Yes | No
Back to the AWS Support Knowledge Center
Need help? Visit the AWS Support Center
Published: 2016-12-23
Updated: 2018-08-02