VPC — Virtual Private Cloud
Your private network inside AWS — subnets, route tables, internet gateways, NAT, and security layers
Skills taught: aws.vpc, aws.subnets, aws.route-tables, aws.igw, aws.nat-gateway, aws.nacl
What is a VPC?
A VPC (Virtual Private Cloud) is your isolated private network inside AWS. Every resource you create — EC2 instances, RDS databases, Lambda functions, ECS tasks — lives inside a VPC. Think of it as owning a floor in a data center: AWS manages the building (physical security, power, networking equipment), but your floor is yours — you control what goes in, what comes out, and who can talk to whom.
VPC architecture — the complete picture
VPC: 10.0.0.0/16 (65,534 usable IP addresses)
│
├── PUBLIC SUBNET: 10.0.1.0/24 [AZ: ap-south-1a] (254 IPs)
│ ├── Load Balancer (ALB) ← has public IP, receives internet traffic
│ ├── NAT Gateway ← lets private resources reach internet
│ └── Bastion host (optional) ← SSH jump server
│
├── PUBLIC SUBNET: 10.0.2.0/24 [AZ: ap-south-1b] (254 IPs)
│ └── Load Balancer (ALB) ← second AZ for high availability
│
├── PRIVATE SUBNET: 10.0.10.0/24 [AZ: ap-south-1a]
│ └── EC2 / ECS tasks ← your app, no public IP
│
├── PRIVATE SUBNET: 10.0.11.0/24 [AZ: ap-south-1b]
│ └── EC2 / ECS tasks ← second AZ
│
├── DATABASE SUBNET: 10.0.20.0/24 [AZ: ap-south-1a]
│ └── RDS primary ← databases, most restricted
│
└── DATABASE SUBNET: 10.0.21.0/24 [AZ: ap-south-1b]
└── RDS standby ← Multi-AZ standby
Traffic flow:
Internet → ALB (public subnet) → App (private subnet) → RDS (db subnet)
Key rule: Databases NEVER in public subnets. Apps usually not either.
Internet Gateway vs NAT Gateway
INTERNET GATEWAY (IGW):
- Attached to the VPC (one per VPC)
- Enables resources with public IPs to communicate with internet
- Both INBOUND (internet → your resource) and OUTBOUND (your resource → internet)
- Resources in public subnets route to IGW for internet access
- Cost: free
NAT GATEWAY (Network Address Translation):
- Lives in a public subnet
- Allows PRIVATE subnet resources to reach the internet (e.g., to apt-get update)
- But blocks INBOUND internet traffic — can't be reached from internet
- EC2 in private subnet → NAT Gateway → IGW → Internet
- Cost: $0.045/hour + $0.045/GB data ($32.40/month minimum)
- One per AZ for HA (if NAT Gateway fails, entire AZ loses internet)
ROUTE TABLES:
Public subnet route table:
0.0.0.0/0 → Internet Gateway (everything else goes to internet)
10.0.0.0/16 → local (VPC traffic stays internal)
Private subnet route table:
0.0.0.0/0 → NAT Gateway (outbound only)
10.0.0.0/16 → local
Database subnet route table:
10.0.0.0/16 → local (no internet at all)
Security: Security Groups vs NACLs
SECURITY GROUPS (resource-level firewall):
- Applied to individual EC2 instances, RDS databases, Lambda, etc.
- STATEFUL: response traffic is automatically allowed
- Rules: Allow only (no explicit Deny)
- Default: allow all outbound, deny all inbound
- Reference other security groups as source (e.g., 'allow from ALB-SG')
- Most important security control
NETWORK ACLs (subnet-level firewall):
- Applied to entire subnets
- STATELESS: must explicitly allow both inbound and outbound
- Rules: Allow and Deny, evaluated in order (lowest number first)
- Default NACL: allow all traffic
- Custom NACLs for compliance requirements (block specific IPs, ranges)
- Second line of defense
TYPICAL SETUP:
ALB security group: inbound 80/443 from 0.0.0.0/0; outbound 3000 to App-SG
App security group: inbound 3000 from ALB-SG only; outbound to DB-SG, internet
DB security group: inbound 5432 from App-SG only; no outbound
This 'security group chaining' means:
- Direct internet → DB: BLOCKED (DB-SG allows only App-SG source)
- App → DB: ALLOWED
- Internet → App: BLOCKED (App-SG allows only ALB-SG)
- Internet → ALB → App → DB: ALLOWED (the intended path)
Tip: The default VPC is fine for learning but not for production. Every AWS account comes with a default VPC in each region, with public subnets and an internet gateway. It's convenient for quick experiments but all resources get public IPs and there's no private/database subnet separation. For any real application, create a dedicated VPC with the three-tier architecture above.
Frequently Asked Questions
How many VPCs should I have?
At minimum: one per environment (production, staging, development) in each region you use. Many teams use one VPC per application (or microservice cluster) in production for stronger isolation. AWS allows up to 5 VPCs per region by default (can be increased). Using separate VPCs prevents a misconfigured staging environment from affecting production traffic.