References and concepts
- Refer
- Install Visual Studio Code and Cloudformation Extension

- Format Version
- Preview of Workflows

Cloudformation Resources
- Refer Here
- In Resources section we describe/declare what resources have to be created.
"Resources" : {
"Logical ID" : {
"Type" : "Resource type",
"Properties" : {
Set of properties
}
}
}
Resources:
Logical ID:
Type: Resource type
Properties:
Set of properties
- Logical ID is unique id given by template designer/creator
- Every resource in AWS has a unique type and it will have set of properties

- In the Resources section, we create multiple resources as per the needs of the application / infra to be deployed
Cloudformation template to create a vpc with 4 subnets
- Make a note of manual steps
- Create a vpc
- Create subnet1 and select the vpc created above

- Add three more subnets in the same way
- Create a new folder ‘vpc’ and add a file ‘vpc.json’ to it
- Now lets add the basic structure with Description and template version
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
}
- Now since we need to create vpc, lets add resources section
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
"Resources": {
}
}
- Lets find aws vpc cloudformation resource docs Refer Here and also look into resources section from here and then fill the template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
"Resources": {
"myvpc": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "myvpc"
}
]
}
}
}
}
- Hint: If you have installed vscode extension as mentioned above, you get help (intellisense) from vscode

- Now we need to add subnets, As shown below, we need vpc id

- Lets add one subnet resource Refer Here.
- While creating template for subnet i need to give vpc id which is not yet created/known i.e. resource with subnet1 id is depending on resource with myvpc id
"subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.0.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet1"
}
],
"VpcId":
}
}
- Now lets look at documentation of vpc cf resource and navigate to Return Values section

- According to docs lets refer Ref section over here
- So adding reference to myvpc in subnet1
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
"Resources": {
"myvpc": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "myvpc"
}
]
}
},
"subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.0.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet1"
}
],
"VpcId": {
"Ref": "myvpc"
}
}
}
}
}
- Now adding other 3 subnets to cf template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
"Resources": {
"myvpc": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "myvpc"
}
]
}
},
"subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.0.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet1"
}
],
"VpcId": {
"Ref": "myvpc"
}
}
},
"subnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.1.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet2"
}
]
}
},
"subnet3": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.2.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet3"
}
]
}
},
"subnet4": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.3.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet4"
}
]
}
}
}
}
- Now execute this tempalte and look into results

- Now if we were asked to create subnet1 in AZ -a subnet2 in Az B etc we need to understand the impact of the change which is mentioned in Update Requires section in Parameter

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is written for learning and it creates vpc with 4 subnets",
"Resources": {
"myvpc": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "myvpc"
}
]
}
},
"subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.4.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet1"
}
],
"VpcId": {
"Ref": "myvpc"
},
"AvailabilityZone": "ap-south-1a"
}
},
"subnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.5.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet2"
}
]
,
"AvailabilityZone": "ap-south-1b"
}
},
"subnet3": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.6.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet3"
}
]
,
"AvailabilityZone": "ap-south-1c"
}
},
"subnet4": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "myvpc"
},
"CidrBlock": "10.0.7.0/24",
"Tags": [
{
"Key": "Name",
"Value": "subnet4"
}
]
,
"AvailabilityZone": "ap-south-1a"
}
}
}
}
- Now if we execute this, new subnets will be created when the stack is updated

- After updating the stack succesfully compare subnet ids

Summary
- Format Version
- Resources
- Visual Studio Code Extension
- Reference
- Update Requires