Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 70 ยท Networking with VPC

VPC Fundamentals

Understand the Amazon VPC: CIDR blocks, subnets across Availability Zones, route tables, the internet gateway, and what makes a subnet public or private.

A Virtual Private Cloud (VPC) is your own private network inside AWS. When you launch an EC2 instance, a database or a load balancer, it lives in a VPC, and the VPC decides which addresses it gets, what it can reach and who can reach it. Understanding it early saves you from the most common beginner problem: "my server is running but I cannot connect to it."

A VPC is built from a few simple parts: an address range, subnets that slice it up, route tables that steer traffic, and gateways that connect it to the outside. This lesson explains each part and how they combine into a public or private subnet. All console and CLI steps are read-only; the runnable examples use Python's ipaddress module to do the real address arithmetic, which is the same math AWS uses, so those parts are genuine rather than stand-ins.

The address range: CIDR blocks

When you create a VPC you choose a CIDR block, a range of private IPv4 addresses written like 10.0.0.0/16. The number after the slash is how many leading bits are fixed. A /16 leaves 16 bits free, which is 65,536 addresses; a /24 leaves 8 bits, which is 256 addresses.

A VPC IPv4 block can be between /16 (largest) and /28 (smallest). You should draw it from the private ranges defined for internal networks (RFC 1918): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Choose with the future in mind: ranges cannot overlap if you ever want to connect two networks together, as Peering, Transit Gateway, VPN and Direct Connect explains, and resizing later is awkward. Using 10.0.0.0/16 for every VPC in every account is the classic mistake.

AWS also supports IPv6 CIDR blocks. They are globally unique addresses, and the rules around them are slightly different, but the same ideas apply.

Subnets: slices in one Availability Zone

A subnet is a smaller range inside the VPC's block, and it lives in exactly one Availability Zone. To make an application survive the loss of a data center, you create subnets in at least two Availability Zones and spread your instances across them (see What is AWS? for zones).

In every subnet AWS reserves five addresses: the first four and the last one. For 10.0.1.0/24 that means 10.0.1.0 (network), .1 (VPC router), .2 (DNS), .3 (reserved for future use) and 10.0.1.255 (broadcast, unused but reserved). So a /24 gives 251 usable addresses, not 256. The smallest subnet, a /28, gives only 11.

The calculator below uses the standard library to split a VPC block into subnets and report the usable addresses with the five-address rule applied. The splitting arithmetic is exactly how you would plan real ranges.

Python VPC subnet planner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change new_prefix=24 to 20 and read the output: fewer, larger subnets. Choosing sizes is a trade-off between waste and flexibility. Services such as load balancers, Lambda in a VPC and container tasks consume addresses, so do not make private subnets too small.

Route tables decide where traffic goes

Each subnet is associated with one route table: a list of rules of the form "destination range goes to this target". Every route table has an automatic local route that covers the VPC's own CIDR, so instances in different subnets can always reach each other (security rules permitting). Routes you add send other destinations to gateways.

When a packet leaves an instance, AWS finds the most specific matching route, meaning the one with the longest prefix. 10.0.0.0/16 local beats 0.0.0.0/0, and a /24 route beats a /16. The special destination 0.0.0.0/0 is the "everything else" route, usually called the default route.

A route table is an example of this shape:

Destination Target Meaning
10.0.0.0/16 local Traffic inside the VPC
0.0.0.0/0 igw-0123abcd Everything else goes to the internet gateway

The internet gateway

An internet gateway (IGW) is a horizontally scaled, highly available component you attach to a VPC. It does two jobs: it is the target for routes that lead to the internet, and it performs the address translation between an instance's private IP and its public IP. An IGW itself costs nothing and has no throughput limit you manage.

Terminal
# read-only: create a VPC, a subnet, an IGW and a default route
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-0aaa --cidr-block 10.0.1.0/24 \
  --availability-zone eu-west-1a
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --internet-gateway-id igw-0bbb --vpc-id vpc-0aaa
aws ec2 create-route --route-table-id rtb-0ccc \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0bbb

What makes a subnet public or private

There is no "public" switch on a subnet. The words describe what the route table does:

  • A public subnet has a route 0.0.0.0/0 to an internet gateway. Instances in it can be reached from the internet only if they also have a public IPv4 address (or an Elastic IP) and a security group that allows the traffic.
  • A private subnet has no route to an internet gateway. Instances there cannot be reached directly from the internet. If they need to download updates, they use a NAT gateway or endpoints, covered in NAT Gateways and VPC Endpoints.

Reaching an instance from the internet therefore needs four things at once: a route to the IGW, a public address, a security group rule and, if you use them, a network ACL that allows it. Any one missing gives you silence rather than an error message. The last two are covered in Connecting to Instances and Security Groups and NACLs, Flow Logs and Network Security.

flowchart TB
    NET(["Internet"]) --- IGW["Internet gateway"]
    subgraph VPC["VPC 10.0.0.0/16"]
        subgraph AZA["Availability Zone a"]
            PUB["Public subnet 10.0.1.0/24<br/>route 0.0.0.0/0 to IGW"]
            PRV["Private subnet 10.0.11.0/24<br/>local route only"]
        end
        IGW --- PUB
        PUB -.-|"local route"| PRV
    end

Both subnets share the VPC's local route; only the public one has a path to the internet gateway.

A route-lookup stand-in

AWS picks the most specific matching route. The code below implements that rule with real ipaddress objects for two route tables, then tests where a few destinations would go. It is a faithful model of the selection rule, though the real VPC router also handles many more target types.

Python Which route wins?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The private table has no default route, so 8.8.8.8 is dropped. That is what "private" means in practice.

The default VPC

Every Region of a new account contains a default VPC: a 172.31.0.0/16 network with one public subnet per Availability Zone, an internet gateway and a default route. It exists so that your first instance can be launched and reached without any network design. It is fine for experiments. For real workloads, create your own VPC with a planned range and a clear separation between public and private subnets, so you control exactly what is exposed.

A typical layout puts load balancers and NAT gateways in public subnets, and application servers and databases in private subnets across at least two zones. Project: Three-Tier Web App builds exactly this.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many usable IPv4 addresses does a /24 subnet have in a VPC?

    A /24 has 256 addresses and AWS reserves five in every subnet (the first four and the last), leaving 251.
  2. 2. What makes a subnet public?

    Public and private are descriptions of the route table. A route to an internet gateway is what makes a subnet public; the instance also needs a public IP and permissive security rules.
  3. 3. A subnet can span two Availability Zones.

    A subnet is tied to one Availability Zone. To spread across zones you create one subnet per zone.
  4. 4. Two routes match a packet: 10.0.0.0/16 to local and 0.0.0.0/0 to an internet gateway. Which is used for 10.0.5.9?

    The route with the longest matching prefix wins, so /16 beats /0.

Summary

  • A VPC is a private network defined by a CIDR block between /16 and /28; plan ranges so networks never overlap.
  • Subnets split the VPC and each lives in one Availability Zone; AWS reserves five addresses per subnet.
  • Route tables choose the target by the longest matching prefix, and the local route always covers the VPC.
  • An internet gateway connects a VPC to the internet; a subnet is public only because its route table points to it.
  • Reaching an instance from the internet needs a route, a public IP, a security group rule and an allowing network ACL.

Next: NAT Gateways and VPC Endpoints.