Part 2 of the Core Foundations series.

A Virtual Private Cloud (VPC) is your own isolated network inside an AWS Region. EC2 instances, RDS databases, load balancers, and Lambda functions you attach to it all get IP addresses in a VPC, and the VPC decides what can talk to what.

Every Region in a new account comes with a default VPC, which is why you can launch an EC2 instance without thinking about networking. That’s convenient for experiments, but you should understand what it’s doing for you before you rely on it.

The building blocks

CIDR range

A VPC has an IPv4 address range written in CIDR notation, such as 10.0.0.0/16. The /16 means the first 16 bits are fixed, leaving 65,536 addresses. Choose a private range (RFC 1918) that won’t overlap with your other VPCs or your office network. Overlapping ranges make connecting networks later painful.

Subnets

A subnet is a slice of the VPC’s range that lives in one Availability Zone, for example 10.0.1.0/24 in us-east-1a. AWS reserves five addresses in every subnet, so a /24 gives you 251 usable addresses.

A resilient design uses at least two AZs, typically with a public and a private subnet in each:

VPC 10.0.0.0/16
├── us-east-1a
│   ├── public  10.0.0.0/24   (load balancer)
│   └── private 10.0.10.0/24  (app servers, databases)
└── us-east-1b
    ├── public  10.0.1.0/24
    └── private 10.0.11.0/24

Route tables

Every subnet is associated with a route table, and that, not the subnet’s name, is what makes it public or private. Every route table includes a local route, so everything inside the VPC can reach everything else (subject to security groups).

Internet gateway

An internet gateway connects the VPC to the internet. A subnet is public if its route table sends 0.0.0.0/0 to the internet gateway. Instances in it also need a public IP address to be reachable.

NAT gateway

Instances in private subnets often still need outbound internet access, for operating system updates or calling external APIs, without being reachable from the internet. A NAT gateway in a public subnet provides that: the private route table sends 0.0.0.0/0 to the NAT gateway.

Watch the cost. A NAT gateway is billed per hour and per GB processed, whether you use it or not. It’s the single most common surprise on learners’ bills. Delete it when you’re done.

VPC endpoints

VPC endpoints let resources in private subnets reach AWS services without going over the internet. Gateway endpoints for S3 and DynamoDB are free and keep that traffic off your NAT gateway, so add them to almost every VPC.

Security groups vs network ACLs

Two layers filter traffic, and they behave differently:

Security group Network ACL
Applies to An elastic network interface (an instance, a load balancer…) A subnet
Rules Allow only Allow and deny
State Stateful: replies are allowed automatically Stateless: return traffic needs its own rule
Evaluation All rules considered together Numbered rules, first match wins

In practice, security groups do most of the work. Leave the default network ACL (allow all) unless you have a specific reason to block a range at the subnet level.

A powerful habit: security group rules can reference other security groups instead of IP ranges. “The database allows port 5432 from the app-server security group” keeps working however many instances you add or replace.

Hands-on: build a two-tier VPC

  1. In the VPC console, choose Create VPC → VPC and more. The wizard draws the resulting network as you change options, which is a great way to learn.
  2. Choose 2 AZs, 2 public subnets and 2 private subnets, NAT gateways: None (to keep it free), and an S3 gateway endpoint.
  3. Inspect what was created: open each route table and see which subnets use the internet gateway and which don’t.
  4. Launch a small instance in a public subnet with a security group allowing nothing inbound, then try connecting with Session Manager (covered in the EC2 post).

When you’re done, delete the VPC. The console removes the subnets, route tables and gateways it created with it.

Gotchas

  • Public IPv4 addresses cost money. AWS charges hourly for every public IPv4 address, including ones attached to running instances. Don’t assign them where you don’t need them.
  • Plan CIDR ranges up front. You can add secondary ranges later, but you can’t change the primary one.
  • “Private” means no route from the internet, not “secure”. Security groups still matter.
  • Lambda in a VPC only has internet access through a NAT gateway (or VPC endpoints). Don’t put functions in a VPC unless they need to reach something there.

Next: EC2: virtual servers, properly.

← Series overview