ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Set up an AWS Virtual Private Cloud (VPC) with AWS CloudFormation

Updated on July 5, 2016

Introduction

This tutorial will step you through creating your own Virtual Private Cloud (VPC) on AWS using CloudFormation.

The VPC will eventually have four subnets. There will be two subnets in the first availability zone and the other two subnets in the second availability zone. We will attach 2 security groups to those subnets. One subnet in each availability zone will act as a public network and the second subnet in each zone will act as a private network.

That will be our whole infrastructure as code but we will start by building the VPC first.

What is AWS CloudFormation ?

CloudFormation is a service offered by AWS (Amazon Web Services) to allow the creation of infrastructure in AWS from a script. It is infrastructure-as-code. The CloudFormation service comes with a web based designer module, allowing you to drag and drop most AWS resources from a palette into a canvas and generating a basic JSON script for the resources. The JSON script can then be modified and configured before being saved as a template. The template can be used to create an infrastructure "Stack", which is basically any AWS resource and it's configurations as defined in the template.

There are also predefined templates in AWS which can be used to create infrastructure.

Pre-requisite Knowledge

It is assumed that the person following this tutorial ALREADY has experience setting up AWS VPCs and EC2 instances via the AWS Management Console. This tutorial is meant as a guide to help get started with using the CloudFormation service to create infrastructure from a script. If you can script your infrastructure, then you can version it. Even better, you can re-deploy an exact replica anywhere, anytime.

Start with the Design

I have drawn up a simple infrastructure.

We have one VPC in the design. VPC stands for Virtual Private Cloud, which is like your own data centre. You can have up to 5 VPCs in your Amazon account by default. You can request more if you really need to.

There are two availability zones being used in the design. Availability zones are AWS data centres located in different geographical locations in a given region e.g. US East (N. Virginia). It is a good idea to plan your resources over two availability zones for redundancy purposes.

Two security groups will be created. Security Groups are virtual firewalls. Security groups are applied to EC2 instances and NOT to subnets. Think of them as being like Windows Firewall, except with Security Groups, each one can be applied to multiple instances.

We will create one security group for instances that will be in the "Public" network and one for instances in the "Private" network regardless of which availability zones they are located in.

EC2 Instances are compute resources i.e. a virtual machine running either Linux or Windows in AWS. EC2 instances must live in a subnet.

We often apply the same security group to instances that are in the same subnet so that there is a similarity with what we have experienced in the physical infrastructure world i.e. a firewall with a LAN/VLAN behind it with servers connected to the LAN/VLAN. If we want to take full advantage of the flexibility of security groups then we can create security groups for EC2 instances with particular purposes.

AWS CloudFormation Resources Reference

In order to be able to create our template, we need to have access to the AWS CloudFormation Resources Reference. The reference lists the resources available to be used with CloudFormation and the properties that are available, along with examples.

The link to the reference is :

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html

Just open up the link in a new tab or a new browser and be ready to flip between the CloudFormation designer and this page.

Log into the AWS Management Console

From the list of AWS Services, select CloudFormation

Select Design Template

Template Designer

Adding the VPC Resource

We are going to add the VPC resource first to our canvas. VPC stands for Virtual Private Cloud.

Scroll the list of resources until we see the EC2 container. Expand the EC2 container and locate the VPC resource. The VPC resource can be dragged and dropped to the canvas by left clicking it, holding the mouse down and dragging it to the canvas.

AWS::EC2::VPC in the Resource Types Reference

Let's look for information on how to configure the properties for the VPC resource in the Resource Types Reference. In the Template Reference section, we open up the link to AWS::EC2::VPC .


JSON Script

CloudFormation uses the JSON format to structure its CloudFormation scripts. If we switch back to the Designer and look at the bottom pane under the properties tab, we can see the skeleton JSON script for the VPC.

{
 "Resources": {
  "EC2VPC466U2": {
      "Type": "AWS::EC2::VPC",
      "Properties": {}
    }
  }
}


Adding Properties to the JSON Script

Based on the information about the AWS::EC2::VPC resource, we can copy the required (compulsory) properties and the optional properties which we want to configure for the VPC.

In the below example, we need to assign a Cidr IP Block (required) to our VPC. We have the option to EnableDnsSupport and where we would like to create the VPC. VPCs can be created on the shared tenancy (default) or a dedicated hosting.

{
  "Resources": {
    "EC2VPC466U2": {
      "Type": "AWS::EC2::VPC",
      "Properties": {"CidrBlock" : "10.2.0.0/16",
      "EnableDnsSupport" : "true",
      "EnableDnsHostnames" : "",
      "InstanceTenancy" : "default",
      "Tags" : [{"Key":"Name","Value":"TestVPC"}]}
    }
  }
}

Saving the Template

Let's save the template. It's good to save often especially with this current version of the designer. I find that sometimes when I click on another tab and then return to my original position, all my work is gone.

Save the CloudForm template Locally

We can save the template to S3 or locally. Let's save it locally for our tutorial.

Testing the Template

We will now test what we've done so far. After saving, hit the Close button, and go to CloudFormation.

We will click on the Create Stack button on the upper left of the screen.

Create a VPC from the TestVPC Template

Will will now select the locally saved file TestVPC.template . Click on the Choose File button.

Click Next
Click Next
Enter TestVPC for the Stack Name. Then click Next.
Enter TestVPC for the Stack Name. Then click Next.
Leave the options as is and click Next.
Leave the options as is and click Next.
Click Create.
Click Create.

AWS VPC Created with CloudFormation

We have now finished creating an Amazon Web Services Virtual Private Cloud using CloudFormation. We can check that it's been created by using the AWS Management Console and looking for our TestVPC.

Note that the TestVPC is created with the name and Cidr IP Block that we had given it.
Note that the TestVPC is created with the name and Cidr IP Block that we had given it.

Summary

We have now created an Amazon Web Services Virtual Private Cloud (AWS VPC) using CloudFormation. We have created a simple script in JSON format and used CloudFormation to create an AWS Stack . An AWS Stack is a collection of AWS resources which can be managed as a single unit. A stack can be created, updated or deleted. In our case, the Stack we created was a Virtual Private Cloud.

In the next tutorials, we will update the stack by adding subnets and security groups.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article