Configuring A VPC
Configuring A VPC
Lab overview
Amazon Virtual Private Cloud (Amazon VPC) gives you the ability to provision a
logically isolated section of the Amazon Web Services (AWS) Cloud where you can
launch AWS resources in a virtual network that you define. You have complete
control over your virtual networking environment, including selecting your IP
address ranges, creating subnets, and configuring route tables and network
gateways.
In this lab, you build a virtual private cloud (VPC) and other network components
required to deploy resources, such as an Amazon Elastic Compute Cloud (Amazon EC2)
instance.
Following diagram showing the final lab architecture having a vpc, a public and a
private subnet with ec2 instance in each and a nat gateway in public subnet. Both
subnets are hosted in a single availability zone. Diagram also shows routing table
associate with each subnet.
Objectives
By the end of this lab, you should be able to do the following:
Create a VPC with a private and public subnet, an internet gateway, and a NAT
gateway.
Configure route tables associated with subnets to local and internet-bound traffic
by using an internet gateway and a NAT gateway.
If you have time, you can complete the optional challenge section in which you
create an Amazon EC2 instance in a private subnet and connect to it through the
bastion server.
Duration
This lab will require approximately 45 minutes to complete.
Wait until the message "Lab status: ready" appears, and then choose X to close the
Start Lab panel.
At the top of these instructions, choose AWS to open the AWS Management Console on
a new browser tab. The system automatically signs you in.
Tip: If a new browser tab does not open, a banner or icon at the top of your
browser will indicate that your browser is preventing the site from opening pop-up
windows. Choose the banner or icon, and choose Allow pop-ups.
Arrange the AWS Management Console so that it appears alongside these instructions.
Ideally, you should be able to see both browser tabs at the same time to follow the
lab steps.
On the AWS Management Console, in the Search bar, enter and choose VPC to go to the
VPC Management Console.
In the left navigation pane, for Virtual private cloud, choose Your VPCs.
In every Region, a default VPC with a Classless Inter-Domain Routing (CIDR) block
of 172.31.0.0/16 has already been created for you. Even if you haven't created
anything in your account yet, you will see some pre-existing VPC resources already
there.
At the top of the page, a message displays similar to the following: "You
successfully created vpc-NNNNNNNNNNN / Lab VPC."
Choose Save.
EC2 instances launched into the VPC now automatically receive a public IPv4 Domain
Name System (DNS) hostname.
Availability Zone: Choose the first Availability Zone in the list. Do not choose No
preference.
You now configure the public subnet to automatically assign a public IP address for
all EC2 instances that are launched within it.
Choose Save.
Even though this subnet has been named Public Subnet, it is not yet public. A
public subnet must have an internet gateway, which you attach in a task later in
the lab.
To create the private subnet, repeat the steps from the previous task, and choose
the following options:
Availability Zone: Choose the first Availability Zone on the list. Do not choose No
preference.
The CIDR block of 10.0.2.0/23 includes all IP addresses that start with 10.0.2.x
and 10.0.3.x. This range is twice as large as the public subnet because most
resources should be kept in private subnets unless they specifically need to be
accessible from the internet.
Your VPC now has two subnets. However, the VPC is totally isolated and cannot
communicate with resources outside the VPC.
Next, you configure the public subnet to connect to the internet through an
internet gateway.
In the left navigation pane, for Virtual private cloud, choose Internet gateways.
Choose Create internet gateway, and then for Name tag, enter Lab IGW.
Your public subnet now has a connection to the internet. However, to route traffic
to the internet, you must also configure the public subnet's route table so that it
uses the internet gateway.
Add a route to the route table to direct internet-bound traffic to the internet
gateway.
In the left navigation pane, for Virtual private cloud, choose Route tables.
Select the route table that includes Lab VPC in the VPC column.
Tip: If you cannot see the VPC column, scroll to the right.
In the Name column, choose the edit icon, enter Private Route Table for Edit Name,
and then choose Save.
There is currently only one route. It shows that all traffic destined for
10.0.0.0/16 (which is the range of the Lab VPC) will be routed locally. This option
allows all subnets within a VPC to communicate with each other.
You now create a new public route table to send public traffic to the internet
gateway.
After the route table is created, in the Routes tab, choose Edit routes.
Note: You now add a route to direct internet-bound traffic (0.0.0.0/0) to the
internet gateway.
Choose Add route and then configure the following options:
Target: Choose Internet Gateway, and then choose Lab IGW from the list.
The final step is to associate this new route table with the public subnet.
The public subnet is now public because it has a route table entry that sends
traffic to the internet through the internet gateway.
In the previous tasks, you created a VPC and attached an internet gateway. Then you
created subnets and a route table and associated a public route table to the public
subnet. You now launch resources in the subnets as required.
In this task, you launch an EC2 instance bastion server in the public subnet that
you created earlier.
On the AWS Management Console, in the Search bar, enter and choose EC2 to go to the
EC2 Management Console.
In the Application and OS Images (Amazon Machine Image) section, configure the
following options:
In the Key pair (login) section, choose Proceed without a key pair (Not
recommended).
You use EC2 Instance Connect to access the shell running on the EC2 instance, so a
key pair is not needed in the lab.
In the Network settings section, choose Edit and configure the following options:
The EC2 instance named Bastion Server is initially in a Pending state. The
Instance state then changes to Running to indicate that the instance has finished
booting.
Continue to the next task. You do not need to wait for the instance to be running.
On the AWS Management Console, in the Search bar, enter NAT gateways, choose the
Features list, and choose NAT gateways.
You now configure the private subnet to send internet-bound traffic to the NAT
gateway.
In the left navigation pane, choose Route tables, and then select Private Route
Table.
Target: Choose NAT Gateway, and then choose nat- from the list.
Resources in the private subnet that wish to communicate with the internet now have
their network traffic directed to the NAT gateway, which forwards the request to
the internet. Responses flow through the NAT gateway back to the private subnet.
In this optional challenge, you launch an EC2 instance in the private subnet and
confirm that it can communicate with the internet.
Follow the instructions that you used to launch the bastion server, and configure
the following options:
In the Application and OS Images (Amazon Machine Image) section, configure the
following options:
In the Key pair (login) section, choose Proceed without a key pair (Not
recommended).
In the Network settings section, choose Edit and configure the following options:
Expand the Advanced Details section, and for User data - optional, paste the
following script:
#!/bin/bash
# Turn on password authentication for lab challenge
echo 'lab-password' | passwd ec2-user --stdin
sed -i 's|[#]*PasswordAuthentication no|PasswordAuthentication yes|g'
/etc/ssh/sshd_config
systemctl restart sshd.service
This script permit login by using a password. It is included to help make the lab
steps shorter but is not recommended for normal instance deployments.
On the AWS Management Console, in the Search bar, enter and choose EC2 to open the
EC2 Management Console.
Choose Connect.
Note: If you prefer to use an SSH client to connect to the EC2 instance, see the
guidance to Connect to Your Linux Instance.
You now connect to the private instance, which is placed in the private subnet.
In the Amazon EC2 console, choose Instances, and select Private Instance (and clear
any other instances).
Copy the Private IPv4 addresses (shown in the lower half of the page) to your
clipboard.
ssh PRIVATE-IP
The command that you run should look similar to the following: ssh 10.0.2.123
If you are prompted with the message "Are you sure you want to continue
connecting", enter yes
You should now be connected to the private instance. You accomplished this task by
first connecting to the bastion server (in the public subnet) and then connecting
to the private instance (in the private subnet).
ping -c 3 amazon.com
You should see results similar to the following:
Conclusion
Congratulations! You now have successfully done the following:
Created a VPC with a private and public subnet, an internet gateway, and a NAT
gateway
Configured route tables associated with subnets to local and internet-bound traffic
by using an internet gateway and a NAT gateway