Simple
- Create S3 bucket
- Create an EC2 instance
- Solution:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
"Resources": {
"mys3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "qts3forpracticemay"
}
},
"myec2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-0470e33cd681b2476",
"InstanceType": "t2.micro"
}
}
}
}
- The above template is not resuable bcoz of names directly used in properties (hard-coded)
- We can eliminate hard coding by using parameters Refer Here
- Lets add a parameter for s3 buckets name and ec2 instance type and Image-Id. Refer Here for parameters section.
- Lets add the string parameters to the template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
"Parameters": {
"s3bucketname": {
"Description": "The name of the bucket which you want to create",
"Type": "String"
},
"ec2instancetype": {
"Description": "Instance type of ec2",
"Type": "String",
"Default": "t2.micro"
},
"amiid": {
"Description": "Image Id",
"Type": "String",
"Default": "ami-0470e33cd681b2476"
}
},
"Resources": {
"mys3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "qts3forpracticemay"
}
},
"myec2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-0470e33cd681b2476",
"InstanceType": "t2.micro"
}
}
}
}
- Lets add the references of the parameters in resources using Ref
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
"Parameters": {
"s3bucketname": {
"Description": "The name of the bucket which you want to create",
"Type": "String"
},
"ec2instancetype": {
"Description": "Instance type of ec2",
"Type": "String",
"Default": "t2.micro"
},
"amiid": {
"Description": "Image Id",
"Type": "String",
"Default": "ami-0470e33cd681b2476"
},
"s3accesscontrol": {
"Description": "S3 Access control",
"Type": "String",
"Default": "PublicRead"
}
},
"Resources": {
"mys3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": {
"Ref": "s3accesscontrol"
},
"BucketName": {
"Ref": "s3bucketname"
}
}
},
"myec2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "amiid"
},
"InstanceType": {
"Ref": "ec2instancetype"
}
}
}
}
}
- Now if we create the stack from cf template the parameters section will be shown in the UI

- Now lets restrict user from entering anything into parameters by using AllowedValues and using Regular expressions in parameter section AllowedPattern.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
"Parameters": {
"s3bucketname": {
"Description": "The name of the bucket which you want to create",
"Type": "String"
},
"ec2instancetype": {
"Description": "Instance type of ec2",
"Type": "String",
"AllowedValues": [
"t2.micro",
"t2.nano",
"t2.small"
],
"Default": "t2.micro"
},
"amiid": {
"Description": "Image Id",
"Type": "String",
"Default": "ami-0470e33cd681b2476"
},
"s3accesscontrol": {
"Description": "S3 Access control",
"Type": "String",
"AllowedValues": [
"Private",
"PublicRead",
"PublicReadWrite",
"AuthenticatedRead",
"LogDeliveryWrite",
" BucketOwnerRead",
"BucketOwnerFullControl"
],
"Default": "PublicRead"
}
},
"Resources": {
"mys3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": {
"Ref": "s3accesscontrol"
},
"BucketName": {
"Ref": "s3bucketname"
}
}
},
"myec2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "amiid"
},
"InstanceType": {
"Ref": "ec2instancetype"
}
}
}
}
}
- Lets add the security group to the AWS Ec2 instance using a AWS Specific parameter AWS::EC2::SecurityGroup::GroupName and key pair
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
"Parameters": {
"s3bucketname": {
"Description": "The name of the bucket which you want to create",
"Type": "String"
},
"ec2instancetype": {
"Description": "Instance type of ec2",
"Type": "String",
"AllowedValues": [
"t2.micro",
"t2.nano",
"t2.small"
],
"Default": "t2.micro"
},
"amiid": {
"Description": "Image Id",
"Type": "String",
"Default": "ami-0470e33cd681b2476"
},
"s3accesscontrol": {
"Description": "S3 Access control",
"Type": "String",
"AllowedValues": [
"Private",
"PublicRead",
"PublicReadWrite",
"AuthenticatedRead",
"LogDeliveryWrite",
" BucketOwnerRead",
"BucketOwnerFullControl"
],
"Default": "PublicRead"
},
"sgname": {
"Description": "Security Groups",
"Type": "AWS::EC2::SecurityGroup::GroupName"
},
"awskeypair": {
"Description": "Keypair",
"Type": "AWS::EC2::KeyPair::KeyName"
}
},
"Resources": {
"mys3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": {
"Ref": "s3accesscontrol"
},
"BucketName": {
"Ref": "s3bucketname"
}
}
},
"myec2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "amiid"
},
"InstanceType": {
"Ref": "ec2instancetype"
},
"SecurityGroups": [
{
"Ref": "sgname"
}
],
"KeyName": {
"Ref": "awskeypair"
}
}
}
}
}