How does VPC networking work — subnets, route tables, NAT gateways, and CIDR allocation?
Quick Answer
A VPC is an isolated virtual network defined by a CIDR block. It contains public subnets (with Internet Gateway routes) and private subnets (with NAT Gateway routes for outbound-only internet). Route tables control traffic flow, and CIDR allocation must be planned to avoid overlap with peered VPCs or on-premises networks.
Detailed Answer
A Virtual Private Cloud (VPC) is your isolated section of the AWS cloud — think of it as your own private office building within a massive shared complex. You control the floor plan (CIDR block), the rooms (subnets), the hallways between rooms (route tables), and the doors to the outside world (gateways).
CIDR allocation is the foundation. When you create a VPC, you assign a primary CIDR block, such as 10.0.0.0/16, which gives you 65,536 IP addresses. This is like choosing the size of your building lot before construction. The critical production consideration is planning for growth and peering. If your payments VPC uses 10.0.0.0/16 and your analytics VPC also uses 10.0.0.0/16, you cannot peer them because overlapping CIDR blocks create routing ambiguity. Many organizations adopt a structured allocation scheme: 10.0.0.0/16 for production, 10.1.0.0/16 for staging, 10.2.0.0/16 for development, and 172.16.0.0/12 reserved for on-premises connectivity via Direct Connect.
Subnets divide the VPC CIDR into smaller segments, each tied to a single Availability Zone. A /16 VPC might be split into /24 subnets, each with 251 usable IPs (AWS reserves 5 per subnet: network address, VPC router, DNS server, future use, and broadcast). Public subnets host resources that need direct internet access — load balancers, bastion hosts, NAT gateways. Private subnets host application servers, databases, and backend services that should never be directly reachable from the internet. The distinction between public and private is not a property of the subnet itself but entirely determined by the route table associated with it.
Route tables are the traffic control system. Every subnet must be associated with exactly one route table. A public subnet's route table has a route like 0.0.0.0/0 pointing to an Internet Gateway (IGW), which allows bidirectional internet traffic. A private subnet's route table has 0.0.0.0/0 pointing to a NAT Gateway, which allows outbound-only internet access. The local route (10.0.0.0/16 → local) is automatically present and enables intra-VPC communication. You can add more specific routes for VPC peering, Transit Gateway, VPN connections, or VPC endpoints.
NAT Gateways sit in a public subnet and enable private subnet resources to initiate outbound connections (pulling Docker images, downloading OS patches, calling external APIs) without exposing them to inbound internet traffic. Think of it as a one-way mirror: your servers can see out, but the internet cannot see in. NAT Gateways are managed, highly available within an AZ, and charged per hour plus per GB processed. A common production mistake is deploying only one NAT Gateway for the entire VPC — if that AZ goes down, all private subnets lose internet access. Best practice is one NAT Gateway per AZ with AZ-specific route tables.
The Internet Gateway is a horizontally scaled, redundant VPC component that performs network address translation for instances with public IPs. Unlike NAT Gateways, IGWs are free and have no bandwidth limits. They are attached at the VPC level but only affect subnets whose route tables direct 0.0.0.0/0 traffic to the IGW.
In production multi-account architectures, VPCs are connected via Transit Gateway, which acts as a central hub. This avoids the O(n^2) complexity of full-mesh VPC peering. Security Groups (stateful, instance-level) and Network ACLs (stateless, subnet-level) add defense-in-depth. VPC Flow Logs capture metadata about all traffic for security analysis and troubleshooting.