VPC Basics
A VPC is your private slice of AWS’s network. Subnets carve it into zones (public, private); route tables steer traffic; security groups + NACLs filter it. Everything else (EC2, RDS, ECS) lives inside.
A real two-tier VPC
EXAMPLE
# Goal: web servers in public subnets, DB in private subnets,
# NAT for outbound, no ingress to private from internet.
# 1) Create the VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16
# 2) Public + private subnets across two AZs
aws ec2 create-subnet --vpc-id vpc-… --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
aws ec2 create-subnet --vpc-id vpc-… --cidr-block 10.0.2.0/24 --availability-zone us-east-1b
aws ec2 create-subnet --vpc-id vpc-… --cidr-block 10.0.11.0/24 --availability-zone us-east-1a
aws ec2 create-subnet --vpc-id vpc-… --cidr-block 10.0.12.0/24 --availability-zone us-east-1b
# 3) Internet Gateway + public route table
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --vpc-id vpc-… --internet-gateway-id igw-…
# Public route: 0.0.0.0/0 → IGW (attach to PUBLIC subnets)
# Private route: 0.0.0.0/0 → NAT GW in a public subnet
# 4) NAT Gateway for outbound from private subnets
aws ec2 allocate-address --domain vpc
aws ec2 create-nat-gateway --subnet-id subnet-PUBLIC … --allocation-id eip-…
# 5) Security groups — stateful, default-deny ingress
aws ec2 create-security-group --group-name web --vpc-id vpc-…
aws ec2 authorize-security-group-ingress --group-id sg-web \
--protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 create-security-group --group-name db --vpc-id vpc-…
aws ec2 authorize-security-group-ingress --group-id sg-db \
--protocol tcp --port 5432 --source-group sg-web
# 6) Always use IaC for this — Terraform / CDK / Pulumi.
# A hand-built VPC is fine to learn; production should be in code.
Why it matters
Pick CIDR blocks BIG. A /16 gives you 65k IPs — cheap insurance against future subnet growth. Going small saves nothing and forces a painful migration when you grow.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
# A Virtual Private Cloud is your private network in AWS. # Default VPC works for prototyping; build custom for prod.Try it Yourself »
Discussion
Loading…