Getting Started with Cloud Formation
Create a new directory with some json file
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "trying to create sample arch",
"Resources": {
}
}
Now lets add the basic resource syntax
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "trying to create sample arch",
"Resources": {
"myVpc" : {
"Type": "",
"Description": ""
Properties: {
}
}
}
}
Now open the cf resource for VPC from here and update the above JSON
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "trying to create sample arch",
"Resources": {
"myVPC": {
"Description": "This is VPC",
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock" : "10.100.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "From CF"
}
]
}
}
}
}
Now create a CF stack by using the above JSON and check the vpc page after success
Create a subnet. In the properties section of subnet VPC id has to be passed. In Cloud formation whenever a resource is created it will also give return values, to use VPC id we would leverage "Reference". To use Ref
{"Ref": "<name of the resource>"}
Name of the resource for VPC is "myVPC" and that would be used as shown below
{"Ref": "myVPC"}
The whole file after adding one subnet appears as shown below
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "trying to create sample arch",
"Resources": {
"myVPC": {
"Description": "This is VPC",
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock" : "10.100.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "From CF"
}
]
}
},
"subnet1": {
"Description": "first subnet",
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": "us-west-2a",
"CidrBlock" : "10.100.0.0/24",
"Tags": [
{
"Key": "Name",
"Value": "Subnet1"
}
],
"VpcId": { "Ref": "myVPC" }
}
}
}
}
Add three more subnets and update the stack with the following template
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "trying to create sample arch",
"Resources": {
"myVPC": {
"Description": "This is VPC",
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock" : "10.100.0.0/16",
"EnableDnsHostnames": true,
"Tags": [
{
"Key": "Name",
"Value": "From CF"
}
]
}
},
"subnet1": {
"Description": "first subnet",
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": "us-west-2a",
"CidrBlock" : "10.100.0.0/24",
"Tags": [
{
"Key": "Name",
"Value": "Subnet1"
}
],
"VpcId": { "Ref": "myVPC" }
}
},
"subnet2": {
"Description": "first subnet",
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": "us-west-2b",
"CidrBlock" : "10.100.1.0/24",
"Tags": [
{
"Key": "Name",
"Value": "Subnet2"
}
],
"VpcId": { "Ref": "myVPC" }
}
},
"subnet3": {
"Description": "first subnet",
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": "us-west-2c",
"CidrBlock" : "10.100.2.0/24",
"Tags": [
{
"Key": "Name",
"Value": "Subnet3"
}
],
"VpcId": { "Ref": "myVPC" }
}
},
"subnet4": {
"Description": "first subnet",
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": "us-west-2a",
"CidrBlock" : "10.100.3.0/24",
"Tags": [
{
"Key": "Name",
"Value": "Subnet4"
}
],
"VpcId": { "Ref": "myVPC" }
}
}
}
}
If you preview the change-set