Beginner · Networking and databases: VPC and RDS · topic 14 of 50 · 35 min
VPC basics: CIDR, subnets and gateways
Explanation
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.
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 table | Destination | Target |
|---|---|---|
| public-rt | 10.0.0.0/16 | local |
| public-rt | 0.0.0.0/0 | igw-0abc (Internet Gateway) |
| private-rt-a | 10.0.0.0/16 | local |
| private-rt-a | 0.0.0.0/0 | nat-0aaa (NAT gateway in AZ a) |
| db-rt | 10.0.0.0/16 | local (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.
| Tier | ap-south-1a | ap-south-1b |
|---|---|---|
| Public (load balancer, NAT) | 10.0.0.0/24 | 10.0.1.0/24 |
| Private app | 10.0.10.0/24 | 10.0.11.0/24 |
| Private database | 10.0.20.0/24 | 10.0.21.0/24 |
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-0abcThe 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.
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
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)
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.
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"].
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- VPC security and connectivityNetworking and databases: VPC and RDS · same module
- Managed relational databases: RDS and AuroraNetworking and databases: VPC and RDS · same module
- Choosing a database: DynamoDB, caches and warehousesNetworking and databases: VPC and RDS · same module
- IP addresses, subnets and CIDRCyber Security: from zero to SOC analyst · Networking for security
- Virtualisation, VRFs and cloud networkingComputer Networking (CCNA level) · Automation, SDN and cloud networking
- Network basics: IP addresses, routes, DNS and the ip commandLinux and Red Hat (RHCSA to RHCE) · Software and networking
Questions people ask about this
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.
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.
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.
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
- 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.
- Module 4 exam: Networking and databases, VPC and RDS · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Serverless, monitoring and cost control. 50 topics left in the course before the final exam.