Using Cloudformation to Create a Virtual Private Cloud in AWS

By | 2016-04-21

Cloudformation

Creating your network infrastructure in AWS is simplified by a service called Cloudformation. Cloudformation allows you to specify your network subnets, groups, and other resources in a JSON file. When you submit that JSON file to AWS, the service will create the resources in your AWS account. This article demonstrates Cloudformation by creating a basic virtual private cloud (VPC) with an accompanying subnet and gateway. You will need an AWS account to test this out. Preferably you would also have read about using the command line AWS client.

A Cloudformation template is a JSON file that describes each resource in your network. Every resource has a specific set of attributes that you can define within this template. Amazon documents all resources and their attributes on its on site as well, so take a look at it for more complete details.

The general structure of a template is a basic map of resources within a “Resources” id. Each resource has a Type and various Properties. A VPC resource that defines a set of IP addresses in a 10.0.0.0/28 block, looks like this:

{
  "Resources": {
    "VPC01": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/28",
        "Tags": [ 
          {
            "Key": "Name",
            "Value": "vpc-charprop"
          }]
      }
    },
    ...
}

Within a Cloudformation template, you can refer to other resources using their logical id. For example, you can associate a subnet with “VPC01” with the following declaration:

  "SUBNET01": {
    "Type": "AWS::EC2::Subnet",
    "Properties": {
      "VpcId": {
        "Ref": "VPC01"
      },
      "CidrBlock": "10.0.0.0/28",
      "Tags": [
        {
          "Key": "Name",
          "Value": "subnet-charprop-public"
        }
      ]
    }
  },

I’ve placed these and other resources into a larger template called network.json. Using this file, you submit your resource creation request with the AWS CLI with this simple command:

aws cloudformation create-stack --stack-name charprop-network --template-body file://./network.json

Assuming network.json is in your current working directory, the command should return immediately, showing a JSON description something like this:

{
  "StackId": "arn:aws:cloudformation:us-west-2:446581796491:stack/charprop-network/c3aa1530-0848-11e6-a533-50a68a2012ba"
}

You’ve successfully created a VPC and subnet using Cloudformation!

In the next article, I’ll add security groups and a Linux machine instance.

3 thoughts on “Using Cloudformation to Create a Virtual Private Cloud in AWS

  1. Maritza

    If you are interested in topic: how to earn money online in pakistan at home – you should
    read about Bucksflooder first

  2. 94Margery

    Hello blogger, i must say you have high quality posts here.
    Your blog should go viral. You need initial traffic only.
    How to get it? Search for: Mertiso’s tips go viral

Leave a Reply

Your email address will not be published. Required fields are marked *