Stop 07 · Networking With Amazon VPCTopics · 3/9

Public vs Private Subnets

Part of the AWS Roadmap

In short

Whether a database can be attacked from the internet often comes down to one thing: which subnet it lives in. Public and private subnets are the basic shape of almost every AWS architecture diagram you will ever draw.

A subnet lives in one Availability Zone. A public subnet has a route to an internet gateway, while a private subnet does not, so resources there cannot be reached directly from the internet. Web-facing load balancers go in public subnets and databases in private ones.

The jaadu stands behind a sweet shop counter facing the street while a curtained doorway behind leads into a quiet back store room stacked with boxes.

Think of it like this

Shop front and back store room

A sweet shop has a front counter that opens onto the street and a back store room with no street door. Customers reach the counter, the staff walk to the store room from inside. Your load balancer is the counter in a public subnet, your database is the store room in a private one.

Watch it happen

What makes a subnet public or private

Public subnet

  • Route table has 0.0.0.0/0 to an internet gateway
  • Resources with public IPs are reachable from the internet (if SGs allow)
  • Holds load balancers, NAT gateways, bastions
  • Often auto-assigns public IPv4 addresses

Private subnet

  • No route to an internet gateway
  • Cannot be reached directly from the internet
  • Holds app servers, databases, caches
  • Reaches out through a NAT gateway or VPC endpoints

'Public' is not a checkbox. A subnet is public only because its route table points to an internet gateway.

The big ideas

What you need to know

One AZ per subnet

A subnet lives in exactly one Availability Zone. For high availability, create matching subnets in two or more AZs.

Defined by routes

The route table decides public vs private. The subnet's name is just a label.

Public IPv4 costs money

AWS charges for every public IPv4 address. Keep app servers private and expose only a load balancer.

Tiered layout

Typical pattern: public tier for entry points, private app tier, and an isolated data tier.

See it in code

Walk through the code

bash
1VPC_ID=vpc-0123456789abcdef02 3PUB_A=$(aws ec2 create-subnet --vpc-id "$VPC_ID" \4  --cidr-block 10.20.0.0/24 --availability-zone ap-south-1a \5  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=public-a}]' \6  --query Subnet.SubnetId --output text)7 8PRIV_A=$(aws ec2 create-subnet --vpc-id "$VPC_ID" \9  --cidr-block 10.20.10.0/24 --availability-zone ap-south-1a \10  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=private-a}]' \11  --query Subnet.SubnetId --output text)12 13# Give instances launched in the public subnet a public IPv4 automatically14aws ec2 modify-subnet-attribute --subnet-id "$PUB_A" --map-public-ip-on-launch

1/3The public-a subnet gets the 10.20.0.0/24 block in AZ a.

One public and one private subnet in ap-south-1a. Neither is truly public yet: that needs an internet gateway and a route, covered next.

Save yourself the pain

Common mistakes

Oops

Putting the database in a public subnet and relying on the password.

Do this instead

Databases belong in private subnets with security groups that allow only the app tier.

Oops

Creating all subnets in one AZ.

Do this instead

Create each tier in at least two AZs so a load balancer and Multi-AZ database can survive an AZ outage.

Your mission

Four subnets, two AZs

~25 min

Quick check

What actually makes a subnet 'public' in AWS?

Go deeper

  1. 1Create one public and one private subnet and launch an instance in each.
  2. 2Confirm you cannot reach the private instance directly from the internet.
  3. 3Draw a diagram showing which tiers of a web app belong in which subnet type.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge