Concepts of Cloud Formation
Templates
- It is a JSON or YAML formatted text in a file.
- In this we define the resources which we need in the specifications
- Sample Template in JSON
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This is my first cf template",
"Resources": {
"myvpc":{
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "192.168.0.0/16",
"Tags" : [
{
"Key": "Name",
"Value": "From CF"
}
]
}
},
"mysubnet1": {
"Type" : "AWS::EC2::Subnet",
"Properties":{
"AvailabilityZone": "us-west-2a",
"CidrBlock": "192.168.0.0/24",
"Tags":[
{
"Key": "Name",
"Value":"From CF"
}
],
"VpcId": { "Ref": "myvpc" }
}
},
"mysubnet2": {
"Type" : "AWS::EC2::Subnet",
"Properties":{
"AvailabilityZone": "us-west-2b",
"CidrBlock": "192.168.1.0/24",
"Tags":[
{
"Key": "Name",
"Value":"From CF"
}
],
"VpcId": { "Ref": "myvpc" }
}
},
"mysubnet3": {
"Type" : "AWS::EC2::Subnet",
"Properties":{
"AvailabilityZone": "us-west-2c",
"CidrBlock": "192.168.2.0/24",
"Tags":[
{
"Key": "Name",
"Value":"From CF"
}
],
"VpcId": { "Ref": "myvpc" }
}
}
}
}
Stacks
- Using Template we create a Stack.
- In Stack we manage resources created from template as a Single Unit
- Create, update, delete can be performed on the Stack
- Stacks are resources in same region, when we want the infrastructure to be created in multiple regions from one template => Stack Set
Change Set
- If we need to make changes in stack we need to mention the changes in the template and the difference in configuration between current stack and desired stack is the changeset
- AWS allows to create a changeset and then apply the changeset to the stack.
Summary
- We write Cloudformation Templates
- Execute them to create stacks
- Update the template, Create changeset and execute to make changes in the stack
- Delete the stack
Lets Create a VPC in AWS using CF
-
Navigate to CF
-
Now if you copy the template from designer
{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
"AWS::CloudFormation::Designer": {
"5d8906ad-15b6-4e90-9fdf-1290c0621262": {
"size": {
"width": 140,
"height": 140
},
"position": {
"x": 217,
"y": 66
},
"z": 0
}
}
},
"Resources": {
"EC2VPCOP16": {
"Type": "AWS::EC2::VPC",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "5d8906ad-15b6-4e90-9fdf-1290c0621262"
}
}
}
}
}
-
This approach of generating template is not workable
-
Lets try to Learn the Cloudformation by writing templates directly, Lets try to learn Cloudformation Template Syntax Refer Here
-
To Write Cloudformation Template Resources is the only section required
-
Lets understand basic cloudformation syntax in JSON
{
"AWSTemplateFormatVersion" : "version date",
"Description" : "JSON string",
"Metadata" : {
template metadata
},
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
"Conditions" : {
set of conditions
},
"Transform" : {
set of transforms
},
"Resources" : {
set of resources
},
"Outputs" : {
set of outputs
}
}
- Lets look at basic structure in YAML
---
AWSTemplateFormatVersion: "version date"
Description:
String
Metadata:
template metadata
Parameters:
set of parameters
Mappings:
set of mappings
Conditions:
set of conditions
Transform:
set of transforms
Resources:
set of resources
Outputs:
set of outputs
- For this example, lets use JSON to create empty template
{
"Resources" : {
}
}
- Now we need to find aws vpc resource syntax for that google
aws vpc cloudformation
- Now if we fill the vpc section and save the file as test.json
{
"Resources" : {
"myvpc": {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.10.0.0/16",
"Tags" : [{"Key": "Name", "Value": "FromCF" }]
}
}
}
}