Amazon VPC Fundamentals: CIDR, Subnets, Route Tables, Internet Gateway, NAT Gateway and a Three-Tier Network
What Is a VPC?
A Virtual Private Cloud (VPC) is your own isolated network inside AWS. You choose the IP range, create subnets, control routing and decide what can reach the internet. A VPC lives in one Region and spans all AZs of that Region.
text
VPC 10.0.0.0/16 (Region ap-south-1)
├─ AZ-a: public 10.0.0.0/24 private-app 10.0.10.0/24 private-db 10.0.20.0/24
├─ AZ-b: public 10.0.1.0/24 private-app 10.0.11.0/24 private-db 10.0.21.0/24
Internet Gateway (IGW) ─ public route table (0.0.0.0/0 → IGW)
NAT Gateway (in a public subnet) ─ private route table (0.0.0.0/0 → NAT)
CIDR Basics
CIDR notation writes an IP range as an address and a prefix length. 10.0.0.0/16 means the first 16 bits are fixed, so there are 2^(32-16) = 65,536 addresses.
| CIDR | Addresses | Usable in an AWS subnet |
| /16 | 65,536 | 65,531 |
| /20 | 4,096 | 4,091 |
| /24 | 256 | 251 |
| /28 (smallest subnet) | 16 | 11 |
- AWS reserves 5 addresses in every subnet: network address, VPC router, DNS, future use, and broadcast
- VPC CIDR size: from /16 (largest) to /28 (smallest). You can add secondary CIDR blocks later
- Use private ranges (RFC 1918):
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
- Plan ahead: ranges of VPCs that you will connect (peering, VPN, Transit Gateway) must not overlap
Subnets
| Item | Details |
| Subnet | A range of the VPC CIDR in exactly one AZ |
| Public subnet | Its route table has a route to an Internet Gateway, and instances have public IPs |
| Private subnet | No direct route to the internet. Outbound access goes through a NAT Gateway (or none at all) |
| Isolated subnet | No route to the internet in or out (databases, sensitive tiers) |
What makes a subnet public? Only its route table: a route 0.0.0.0/0 → igw-.... The name does not matter.
Route Tables
- Every subnet is associated with one route table (the main one if you do not choose)
- A route table has a local route (the VPC CIDR) that cannot be removed, so subnets in the VPC can talk to each other by default
- The most specific route wins (longest prefix match)
- Targets can be an IGW, NAT gateway, VPC peering, Transit Gateway, VPN gateway, VPC endpoint, network interface and more
| Destination | Target | Meaning |
| 10.0.0.0/16 | local | Inside the VPC |
| 0.0.0.0/0 | igw-0abc | Everything else goes to the internet (public route table) |
| 0.0.0.0/0 | nat-0abc | Everything else goes via NAT (private route table) |
| 10.1.0.0/16 | pcx-0abc | To a peered VPC |
| pl-xxxx (S3 prefix list) | vpce-0abc | S3 over a gateway endpoint |
Internet Gateway and NAT Gateway
| Internet Gateway (IGW) | NAT Gateway |
| Purpose | Two-way internet access for resources with public IPs | Outbound-only internet for private subnets (updates, API calls) |
| Placed | Attached to the VPC | In a public subnet with an Elastic IP |
| Availability | Highly available, managed by AWS | Lives in one AZ. For HA create one per AZ and route each private subnet to its own |
| Cost | Free (data transfer is charged) | Hourly + per GB processed. A common surprise cost |
| Inbound from internet | Yes (with public IP and security group allow) | No: cannot start inbound connections |
Alternatives: NAT Instance (old, you manage it, can be cheaper for tiny labs). Egress-only Internet Gateway gives IPv6 outbound-only. VPC endpoints let private subnets reach AWS services without NAT and save money.
Build a VPC With the CLI
bash
VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC --enable-dns-hostnames
aws ec2 create-tags --resources $VPC --tags Key=Name,Value=shop-vpc
PUB_A=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.0.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
PUB_B=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.1.0/24 --availability-zone ap-south-1b --query Subnet.SubnetId --output text)
APP_A=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.10.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
APP_B=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.11.0/24 --availability-zone ap-south-1b --query Subnet.SubnetId --output text)
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC
PUB_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PUB_RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_A
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_B
EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT=$(aws ec2 create-nat-gateway --subnet-id $PUB_A --allocation-id $EIP --query NatGateway.NatGatewayId --output text)
aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT
PRIV_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PRIV_RT --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $APP_A
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $APP_B
The Same in Terraform (short)
hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "shop-vpc"
cidr = "10.0.0.0/16"
azs = ["ap-south-1a", "ap-south-1b"]
public_subnets = ["10.0.0.0/24", "10.0.1.0/24"]
private_subnets = ["10.0.10.0/24", "10.0.11.0/24"]
database_subnets = ["10.0.20.0/24", "10.0.21.0/24"]
enable_nat_gateway = true
one_nat_gateway_per_az = true # HA. Use single_nat_gateway = true for cheap labs
enable_dns_hostnames = true
}
Default VPC
- Every Region has a default VPC (172.31.0.0/16) with one public subnet per AZ, an IGW and a route to the internet
- Good for quick tests. Not for production: design your own VPC with private subnets
- Instances in default subnets get a public IP automatically
DNS and DHCP
- **
enableDnsSupport and enableDnsHostnames** control the Amazon DNS resolver (VPC base + 2) and DNS names for instances. PrivateLink and private hosted zones need them
- DHCP option sets set domain name, DNS servers and NTP for the VPC
IPv6 in a VPC
- Optionally add an Amazon-provided IPv6 CIDR (/56). Each subnet gets a /64
- IPv6 addresses are public and globally unique: there is no NAT. Use an egress-only internet gateway to allow outbound only
- Dual-stack designs avoid NAT cost for outbound traffic to IPv6 destinations
Three-Tier Architecture
text
Internet ─► ALB (public subnets, 2 AZs)
└► App tier: EC2/ECS in private-app subnets (2 AZs) ──► NAT Gateway for outbound updates
└► Database tier: RDS Multi-AZ in private-db subnets (no internet route at all)
| Tier | Subnet type | Reachable from |
| Web / load balancer | Public | Internet (80/443) |
| Application | Private | Only the load balancer security group |
| Database | Private/isolated | Only the application security group (3306/5432) |
Design Tips
- Use at least two AZs and put each tier in subnets in each AZ
- Give subnets room to grow (do not use /28 everywhere). Containers on EKS can use many IPs
- Keep databases out of public subnets
- Create one NAT Gateway per AZ for production, or use endpoints to reduce NAT usage
- Tag everything and keep the CIDR plan in a document or IPAM
- Use VPC IP Address Manager (IPAM) for large environments
- Enable VPC Flow Logs for troubleshooting and security
Practice Tasks
- Calculate how many usable IPs a /22 subnet has in AWS
- Build the VPC above with the CLI or Terraform and launch an instance in a private subnet that can run
curl to the internet via NAT
- Remove the NAT route and observe the failure
- Explain why two VPCs with
10.0.0.0/16 cannot be peered
- Draw a three-tier design for an app that needs 3 AZs
Conclusion
A VPC is your private network: choose a non-overlapping CIDR, create subnets per AZ, use route tables (IGW route makes a subnet public, NAT route gives private subnets outbound access), keep databases private, and use multiple AZs. NAT Gateways cost money, so plan one per AZ and use endpoints where possible.
Quick Summary: VPC/CIDR math (5 reserved); subnets public/private/isolated; route tables; IGW vs NAT (one per AZ); CLI + Terraform; default VPC; DNS; IPv6 egress-only; three-tier; design tips.