- Cloud Security Lab a Week (S.L.A.W)
- Posts
- Build a VPC from Scratch
Build a VPC from Scratch
In today's lab we will create a bare-bones VPC piece by piece to learn what you need, what you don't, and how it works.
Prerequisites
A workload account
The Lesson
I’m not here to teach you how to be a cloud network engineer, but I think I need to teach you how to be a cloud network engineer. Sorry. 🐼
Information security’s origins are in network security. It’s still one of the bedrocks of our profession, even though there are many newer career paths which never involve touching a firewall. But if you think about cloud computing, one of the essential characteristics is that we do everything… over the network.
So why network engineering instead of just talking about security features? It’s simple: network design profoundly affects network security. Unlike a physical network, with cloud we have way more capabilities to tune our networks at no additional cost for better security design.
In Journey to the Center of the VPC I introduced a bunch of concepts around Software Defined Networks (SDNs) and how Amazon‘s implementation, the mighty VPC, functions. With that intro out of the way I want to cover a networking fundamental — Internet Gateways — before we start building a basic VPC by hand.
As we work through the lab I’ll explain each component, with a focus on the minimum required elements.
Pour some CIDR on RFC 1918
Classless Intern-Domain Routing (CIDR, and yes, it’s pronounced cider) is the standard notation we use to describe and define networks and subnets. When you see “192.168.0.0/16”, that’s CIDR notation. You can read the details at Wikipedia but here’s the short version.
CIDR notation defines an IP address range. It consists of an IP address (like 192.168.0.0) and a number after the / (often 24 or 16) which, combined with the base address, defines how large the network is and which IP addresses it includes. 192.168.0.0/16 is what we call a Class B network, including the IP addresses between 192.168.0.0 and 192.168.255.255 — a total of 65,536 addresses.
192.168.2.5/32 is a single IP address.
You’ll see in a minute how we use CIDR notation in building a VPC; we have a lot of flexibility to define subnets as we want. These are all IPv4 examples, but AWS also has options for IPv6, which we will get to… in a while.
So what’s that RFC 1918? I mentioned it briefly last week — it’s the specification for addresses reserved for private networks. These addresses are never routed over the Internet, and we can use them however we want for our own private networks. What if something needs to talk to the Internet? Stand by for our Network Address Translation lab.
There are 3 blocks of reserved private IP addresses:
10.0.0.0-10.255.255.255 (/8)
172.16.0.0-172.31.255.255 (/12)
192.168.0.0-192.168.255.255 (/16)
We will use both CIDR and RFC 1918 in a couple minutes.
Internet Gateways
VPCs are Software Defined Networks, where all the packets are wrapped and shipped around using Amazon’s internal software. But hey, at some point we want to, you know, talk to the Internet. And the Internet still uses old-school standard networking. So how the heck do we pull this off?
We use a special construct called an Internet Gateway. I assume this is massive hardware and not merely software, but an Internet Gateway is a thing you attach to your VPC which connects you, bidirectionally, to the Internet.
Internet Gateways connect your resources in a VPC to the Internet and handle translating from the AWS Software Defined Network to the rest of the Internet.
With an Internet Gateway we can assign Public IP addresses to our resources, including instances (virtual machines) via their Elastic Network Interfaces (ENI, the virtual “hardware” which emulates a network interface and cable). By default these are IPv4 public IP addresses within the huge range of Internet addresses AWS bought and owns. You get a random address from their pool, and it changes when you shut a resource down and start it up again. You can pay more to get a stable address, which we will talk about soon.
The cool thing about Internet Gateways is that they track and translate the software defined resources in AWS, and handle all the unwrapping and routing of packets from the VPC to the Internet, and back from the Internet to your virtual resource. How? I assume magic; and in my head I see unicorns in hamster wheels, which may indicate I need professional help.
Lesson Key Points
CIDR notation is how we define address ranges of networks and subnets.
RFC 1918 sets aside 3 address ranges for private networks.
Internet Gateways connect VPCs to the Internet, and allow us to assign public IP addresses to our resources (inside a VPC).
The Lab
We will build our first bare-bones VPC by defining our VPC, subnets, and an Internet Gateway. Along the way I’ll review some other smaller components.
Video Walkthrough
Step-by-Step
Sign into your portal and go to TestAccount1 with AdministratorAccess:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/99bf493e-3bf8-430b-ab81-302b7cc035f0/image.png?t=1721258747)
Then go to VPC, and make sure you are in us-west-2:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/9c39c368-aa82-4077-9b85-599eab01c255/image.png?t=1721258780)
You shouldn’t currently have a VPC in us-west-2 since we deleted it, so click Create VPC:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/03293ace-2cc9-429f-bf42-354313d5d00d/image.png?t=1721258917)
You’ll see a pretty screen with a nice diagram for a potential VPC to build, with lines and boxes visualizing everything.
We won’t use that. I’m kind of a jerk, so we will build everything by hand. Using a number 2 pencil. (Okay, not that last part.)
We only need to change 3 settings on this page. First select the VPC only option. Then name your VPC CloudSLAW. This is a really good habit. I’ve assessed a lot of environments where there were either no names on a VPC, or a meaningless name. That makes it much harder to know what’s going on as you build out dozens, hundreds, or thousands of these things.
Document your code and show your work, people!
The last change is to set our IPv4 CIDR block; we want to use 10.0.0.0/16, that class B network which gives us 65K+ addresses to play with. No, we don’t need that many, but it isn’t like we pay extra for the bigger network, so don’t overthink it.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/1f01888f-b721-497e-84cc-2409cb05123c/image.png?t=1721259679)
In case you were wondering, Tenancy can put you on dedicated hardware. You pay much more for it, so you need very particular requirements for it to make sense. Okay, now…
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/254a62ab-bce3-4160-8347-82d2b81c04f0/image.png?t=1721259961)
AWS created 3 components for you automatically since you used the console, which you would need to create manually using the command line or infrastructure as code (CloudFormation/Terraform). These are all required:
DHCP Option Set: This configures DNS and internal domain names. For our purposes we are fine with the defaults (feel free to look at them).
Route Table: We’ll look at this in a minute.
Main network ACL (Access Control List): This is a security construct we won’t use for a while, which I generally recommend you don’t touch until you know with certainty what you need to change. The default one AWS create allows all inbound and outbound traffic. I’ll mention these again when we talk about Security Groups, but for now just know that I rarely need to modify the default NACL; you usually only have to mess with them in complex enterprise network scenarios.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/c50570a0-e8d5-412b-b8b0-55e716480f3d/image.png?t=1721260328)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/4933e14b-f45a-4ddd-bd59-6c7f2afa1037/image.png?t=1721260798)
DHCP options, such as setting internal domain names
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/18b4651f-7886-4c3c-8f63-a381c9cf712a/image.png?t=1721260833)
Allows all inbound and outbound traffic
It’s great that we have a VPC, but we don’t really have a network yet. We need subnets for that. Go to Subnets > Create subnet:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ebb4a6ad-762b-4618-abfe-9e3c118e7719/image.png?t=1721261086)
Pick the CloudSLAW VPC. If you see anything else you haven’t been following my instructions:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/45e6f8ee-2e6e-434b-8cf5-eec2ed67f398/image.png?t=1721261184)
Change the following settings:
Name it slaw-public-1.
Choose us-west-2a for your Availability Zone. When you click that you’ll also see another designation like usw-az2. This is a weird thing where 2a is specific to your VPC, but az2 is the physical AZ. AWS used to not let you know which physical AZ you were in, but they had to add that over time as people started to connect more across VPCs and accounts, and needed to keep some things in the same physical datacenter.
Set 10.0.1.0/24 as the CIDR for the subnet. This gives us 256 potential IP addresses (not quite — AWS steals some for internal things it needs, so we only get 251 we can actually use ourselves).
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/d080d500-81ed-4054-8f8d-44c5baecf5ee/image.png?t=1721261544)
Then click Add new subnet to configure another one before we actually create it. Use these settings, then click Create subnet:
slaw-public-2
us-west-2b
10.0.2.0/24
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/1f282209-d4be-4172-953b-e16ac4abdb02/image.png?t=1721261658)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f00e0230-02a2-47fd-9c20-eba856c166e2/image.png?t=1721261755)
Success!
Click slaw-public-1 to review at the settings. Nothing too exciting. Then click on the route table to review that:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/2407ccae-efbf-4749-9c7a-ebaf65b70943/image.png?t=1721261852)
If you drill down you can see we only have the one route, which only sends traffic to anything with the same VPC. If you try to talk to the Internet there’s no place for the traffic to go.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/dcfa7ca3-ae99-4e26-b45c-59c578210809/image.png?t=1721261940)
One cool thing is that we can define routes between some subnets and not others, which would restrict traffic to only the allowed subnets… all without needing a firewall. In our case we want these to be public subnets which can talk to the Internet, so let’s Create internet gateway:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/9c70ab64-dcc3-4e25-a003-0ba853d1e844/image.png?t=1721262072)
Call it slaw-ig. These name tags really do help:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/911406c4-2212-4cea-9b44-e847b9c1eb04/image.png?t=1721262147)
We now have an Internet gateway, but we need to attach it to our VPC. Actions > Attach to VPC:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ad983534-7a56-4ddf-824a-9b59e3b19cbb/image.png?t=1721262264)
Select your CloudSLAW VPC, and then Attach internet gateway:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/3633ad8b-7a9e-409a-98a9-eccbc3a7ce07/image.png?t=1721262332)
If we go back into Your VPCs and click our VPC, you can see in the visualization that we have an Internet gateway, but it isn’t connected to anything.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f60acba0-5caf-4a4e-9c43-730340c0de00/image.png?t=1721262447)
To fix that we need to edit our route table to send traffic to the Internet gateway. Go to Route tables and pick the only one we have. Yes, as we add more we will absolutely use names. Perhaps you can feel my prior trauma oozing out of my pores, from all those assessments and repair jobs on consulting gigs where things were unnamed.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/d97efc89-e49a-465f-8ca7-8e51bcba157b/image.png?t=1721262586)
There are a few steps here. First we need to Add route, then we need to select the Destination of 0.0.0.0/0 which is CIDR-style notation for “The Internet”. Then click the Target dropdown to send the traffic to an Internet Gateway, and you’ll see the field fill in with igw-. Click that and select your slaw-ig gateway. Then Save changes:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/a5824a97-a8e2-4cd1-b492-15de328fad5b/image.png?t=1721262850)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/58af4167-6e6a-472c-827c-aa63b1fbf0b3/image.png?t=1721262897)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/dff7fa57-7140-4124-9850-10cef9be000d/image.png?t=1721262943)
Now local traffic routes within the VPC (but only to the destination, remember — this isn’t like a physical network where you can sniff traffic) and Internet traffic routes to the Internet gateway. BTW: “Propagated” means it is a route pushed into the table from someplace else. We don’t have someplace else yet, so that will always say No.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/cc24ef3e-9fa6-400f-8013-aace6503ac3c/image.png?t=1721263025)
If we look back in the Resource map, everything is now wired up! We have a VPC with 2 subnets which can talk to each other and the Internet. This is a fully functional VPC:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/6ea16a27-22ff-46d5-b662-e21ba2c884fc/image.png?t=1721263146)
I’d say more, but my dog is giving me a really big hint that it’s time for her walk. (Her brother is napping). Oh… without any network traffic this VPC is free. None of these constructs costs anything.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f047af1a-728d-448a-bc31-7dbc9120a0c5/IMG_4621.jpeg?t=1721263407)
Lab Key Points
A minimal VPC requires the VPC, a subnet, a route table, DHCP options set, and NACL.
To connect to the Internet use an Internet gateway.
Route tables define where traffic is… routed.
-Rich
Reply