Skip to content

Beginner · Networking and databases: VPC and RDS · topic 14 of 50 · 35 min

VPC basics: CIDR, subnets and gateways

Explanation

A VPC (Virtual Private Cloud) is your own private network inside an AWS region, with an IP range written in CIDR notation such as 10.0.0.0/16. You split it into subnets, each in one Availability Zone. A subnet is public when its route table sends internet traffic to an Internet Gateway; private subnets reach the internet only outward, through a NAT gateway.

Why a private network?

When you run servers and databases in the cloud, you decide which of them the internet may reach. A web server must be reachable; a database with customer data must not be. A VPC (Virtual Private Cloud) is an isolated network that belongs to your account in one region. You choose its address range, cut it into smaller networks called subnets, and decide where traffic may flow.

Think of it like this: A VPC is like a gated housing society. The society has its own range of house numbers (the CIDR block). Each tower is a subnet. The main gate to the road is the Internet Gateway. Some towers face the road with their own entrance (public subnets); others are inside, and residents can go out through the security gate but strangers cannot walk in (private subnets behind a NAT gateway).

CIDR blocks

CIDR (Classless Inter-Domain Routing) notation writes a range of IP addresses as a start address and a prefix length. 10.0.0.0/16 means the first 16 bits are fixed, so the range is 10.0.0.0 to 10.0.255.255, which is 65,536 addresses. A larger prefix means a smaller network: a /24 has 256 addresses and a /28 has 16. In AWS a VPC block can be from /16 (largest) to /28 (smallest). Use private ranges from RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16), and pick one that does not overlap your office network or other VPCs you may connect later.

Subnets and the 5 reserved addresses

A subnet is a slice of the VPC range that lives in exactly one Availability Zone (AZ). To survive the loss of a zone, you create matching subnets in at least two AZs. AWS reserves 5 addresses in every subnet: the first four and the last one. In 10.0.1.0/24 those are 10.0.1.0 (network address), 10.0.1.1 (VPC router), 10.0.1.2 (Amazon DNS), 10.0.1.3 (reserved for future use) and 10.0.1.255 (the last address). So a /24 gives 256 minus 5, which is 251 usable addresses, and a /28 gives only 11.

Route tables, Internet Gateway and NAT gateway

A route table is a list of rules that says where traffic for each destination goes. Every route table has a "local" route for the VPC range, so all subnets can talk to each other. Each subnet is associated with one route table.

  • Internet Gateway (IGW): attached to the VPC, it connects the VPC to the internet in both directions. A subnet is public if its route table has 0.0.0.0/0 pointing to the IGW. An instance there also needs a public IP to be reachable.
  • NAT gateway: lives in a public subnet with an Elastic IP. Private subnets send 0.0.0.0/0 to it, so their instances can download updates or call payment APIs, but nobody on the internet can start a connection to them. It is charged per hour and per GB processed, so check the pricing page and watch the bill.
Route tableDestinationTarget
public-rt10.0.0.0/16local
public-rt0.0.0.0/0igw-0abc (Internet Gateway)
private-rt-a10.0.0.0/16local
private-rt-a0.0.0.0/0nat-0aaa (NAT gateway in AZ a)
db-rt10.0.0.0/16local (no internet route at all)

A standard two-AZ design

Most production apps use three tiers of subnets, repeated in two (or three) AZs: public subnets for the load balancer and NAT gateways, private app subnets for the EC2 instances or containers, and private database subnets with no internet route.

Tierap-south-1aap-south-1b
Public (load balancer, NAT)10.0.0.0/2410.0.1.0/24
Private app10.0.10.0/2410.0.11.0/24
Private database10.0.20.0/2410.0.21.0/24
bash
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-0abc --cidr-block 10.0.0.0/24 --availability-zone ap-south-1a
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --internet-gateway-id igw-0abc --vpc-id vpc-0abc
aws ec2 create-route --route-table-id rtb-0pub --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc
aws ec2 associate-route-table --route-table-id rtb-0pub --subnet-id subnet-0pub1
aws ec2 create-nat-gateway --subnet-id subnet-0pub1 --allocation-id eipalloc-0abc
aws ec2 create-route --route-table-id rtb-0priv --destination-cidr-block 0.0.0.0/0 --nat-gateway-id nat-0abc
The building blocks with the CLI. In real projects you write this as infrastructure as code (module 9). IDs are examples.

The default VPC

Every region in a new account has a default VPC (172.31.0.0/16) with a public subnet in each AZ and an Internet Gateway, and instances there get public IPs automatically. It is handy for quick tests, but for real workloads create your own VPC with private subnets.

Careful: Common mistakes: (1) Choosing 10.0.0.0/16 for every VPC and the office too, then finding you cannot connect them because the ranges overlap; plan ranges up front. (2) Making subnets too small, such as /28, and running out of IPs when containers or Lambda functions each take one. (3) Putting databases in public subnets. (4) One NAT gateway for two AZs: if its AZ fails, the other AZ loses internet access; use one per AZ for production. (5) Forgetting that a public subnet alone is not enough; the instance also needs a public IP.
Tip: Send S3 and DynamoDB traffic through a free gateway endpoint (next lesson) instead of the NAT gateway. It can cut the NAT data processing bill a lot.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

VPC basics: CIDR, subnets and gateways · 1/7

VPC = your private network One region

A VPC is your own private network in one AWS region. You choose its address range and split it into subnets.

Prefer a video by a teacher? Find free videos on “aws vpc tutorial subnets route tables internet gateway nat gateway” on YouTube ↗

In real life: A fintech startup in Bengaluru plans its network

A lending startup must keep customer KYC data away from the internet. Their architect picks 10.20.0.0/16 for production (the office uses 192.168.0.0/16 and staging uses 10.30.0.0/16, so nothing overlaps). In ap-south-1 they create public subnets for the Application Load Balancer and two NAT gateways, private app subnets for the containers, and database subnets for PostgreSQL with no internet route. Auditors can see in the route tables that the database subnets have no path to the internet at all.

Practice (0/3 done)

Task 1 · Code · Python

Write usable_ips(cidr) that returns how many addresses you can use for instances in an AWS subnet: the total number of addresses minus the 5 that AWS reserves. Use the ipaddress module. Example: usable_ips("10.0.1.0/24") returns 251.

Task 2 · Code · Python

Write reserved_ips(cidr) that returns the 5 addresses AWS reserves in a subnet, as strings, in order: the first four addresses and the last one. Example: for "10.0.1.0/24" return ["10.0.1.0", "10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.255"].

Task 3 · Code · Python

Write plan_subnets(vpc_cidr, new_prefix, count) that returns the first count subnets of size new_prefix inside vpc_cidr, as strings in order. If the VPC cannot hold that many, raise ValueError. Example: plan_subnets("10.0.0.0/16", 24, 3) returns ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24"].

Quick quiz

Question 1

1.What makes a subnet "public" in AWS?

Question 2

2.How many usable addresses does an AWS subnet 10.0.5.0/26 have?

Question 3

3.An app server in a private subnet must download OS updates but must not accept connections from the internet. What does its route table need?

Question 4

4.Which VPC CIDR size is NOT allowed in AWS?

Question 5

5.A subnet lives in how many Availability Zones?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What makes a subnet "public" in AWS?

Answer: Its route table has 0.0.0.0/0 pointing to an Internet Gateway

Public means the subnet's route table sends internet traffic to an Internet Gateway.

How many usable addresses does an AWS subnet 10.0.5.0/26 have?

Answer: 59

A /26 has 64 addresses; AWS reserves 5, leaving 59.

An app server in a private subnet must download OS updates but must not accept connections from the internet. What does its route table need?

Answer: 0.0.0.0/0 to a NAT gateway

A NAT gateway allows outbound connections from private subnets while blocking inbound ones.

Which VPC CIDR size is NOT allowed in AWS?

Answer: /8

AWS allows VPC blocks from /16 to /28. A /8 is too large.

A subnet lives in how many Availability Zones?

Answer: Exactly one

Each subnet is tied to one AZ, so you create subnets in several AZs for high availability.

Which address is private?Related

Answer: 172.20.1.5

172.16.0.0/12 covers 172.16.x.x to 172.31.x.x, so 172.20.1.5 is private; 172.32.1.5 is not.

From IP addresses, subnets and CIDR

A private app server must download updates from the internet but must not accept connections from it. What does its subnet route to?Related

Answer: A NAT gateway

A NAT gateway lets private servers start outbound connections while blocking inbound ones.

From Virtualisation, VRFs and cloud networking

Which services can use a gateway VPC endpoint?Related

Answer: S3 and DynamoDB

Gateway endpoints exist only for S3 and DynamoDB. Other services use interface endpoints.

From VPC security and connectivity

Which command shows the default gateway?Related

Answer: ip route

The "default via" line in ip route is the gateway.

From Network basics: IP addresses, routes, DNS and the ip command

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “Networking and databases: VPC and RDS” before its exam: VPC security and connectivity, Managed relational databases: RDS and Aurora, Choosing a database: DynamoDB, caches and warehouses, DNS and CDN: Route 53 and CloudFront.
  2. Module 4 exam: Networking and databases, VPC and RDS · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Serverless, monitoring and cost control. 50 topics left in the course before the final exam.

← Previous: Securing and sharing S3 data