Creating An Amazon Virtual Private Cloud (VPC) With AWS Cloud Formation
Creating An Amazon Virtual Private Cloud (VPC) With AWS Cloud Formation
Overview
This lab shows how to create an Amazon Virtual Private Cloud (VPC) using AWS
CloudFormation.
You will walkthrough sections of an AWS CloudFormation template and examine the
resources that are deployed. You will also learn how to perform updates through
CloudFormation.
Topics covered
To successfully complete this lab, you should be familiar with Amazon Virtual Private
Cloud (VPC) concepts.
Introducing the Technologies
AWS CloudFormation
Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated
section of the AWS cloud where you can launch resources within a virtual network. You
have complete control over your virtual networking environment, including selection of
your own IP address range, creation of subnets, and configuration of route tables and
network gateways.
You can easily customize the network configuration for your virtual private cloud. For
example, you can create a public-facing subnet for your web servers that has access
to the Internet and place your backend systems such as databases or application
servers in a private-facing subnet with no Internet access. You can leverage multiple
layers of security, including security groups and network access control lists, to help
control access to Amazon EC2 instances in each subnet.
Start Lab
1. At the top of your screen, launch your lab by clicking Start Lab
This will start the process of provisioning your lab resources. An estimated amount of
time to provision your lab resources will be displayed. You must wait for your resources
to be provisioned before continuing.
If you are prompted for a token, use the one distributed to you (or credits you have
purchased).
2. Open your lab by clicking Open Console
This will automatically log you into the AWS Management Console.
9. On the Review page:
Review the configuration
Click Create stack
The stack status will be CREATE_IN_PROGRESS until the resources have been
created.
While you are waiting, look in the Events tab to view the work being performed by
CloudFormation.
If you do not see the Events tab, it is because your web browser window is too narrow.
Either make the window wider until the tabs appear, or click the pull-down arrow in
the Overview tab.
10. Click the Stack info tab.
11. Wait until the status changes to CREATE_COMPLETE. Click the Refresh icon
every 30 seconds to update the status.
When the stack status is CREATE_COMPLETE, it means that the resources have been
created.
12. Click the Resources tab.
A list of resources is displayed. These resources will be explained in the next task. You
may need to refresh the screen to see the resources.
Task 2: Examine the VPC
In this task, you will examine the VPC resources that were created together with the
code from the CloudFormation template that created the resources.
AWSTemplateFormatVersion: 2010-09-09
Description: Deploy a VPC
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: true
Tags:
- Key: Name
Value: Lab VPC
This code is in YAML format. AWS CloudFormation can also accept code in JSON
format. If you are more familiar with JSON, you can download the JSON version of this
template.
The Type parameter in the above code declares the type of resources being created by
CloudFormation. The Properties section then specifies more information about the
resource to create. In this case, it defines:
CidrBlock: The IP address range associated with the VPC.
EnableDnsHostnames: Configures the VPC to associate DNS names with
Amazon EC2 instances.
Tags: Adds a friendly name to the resource.
Each type of resource has a different set of properties that can be used.
Here is the code from the CloudFormation template that created this Internet Gateway:
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: Lab Internet Gateway
In the management console, the Internet Gateway is showing that it is attached to the
VPC. This was done with this code in the CloudFormation template:
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
A VPC Gateway Attachment creates a relationship between a VPC and a gateway,
such as this Internet Gateway.
Notice that the template refers to other elements in the template with the !Ref keyword,
followed by the name of the other resource. This makes it easy to build resources that
link to each other simply by referencing their name.
18. In the left navigation pane, click Subnets.
Two subnets will appear:
Public Subnet 1 is connected to the Internet via the Internet Gateway and can
be used by resources that need to be publicly accessible.
Private Subnet 1 is not connected to the Internet. Any resources in this subnet
cannot be reached from the Internet, thereby providing additional security around these
resources.
Here is the code from the CloudFormation template that created the subnets:
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.0/24
AvailabilityZone: !Select
- '0'
- !GetAZs ''
Tags:
- Key: Name
Value: Public Subnet 1
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: !Select
- '0'
- !GetAZs ''
Tags:
- Key: Name
Value: Private Subnet 1
The properties are:
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: Public Route Table
There is also similar code for the Private Route Table.
Here is the code that defined the route to the Internet within the Public Route Table:
PublicRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
The configuration for the route is:
PublicSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRouteTable
It declares that Public Subnet 1 is associated with the Public Route Table.
In additional to creating resources, CloudFormation can provide information about the
resources that have been created.
AZ1:
Description: Availability Zone 1
Value: !GetAtt
- PublicSubnet1
- AvailabilityZone
The VPC output is simply a reference to the VPC, which results in the VPC ID being
displayed.
Once a CloudFormation stack has been deployed, it is recommended that any changes
to the resources should be made through CloudFormation rather than by directly
modifying the resources.
In this task, you will update the stack with a new CloudFormation template that defines
the following resources:
Additional public and private subnets have been added in another Availability Zone.
This is best practice to ensure that your resources can run in multiple data centers
(Availability Zones) to ensure High Availability in case of system failures.
26. Right-click this link to download an updated CloudFormation template to your
computer: vpc-2.yaml
(A JSON version of this template is also available.)
27. Click Update , then configure:
Select Replace current template
Click Upload a template file
Click Choose file
Select the vpc-2.yaml template you just downloaded.
28. Click Next
29. Click Next
You will accept the default Options.
In this task, you will view the template using the AWS CloudFormation Designer.
AWS CloudFormation Designer (Designer) is a graphic tool for creating, viewing, and
modifying AWS CloudFormation templates. With Designer, you can diagram your
template resources using a drag-and-drop interface, and then edit their details using the
integrated JSON and YAML editor. Whether you are a new or an experienced AWS
CloudFormation user, AWS CloudFormation Designer can help you quickly see the
interrelationship between a template's resources and easily modify templates.
37. On the Services menu, click CloudFormation.
38. Click the Lab stack.
39. Click the Template tab.
40. Click View in Designer
The top portion of the window provides a graphical overview of the VPC that is defined
by the template.
41. Use the Zoom controls to examine the diagram. You can move the diagram by
dragging the image.
The lower portion of the window displays the code within the template that defines the
resource.
Arrows show the relationship between resources, such as Route Tables that are
associated with Subnets.
When the stack has been deleted, it will disappear from the list.
Conclusion
Follow these steps to close the console, end your lab, and evaluate the experience.
Additional Resources