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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)