Introduction
In this blog, we’re going to create a three-tier VPC network from scratch. We’ll start by building the VPC, building and attaching an Internet gateway, and building six different subnets inside our VPC:
- Two DMZ layers
- Two App layers
- Two Database layers
Video Tutorial for building a VPC from scratch
Next, we’re going to split these pairs of subnets across two different Availability Zones — the bare minimum we always want to do for highly available and fault-tolerant architecture in AWS.
Then we’re going to create two different route tables:
- A route to the Internet for our public subnets or subnets we want to have access to the open Internet.
- A route to the NAT gateway so that anything placed in our private subnets will have a route to update software from the open Internet.
Finally, we’ll add some security to our subnets with three Network Access Control Lists (NACLs), which we’ll assign to our pairs of subnet layers.
Solution
Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you’re using the US-east-1 region.
Create a VPC
- Select All services.
- Click VPC under Networking & Content Delivery.
- In the left sidebar menu, click Your VPCs.
- Click Create VPC.
- Enter the following values for Create VPC:
- Name tag: Enter prod-vpc.
- Pv4 CIDR block range: Enter 10.10.0.0/16.
- Leave the IPv6 CIDR block and Tenancy fields as their default values.
- Click Create VPC.
Creating Subnets in our VPC
1. First DMZ Layer
- In the left sidebar menu, click Subnets.
- Click Create subnet.
- Enter the following values for Create subnet:
- VPC ID: Select prod-vpc.
- Name tag: Enter DMZpublic1.
- Availability Zone: Select US East (N. Virginia)/us-east-1a.
- IPv4 CIDR block range: Enter 10.10.1.0/24.
- Click Create subnet.
2. Second DMZ Layer
- Click Create subnet.
- Enter the following values for Create subnet:
- VPC ID: Select prod-vpc.
- Name tag: Enter DMZpublic2.
- Availability Zone: Select US East (N. Virginia)/us-east-1b.
- IPv4 CIDR block range: Enter 10.10.2.0/24.
- Click Create subnet.
3. First App Layer
- Click Create subnet.
- Enter the following values for Create subnet:
- PC ID: Select prod-vpc.
- Name tag: Enter App-private1.
- Availability Zone: Select US East (N. Virginia)/us-east-1a.
- IPv4 CIDR block range: Enter 10.10.3.0/24.
- Click Create subnet.
4. Second App Layer
- Click Create subnet.
- Enter the following values for Create subnet:
- VPC ID: Select prod-vpc.
- Name tag: Enter App-private2.
- Availability Zone: Select US East (N. Virginia)/us-east-1b.
- IPv4 CIDR block range: Enter 10.10.4.0/24.
- Click Create subnet.
4. First Database Layer
- Click Create subnet.
- Enter the following values for Create subnet:
- VPC ID: Select prod-vpc.
- Name tag: Enter DB-private1.
- Availability Zone: Select US East (N. Virginia)/us-east-1a.
- IPv4 CIDR block range: Enter 10.10.5.0/24.
- Click Create subnet.
6. Second Database Layer
- Click Create subnet.
- Enter the following values for Create subnet:
- VPC ID: Select prod-vpc.
- Name tag: Enter DB-private2.
- Availability Zone: Select US East (N. Virginia)/us-east-1b.
- IPv4 CIDR block range: Enter 10.10.6.0/24.
- Click Create subnet.
NOTE: We’ve now created all six subnets. We should have three subnets each in the us-east-1a
Availability Zone and the us-east-1b
Availability Zone.)*
NOTE: Whether we labelled these subnets public or private doesn’t actually make them public or private — it’s just a naming construct. We’ll actually make them public or private in a bit when we route them to a public or private route table.
Create Internet Gateway
- In the left sidebar menu, click Internet Gateways.
- For the Name tag, enter “prod-vpc-IGW”.
- Click Create internet gateway.NOTE: Once it’s created, you’ll see its State says Detached. Even though it’s been created, it isn’t part of the VPC yet. Let’s fix that.
- Click Actions at the top of the screen.
- From the dropdown menu, click Attach to VPC.
- For Avaliable VPCs, select prod-vpc.
- Click Attach internet gateway.
NOTE: The state should now say Attached.
Creating NAT Gateway
Let’s create –
1. NAT Gateway
- In the left sidebar menu, click NAT Gateways. (NOTE: If there’s one already in your account, you can ignore it; we’re still going to create a new one.)
- Click Create NAT Gateway.
- Enter the following for Create NAT gateway:
- Name: Enter prod-NAT.
- Subnet: Select DMZpublic2.
- Connectivity type: Select Public.
- Elastic IP allocation ID: Select Allocate Elastic IP.
- Click Create NAT gateway.
2. Public Route Table
- In the left sidebar menu, click Route Tables. (NOTE: A route table will already exist — when we created the VPC, it created a default route table. But we’re going to create two new route tables.)
- Click Create route table.
- Enter the following for Create route table:
- Name tag: Enter prod-vpc-public-route.
- VPC: Select prod-vpc.
- Click Create route table.
3. Private Route Table
- Click Create route table.
- Enter the following for Create route table:
- Name tag: Enter prod-vpc-private-route.
- VPC: Select prod-vpc.
- Click Create route table.
On their own, route tables don’t do anything — we have to give them routes to something. For the public route table, we need to. For the private route table, we need to provide a route to the NAT gateway.
Provide Route from the Public Route Table to the Internet Gateway
- Select prod-vpc-public-route.
- Click on the Routes tab.
- Click Edit routes.
- Click Add route.
- For Target, select Internet Gateway.
- For Destination, select 0.0.0.0/0.
- Click Save changes.
We’ve now created a route from our public route table through the internet gateway into the open internet.
Provide Route from the Private Route Table to the NAT Gateway
- Select prod-vpc-private-route.
- Click on the Routes tab.
- Click Edit routes.
- Click Add route.
- For Target, select NAT Gateway.
- For Destination, select 0.0.0.0/0.
- Click Save changes.
Associate Public Subnets with the Public Route Table
- Click prod-vpc-public-route.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the DMZpublic1 and DMZpublic2 subnets.
- Click Save associations.
Associate Private Subnets with the Private Route Table
- Click private-route.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the following subnets:
- App-private1
- App-private2
- DB-private1
- DB-private2
- Click Save associations.
Now, anything placed inside the public route table has a route to the internet gateway, and anything placed inside the private route table has a route to the NAT gateway.
If we have databases or EC2 instances located inside these private subnets, they can get updates from the open internet by going through the NAT gateway, which provides an extra layer of security. Essentially, it’s a one-way street: The resources in the private subnets can access the open internet, but the open Internet cannot access the resources in the private subnets (unless we explicitly allow it).
We’re almost done with this lab. Before we wrap things up, let’s add another layer of security to our VPC by creating an NACL — a sort of firewall for controlling traffic in and out of one or more subnets — for each of our layers.
Createing Three NACLs and Associating them with Subnets
Lets create –
1. DMZ NACL
- In the left sidebar menu, click Network ACLs. (NOTE: We should see a default NACL, similar to route tables. The default NACL was created when we created our VPC. But we’re going to create three new ones.)
- Click Create network ACL.
- For Name, enter DMZ-prod-vpc.
- For VPC, select prod-vpc.
- Click Create network ACL.
2. App NACL
- Click Create network ACL.
- For Name, enter APP-prod-vpc.
- For VPC, select prod-vpc.
- Click Create network ACL.
3. DB NACL
- Click Create network ACL.
- For Name, enter DB-prod-vpc.
- For VPC, select prod-vpc.
- Click Create network ACL.
Associate Subnets with NACLs
- Select DMZ-prod-vpc.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the DMZpublic1 and DMZpublic2 subnets.
- Click Save changes.
NOTE: Now traffic coming in and out of these subnets will be subject to the inbound and outbound rules we set up on this particular NACL. We’re not going to set up any rules as part of this lab — right now, we’re just building the infrastructure and a shell we could put resources in.
Finish NACLs for the Remaining Layers
- Click APP-prod-vpc.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the App-private1 and App-private2 subnets.
- Click Save changes.
- Click DMZ-prod-vpc.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the DB-private1 and DB-private2 subnets.
- Click Save changes.
Conclusion
Congratulations! You’ve just built a three-tier VPC networking architecture inside AWS. For more such content please subscribe to our blog and our youtube channel.