VPC basics: CIDR, subnets, route tables and the internet gateway
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Operations and security
-
Phase 3: SQL Server on AWS
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Every server, database and load balancer in this course sits inside one private network. This AWS VPC tutorial walks through CIDR blocks, subnets, route tables and internet gateways in plain words, and then you build saat-vpc from the command line.
Since a VPC and its subnets cost nothing, this network stays in your account for the rest of the course, and you’ll save its IDs in a file that later lessons read.
| Time | You need | By the end you can |
|---|---|---|
| 60 minutes | Your laptop signed in with aws login --profile saat-admin. | Plan IP ranges with CIDR, build a VPC with public, private and database subnets across two zones, route a public subnet to the internet, and fix subnets that overlap or fall outside the range. |
What it is, in one line
A VPC is your own private network in AWS, divided into subnets, with route tables deciding where traffic is allowed to go.
In Simple Words
A gated housing society
Think of a gated housing society. The whole society has a range of house numbers, say 1 to 65,000, and that range is the VPC’s CIDR block. It’s divided into blocks: Block A near the main gate for shops, Block B for homes and Block C behind the homes for the bank vault, and these are your subnets. The signboards at each junction (“To main gate: turn left”) are route tables and the main gate is the internet gateway. Only Block A has a road to the gate, so a house in Block B can’t get to the street, not because there’s a wall but because no signboard points that way.
Engineer's View
How a VPC is put together
A VPC belongs to one Region and has an IPv4 range written in CIDR notation, like 10.20.0.0/16. The number after the slash says how many bits are fixed, so a /16 has 65,536 addresses, a /24 has 256 and a /28 has 16. VPCs can be anywhere from /16 to /28.
Stick to private ranges (10.x, 172.16 to 172.31, 192.168.x) that don’t clash with your office network or with other VPCs you might connect later.
A subnet is a slice of the VPC range that sits in one Availability Zone. AWS holds back five addresses in every subnet (network, router, DNS, one reserved and broadcast), which leaves 251 usable addresses in a /24.
Each subnet uses exactly one route table. Every table has a local route covering the whole VPC, so all the subnets can talk to each other. A subnet only counts as public when its route table sends 0.0.0.0/0 to an internet gateway and its instances have public IPs. Any subnet without an explicit association falls back to the VPC’s main route table.
Public IPv4 addresses are charged by the hour (about USD 0.005 an hour each since 2024), which is why only the public subnets hand them out automatically in our design.
How it works, step by step
This is the design you’re about to build, so read through it once before you type anything.
1Choose the range
10.20.0.0/16 gives you room for years of labs and is unlikely to clash with home routers (usually 192.168.x) or the usual office ranges (10.0.x).
2Cut it into layers and zones
There are three layers: public (load balancers, bastions), private (app servers) and database. Each layer gets one /24 in ap-south-1a and another in ap-south-1b, so if a zone fails, half of everything keeps running.
3Give the public layer a way out
An internet gateway is attached to the VPC, and a route table called saat-rt-public points 0.0.0.0/0 at it. Only the two public subnets use that table.
4Keep the others inside
saat-rt-private has nothing but the local route. Private servers will get outbound internet through a NAT gateway in Lesson 9, and the databases never get it at all.
Keep in mind that AWS doesn’t care what you call a subnet; this is the only test that counts.
When someone tells you “my instance in the public subnet can’t reach the internet”, check three things in order: that subnet’s route table, whether the instance has a public IP, and then the security group (Lesson 10).
✅ Checkpoint: You can say how many usable addresses a /24 gives you in AWS (251) and what makes a subnet public.
Hands-on lab: AWS VPC tutorial, build saat-vpc
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Start as usual:
cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'1Do the CIDR maths first
Paste this whole block:
function Get-SubnetInfo([string]$Cidr) {
$ip, $bits = $Cidr -split '/'
$size = [math]::Pow(2, 32 - [int]$bits)
[pscustomobject]@{ Cidr = $Cidr; Addresses = $size; UsableInAws = $size - 5 }
}
'10.20.0.0/16','10.20.1.0/24','10.20.21.0/28' | ForEach-Object { Get-SubnetInfo $_ } | Format-TableYou should see this:
Cidr Addresses UsableInAws
---- --------- -----------
10.20.0.0/16 65536 65531
10.20.1.0/24 256 251
10.20.21.0/28 16 112Create the VPC
Run this:
$vpc = aws ec2 create-vpc --cidr-block 10.20.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=saat-vpc},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=08}]' --query Vpc.VpcId --output text
aws ec2 modify-vpc-attribute --vpc-id $vpc --enable-dns-hostnames Value=true
$vpcYou should see something like this:
vpc-0f1e2d3c4b5a697883Create the six subnets
Paste this whole block:
$plan = @(
@{ Name = 'saat-public-a'; Cidr = '10.20.1.0/24'; Az = 'ap-south-1a' },
@{ Name = 'saat-public-b'; Cidr = '10.20.2.0/24'; Az = 'ap-south-1b' },
@{ Name = 'saat-private-a'; Cidr = '10.20.11.0/24'; Az = 'ap-south-1a' },
@{ Name = 'saat-private-b'; Cidr = '10.20.12.0/24'; Az = 'ap-south-1b' },
@{ Name = 'saat-db-a'; Cidr = '10.20.21.0/24'; Az = 'ap-south-1a' },
@{ Name = 'saat-db-b'; Cidr = '10.20.22.0/24'; Az = 'ap-south-1b' }
)
$ids = @{}
foreach ($s in $plan) {
$ids[$s.Name] = aws ec2 create-subnet --vpc-id $vpc --cidr-block $s.Cidr --availability-zone $s.Az --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$($s.Name)},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=08}]" --query Subnet.SubnetId --output text
}
$ids4Add the internet gateway and the public route table
Paste this whole block:
$igw = aws ec2 create-internet-gateway --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=saat-igw},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=08}]' --query InternetGateway.InternetGatewayId --output text
aws ec2 attach-internet-gateway --internet-gateway-id $igw --vpc-id $vpc
$rtPub = aws ec2 create-route-table --vpc-id $vpc --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=saat-rt-public},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=08}]' --query RouteTable.RouteTableId --output text
aws ec2 create-route --route-table-id $rtPub --destination-cidr-block 0.0.0.0/0 --gateway-id $igw
foreach ($n in 'saat-public-a','saat-public-b') {
aws ec2 associate-route-table --route-table-id $rtPub --subnet-id $ids[$n] | Out-Null
aws ec2 modify-subnet-attribute --subnet-id $ids[$n] --map-public-ip-on-launch
}The create-route command should print:
{
"Return": true
}5Add the private route table
Paste this whole block:
$rtPriv = aws ec2 create-route-table --vpc-id $vpc --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=saat-rt-private},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=08}]' --query RouteTable.RouteTableId --output text
foreach ($n in 'saat-private-a','saat-private-b','saat-db-a','saat-db-b') {
aws ec2 associate-route-table --route-table-id $rtPriv --subnet-id $ids[$n] | Out-Null
}6Check the result
Run this:
aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc" --query "Subnets[].[Tags[?Key=='Name']|[0].Value,CidrBlock,AvailabilityZone,MapPublicIpOnLaunch]" --output text | Sort-ObjectYou should see this:
saat-db-a 10.20.21.0/24 ap-south-1a False
saat-db-b 10.20.22.0/24 ap-south-1b False
saat-private-a 10.20.11.0/24 ap-south-1a False
saat-private-b 10.20.12.0/24 ap-south-1b False
saat-public-a 10.20.1.0/24 ap-south-1a True
saat-public-b 10.20.2.0/24 ap-south-1b True
7Save the IDs for later lessons
Run this:
[pscustomobject]@{
VpcId = $vpc; IgwId = $igw; RtPublic = $rtPub; RtPrivate = $rtPriv
PublicA = $ids['saat-public-a']; PublicB = $ids['saat-public-b']
PrivateA = $ids['saat-private-a']; PrivateB = $ids['saat-private-b']
DbA = $ids['saat-db-a']; DbB = $ids['saat-db-b']
} | ConvertTo-Json | Set-Content C:\aws-lab\saat-vpc.json
Get-Content C:\aws-lab\saat-vpc.jsonAfter this, any lesson can load the network with $v = Get-Content C:\aws-lab\saat-vpc.json | ConvertFrom-Json.
✅ Checkpoint: Six subnets exist and only the two public ones say True, the public route table sends 0.0.0.0/0 to saat-igw, and saat-vpc.json holds all ten IDs. Don’t delete this VPC; it costs nothing and you’ll need it from Lesson 9 onwards.
Break It, Then Fix It
CIDR mistakes are the errors people make most often when building networks, and AWS catches both of these straight away.
1Break it: a subnet that overlaps another
Try creating a subnet inside saat-public-a’s range:
aws ec2 create-subnet --vpc-id $vpc --cidr-block 10.20.1.128/25 --availability-zone ap-south-1aYou’ll get this error:
An error occurred (InvalidSubnet.Conflict) when calling the CreateSubnet operation: The CIDR '10.20.1.128/25' conflicts with another subnet10.20.1.128/25 is the top half of 10.20.1.0/24, and subnets in the same VPC can never share addresses. Fix: choose a free block, for example 10.20.3.0/24, and keep a written plan like the one in step 3 so everybody takes the next unused number.
2Break it: a subnet outside the VPC range
Run this:
aws ec2 create-subnet --vpc-id $vpc --cidr-block 10.30.1.0/24 --availability-zone ap-south-1aYou’ll get this error:
An error occurred (InvalidSubnet.Range) when calling the CreateSubnet operation: The CIDR '10.30.1.0/24' is invalid.
All the message says is “invalid”, but the reason is that 10.30.x isn’t inside 10.20.0.0/16. Fix: use a block that sits inside the VPC, or, if you really need more space, add a secondary CIDR block to the VPC first.
✅ Checkpoint: You’ve seen both CIDR errors and know how to fix each one. Nothing new got created, so there’s nothing to clean up.
This happened at work
The VPC that matched the office
A logistics company built its first VPC using the range the console suggested, 10.0.0.0/16, and for a year everything worked. Then the company decided it wanted a Site-to-Site VPN so the warehouse systems could reach the cloud app directly.
The trouble was that the head office network was also 10.0.0.0/16. With the same addresses on both sides, the routers had no way of knowing which 10.0.5.20 was meant, so the VPN came up but nothing could talk across it.
In the end the team built a new VPC with 10.40.0.0/16, rebuilt the servers into it and moved the database over a weekend window. Ever since, each new VPC takes its range from a shared IP plan (later managed in VPC IP Address Manager), and nobody accepts a default range any more.
Common mistakes and troubleshooting
| Mistake | What happens | What to do |
|---|---|---|
| Using the default 10.0.0.0/16 everywhere | VPN or peering fails later on because the ranges overlap | Plan a range for each VPC and avoid the ones your office uses |
| VPC too small (for example /24) | No room for new subnets later | Start a main VPC at /16, since subnets are cheap to add |
| Calling a subnet “public” but not routing it | Instances can’t reach the internet | Route 0.0.0.0/0 to an internet gateway in the subnet’s route table |
| Forgetting to associate a subnet | It silently uses the main route table | Associate every subnet explicitly |
| Auto-assigning public IPs in private subnets | Extra hourly charges and exposure | Only public subnets get map-public-ip-on-launch |
| Forgetting AWS keeps 5 addresses per subnet | Fewer IPs than you expected, and launches fail once it’s full | A /24 gives 251 usable addresses, a /28 only 11 |
Interview questions
Click a question to open it. Each one is worded the way an interviewer might ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. How would you explain a VPC to someone who has only worked with an office network?
Why they ask this: They’re checking you understand the basic network idea in AWS, since the rest of AWS networking builds on it.
How you might answer: I’d describe a VPC as a private, isolated network for my account in one Region, where I choose the IP range and create the subnets, route tables and gateways. Things like EC2 instances and RDS databases live inside it, much like servers sit inside the office network.
2. A teammate named a subnet “public-subnet”, but nothing in it can reach the internet. What actually makes a subnet public?
Why they ask this: It’s one of the most common VPC questions, and they want to know you look at routes rather than names.
How you might answer: The name has nothing to do with it. A subnet is public when its route table has a 0.0.0.0/0 route to an internet gateway and its instances have public IP addresses, so I’d check the route table association first and then whether the instances got a public IP.
3. You create a /24 subnet in AWS. How many IP addresses can you actually use, and why?
Why they ask this: They want to see practical CIDR knowledge, because getting this wrong leaves you with subnets that fill up sooner than planned.
How you might answer: You get 251. A /24 has 256 addresses, and AWS reserves 5 in every subnet for the network address, the VPC router, DNS, future use and the broadcast address.
2–5 years experience
4. Why would you bother creating subnets in more than one Availability Zone?
Why they ask this: They’re looking for high availability thinking, since designs that sit in one zone are a common cause of outages.
How you might answer: A subnet only lives in one zone, so if that zone has trouble, everything in the subnet is hit. With matching subnets in two or three zones, load balancers and Multi-AZ databases can carry on when one zone fails.
5. Someone added an internet route to the main route table, and suddenly several “private” subnets became public. How did that happen?
Why they ask this: They want to know if you understand the hidden defaults in a VPC, because quiet defaults like this one cause real security incidents.
How you might answer: Any subnet without an explicit association uses the main route table, so putting an internet route there made every unassociated subnet public. To avoid it, I keep the main table private and associate each subnet with a route table explicitly.
6. You’re creating a new VPC. How would you choose its CIDR range?
Why they ask this: They want to see you plan for growth and future connections, because overlapping ranges are very painful to fix later.
How you might answer: I’d choose a private range that doesn’t overlap the office network, other VPCs or any partner I might connect to, and make it big enough to grow, often a /16. Then I’d record it in a shared IP plan or in IPAM so nobody else grabs it.
Senior level
7. Your company wants 50 VPCs connected to each other and to the data centre. How would you plan the IP addresses?
Why they ask this: They want to see network design at scale, and whether you’d take out the manual steps that lead to mistakes.
How you might answer: I’d set aside a large supernet for AWS that doesn’t clash with on-premises, and split it into pools per Region and per environment in VPC IPAM, so every VPC gets a block that doesn’t overlap any other. They’d connect through a Transit Gateway with route tables per environment. Allocation is documented and automated, so nobody picks ranges by hand.
8. A VPC is running out of IP addresses and the app team needs more servers next week. What are your options?
Why they ask this: They want to see how you solve problems under pressure, and that you reach for the least disruptive options first.
How you might answer: I’d add a secondary CIDR block to the VPC and create new subnets in it, use smaller subnets for the next workloads, and clean up unused ENIs and IPs. For containers I’d look at prefix delegation or IPv6, and I’d only rebuild the VPC as a last resort.
Quick quiz
Q1. How many addresses does AWS reserve in every subnet?
- 0
- 2
- 5
- 10
Show answer
C: AWS reserves 5 addresses in each subnet, which leaves 251 usable addresses in a /24.
Q2. What makes saat-public-a a public subnet?
- Its name
- Its route table sends 0.0.0.0/0 to an internet gateway
- It is in ap-south-1a
- It has a /24 range
Show answer
B: only the route to an internet gateway (plus public IPs) makes a subnet public.
Q3. A subnet lives in how many Availability Zones?
- One
- Two
- All zones of the Region
- None
Show answer
A: each subnet belongs to exactly one zone, which is why you create them in pairs.
Q4. Creating 10.20.1.128/25 fails with InvalidSubnet.Conflict. Why?
- The VPC is full
- It overlaps 10.20.1.0/24
- /25 isn’t allowed
- Wrong zone
Show answer
B: 10.20.1.128/25 is half of a subnet that already exists, and subnets can’t overlap.
Q5. Which range is the safest choice for a VPC you may later connect to an office using 10.0.0.0/16?
- 10.0.0.0/16
- 10.0.128.0/17
- 10.20.0.0/16
- 0.0.0.0/0
Show answer
C: 10.20.0.0/16 stays clear of the office range.
Quick recap
- A VPC is a private network in one Region with a CIDR range, and saat-vpc uses 10.20.0.0/16.
- Subnets are slices of that range in a single zone, and AWS keeps back 5 addresses in each one.
- Route tables decide where traffic goes, and a subnet is only public when it has 0.0.0.0/0 to an internet gateway.
- Plan your ranges so they never overlap networks you might connect later.
- saat-vpc.json holds all the IDs, and later lessons load it instead of looking them up.
Next lesson
Lesson 9: Private subnets, NAT gateway and VPC endpoints → Next you’ll give private servers a safe way out to the internet and see why NAT gateways show up on bills.
Further reading: AWS VPC tutorial
If you want to go further after this AWS VPC tutorial, read How Amazon VPC works and Subnet CIDR blocks in the Amazon VPC User Guide. The full plan for this course is on the AWS Cloud Administration course page.
