AWS CloudFormation - Using Parameters
Parameters are input arguments to CloudFormation script. Items like InterfaceID and InstanceProfile are custom parameters where we define the possible values. NameTag is a customer parameter where input values are open-ended with character limit/type limits. The remainder are AWS Supported Parameter Types, more can be found
here.
"Parameters":{
"ImageId":{
"Description":"Image ID",
"Type":"String",
"Default":"ami-284f1a3e",
"AllowedValues":[
"ami-284f1a3e",
"ami-36174e4d"
],
"ConstraintDescription":""
},
"InstanceType":{
"Description":"Instance Type",
"Type":"String",
"Default":"t2.small",
"AllowedValues":[
"t2.nano",
"t2.micro",
"t2.small",
"t2.medium",
"t2.large",
"t2.xlarge",
"t2.2xlarge"
],
"ConstraintDescription":""
},
"KeyName":{
"Description":"Key Name",
"Type":"AWS::EC2::KeyPair::KeyName",
"ConstraintDescription":""
},
"InterfaceID":{
"Description":"Network Interface ID",
"Type":"String",
"Default":"eni-db0e3c7a",
"AllowedValues":[
"eni-9158e145",
"eni-db0e3c7a"
],
"ConstraintDescription":""
},
"NameTag":{
"Description":"Name Tag",
"Type":"String",
"Default":"NameTag",
"MinLength":"1",
"MaxLength":"10",
"AllowedPattern":"[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription":""
},
"InstanceProfile":{
"Description":"Instance Profile",
"Type":"String",
"Default":"read_only_instance",
"AllowedValues":[
"read_only_instance",
"admin_instance"
],
"ConstraintDescription":""
}
}
In the Resources, we reference the above parameters using keyword, "Ref"
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "ImageId"
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"NetworkInterfaces": [
{
"NetworkInterfaceId":
{
"Ref": "InterfaceID"
},
"DeviceIndex":"0"
}
],
"Tags": [
{
"Key": "Name",
"Value": {
"Ref": "NameTag"
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "4b94664e-1e18-400c-9733-ff095ff6e854"
}
}
}
}
The whole thing looks like this:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters":{
"ImageId":{
"Description":"Image ID",
"Type":"String",
"Default":"ami-284f1a3e",
"AllowedValues":[
"ami-284f1a3e",
"ami-36174e4d"
],
"ConstraintDescription":""
},
"InstanceType":{
"Description":"Instance Type",
"Type":"String",
"Default":"t2.small",
"AllowedValues":[
"t2.nano",
"t2.micro",
"t2.small",
"t2.medium",
"t2.large",
"t2.xlarge",
"t2.2xlarge"
],
"ConstraintDescription":""
},
"KeyName":{
"Description":"Key Name",
"Type":"AWS::EC2::KeyPair::KeyName",
"ConstraintDescription":""
},
"InterfaceID":{
"Description":"Network Interface ID",
"Type":"String",
"Default":"eni-db0e3c7a",
"AllowedValues":[
"eni-9158e145",
"eni-db0e3c7a"
],
"ConstraintDescription":""
},
"NameTag":{
"Description":"Name Tag",
"Type":"String",
"Default":"NameTag",
"MinLength":"1",
"MaxLength":"10",
"AllowedPattern":"[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription":""
},
"InstanceProfile":{
"Description":"Instance Profile",
"Type":"String",
"Default":"read_only_instance",
"AllowedValues":[
"read_only_instance",
"admin_instance"
],
"ConstraintDescription":""
}
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"4b94664e-1e18-400c-9733-ff095ff6e854": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 304,
"y": 225
},
"z": 0
}
}
},
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "ImageId"
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"NetworkInterfaces": [
{
"NetworkInterfaceId":
{
"Ref": "InterfaceID"
},
"DeviceIndex":"0"
}
],
"Tags": [
{
"Key": "Name",
"Value": {
"Ref": "NameTag"
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "4b94664e-1e18-400c-9733-ff095ff6e854"
}
}
}
}
}
When you create stack from this: