AWS Builder Center

Public vs Private Subnets in AWS: What's the Difference?

Learn what actually makes a subnet public or private, where to put web servers and databases, and prove it yourself by breaking and fixing a route.

Series: AWS Beginners Guide (9 articles)

  1. …
  2. 9
    Public vs Private Subnets in AWS: What's the Difference? This article
Software Engineering Undergraduate | AWS | Cloud & DevOps | Full-Stack Developer

Two servers, two different jobs

Say you run a small web app. There's a web server that visitors need to reach, and a database holding every user's email and password hash. Both live in your VPC.
The web server has to be reachable from anywhere. The database should be reachable from exactly one place, the web server. If a stranger on the internet can even attempt a connection to it, you've already made a design mistake, whether or not the password is strong.
Public and private subnets are how you build that split. The web server goes in one, the database in the other, and the network itself enforces the difference.



The one-line answer

A subnet is public if its route table has a route sending internet traffic to an internet gateway. Otherwise it's private.
That's all. Nothing in the subnet's settings says "public", and the name  public-subnet-1  has no effect. It's a label you typed. The route table is the only thing that counts, and plenty of people have a subnet named "private" that is quietly public because someone added one route to fix a different problem.

Public subnet
Private subnet
Route to internet gateway
Yes ( 0.0.0.0/0  to  igw-... )
No
Instances usually get a public IP
Yes
No
Reachable from the internet
Yes, if the security group allows it
No
Can reach the internet itself
Yes
Only through a NAT gateway
Typical residents
Load balancers, web servers, NAT gateways
Databases, app servers, internal services



What a server needs to be reachable

Three things all have to be true before someone on the internet can talk to an instance:
  1. Its subnet has a route to an internet gateway. Without it, packets have no path.
  1. The instance has a public IPv4 address (or an Elastic IP). Without one, the internet has nothing to send packets to.
  1. Its security group allows the traffic. Without a rule for the port, the packets are dropped on arrival.
Miss any one and it's unreachable. That's useful when you're locking things down, and infuriating when you're debugging at midnight. Almost every "I can't connect to my server" problem is one of these three, and the troubleshooting table below is built around them.
The default VPC makes this easy to get wrong. Every subnet in it is public, and new instances get a public IP automatically, so the only thing standing between a new server and the open internet is its security group. That's one layer, where a private subnet gives you two.



How the layers usually fit together

The standard layout for a web application looks like this:
Internet
|
[ Internet gateway ]
|
Public subnets --> Load balancer, NAT gateway
|
Private subnets --> App servers (the Auto Scaling group)
|
Private subnets --> Database
Visitors only ever talk to the load balancer. The load balancer passes requests to servers in private subnets. Those servers talk to the database. No one from outside can open a connection to anything but the load balancer.
If you built the Auto Scaling setup from the earlier article, your instances were in public subnets with public IPs, which is fine for learning. In a real deployment you'd move them to private subnets, keep the load balancer public, and nothing else would change for visitors.
Spread each layer across at least two Availability Zones. A subnet lives in one zone, so a design with one private subnet has one zone's worth of resilience, however many servers you put in it.



"But my private server needs updates"

A private instance has no route to the internet, so  dnf update  just sits there. Servers still need patches, package downloads, and calls to external APIs. There are two ways to solve it:
  • NAT gateway. It lives in a public subnet. Private subnets send their internet-bound traffic to it, and it forwards that traffic out. Replies come back, but nobody outside can start a connection inward. It's a one-way door. It also bills hourly, which is why it gets its own article later in this series.
  • VPC endpoints. For AWS services like S3, an endpoint gives your private subnet a direct path to the service without touching the internet at all. The S3 gateway endpoint has no hourly charge.
For anything beyond AWS services, you need the NAT gateway.



"And how do I log in to it?"

If a private server has no public IP, you can't SSH from your laptop. The usual options:
Option
How it works
Bastion host
A small instance in a public subnet. You SSH to it, then hop to the private server
Session Manager
Browser or CLI access through AWS Systems Manager. No open ports, no keys. Covered later in the series
EC2 Instance Connect Endpoint
An AWS-managed way to reach private instances over SSH from the console or CLI
Session Manager is the cleanest, and it removes the need to open port 22 anywhere. We'll build it in the Systems Manager article. For this one, we'll use the bastion approach because it makes the network visible.



What it costs

Subnets and route tables are free. The same two items from the VPC article apply here: public IPv4 addresses (about $0.005 per hour each) and NAT gateways (about $0.045 per hour plus data). This guide doesn't create a NAT gateway. Check current pricing before you build anything permanent.



Hands-on: build both and break one

Step 1: Get a VPC with both kinds of subnet

If you still have the  demo  VPC from the VPC article, use it. If you deleted it during cleanup, recreate it. Open VPC → Create VPC → VPC and more and set:
Setting
Value
Name tag auto-generation
 demo 
IPv4 CIDR block
 10.42.0.0/16 
Availability Zones
2
Public subnets
2
Private subnets
2
NAT gateways
None
VPC endpoints
None
Open the Resource map and note the subnet CIDR ranges the wizard picked. You'll need the private one in a moment.

Step 2: Create two security groups

Go to EC2 → Security Groups → Create security group, with the VPC set to  demo-vpc .
 bastion-sg 
  • Inbound: SSH, port 22, source My IP
 private-sg 
  • Inbound: Custom TCP, port 8080, source bastion-sg (select the group from the dropdown)
The second rule is the interesting one. It says "port 8080 is open, but only to instances in  bastion-sg ." Nothing else in the world matches, even another instance in the same subnet.

Step 3: Launch the private instance

Go to EC2 → Launch instance:
  • Name:  private-app 
  • AMI: Amazon Linux 2023, type  t3.micro 
  • Key pair: the one you already use
  • Network settings → Edit: VPC  demo-vpc , subnet a private one, Auto-assign public IP: Disable
  • Security group:  private-sg 
  • Under Advanced details → User data:
#!/bin/bash
mkdir -p /srv/site
echo "Hello from the private subnet" > /srv/site/index.html
systemd-run --unit=private-app python3 -m http.server 8080 --directory /srv/site
This starts a tiny web server using Python, which is already installed. It needs no internet access, and that matters, because the instance has no route to it.
Once it's running, copy its private IPv4 address from the instance details. It'll look like  10.42.130.25 .

Step 4: Launch the bastion

Launch a second  t3.micro  named  bastion :
  • Subnet: a public one
  • Auto-assign public IP: Enable
  • Security group:  bastion-sg 

Step 5: Reach the private server from the bastion

SSH into the bastion using the public IP:
ssh -i my-key.pem ec2-user@<bastion-public-ip>
Then from inside the bastion:
curl http://<private-ip>:8080
You should get "Hello from the private subnet." Your laptop could never do this. The private server has no public address, and its security group only admits the bastion.
Try one more thing while you're in there:
curl -m 5 http://<private-ip>:22
This one times out or gets refused, because  private-sg  has no rule for port 22 from anywhere. Closed means closed.

Step 6: Break the public subnet on purpose

This part is worth doing, because it turns the one-line definition from earlier into something you've seen.
  1. Keep your SSH session to the bastion open
  1. Go to VPC → Route tables and open the public route table for  demo-vpc 
  1. On the Routes tab, click Edit routes and delete the  0.0.0.0/0  route that points to the internet gateway. Save.
Go back to your terminal and type something. Within a few seconds the session freezes, because replies can no longer leave the VPC. Try opening a new SSH connection from another terminal. It times out.
Nothing about the instance changed. It still has the same public IP, the same security group, and the same running Nginx or whatever you put on it. The subnet is now private, because it lost its route.
Put the route back (Add route, destination  0.0.0.0/0 , target Internet Gateway, pick  demo-igw ). Your SSH connection may recover on its own or you can reconnect. The subnet is public again.
The wizard's public subnets share one route table, so this affected both of them.



Clean up

  1. Terminate  bastion  and  private-app 
  1. Delete the two security groups ( private-sg  first, since it refers to  bastion-sg )
  1. If you're finished with the VPC, select it in the VPC console and choose Actions → Delete VPC
  1. Check EC2 → Instances to confirm nothing is still running



Troubleshooting

What you see
Usual cause
Can't SSH to the bastion, times out
One of the three: no internet gateway route, no public IP, or no port 22 rule for your current IP
SSH worked yesterday, times out today
Your home IP changed and the security group still has the old one
Bastion can't reach the private server
The security group on the private instance doesn't allow the bastion's group on that port
 curl  to the private server hangs
Wrong port, or the service didn't start. Check user data finished with  sudo systemctl status private-app  on the private instance
Private instance can't download packages
Expected. No NAT gateway, so no route out
"Public" instance has a public IP but isn't reachable
Its subnet's route table is missing the internet gateway route
Private instance suddenly has internet access
Someone added a  0.0.0.0/0  route to a gateway on its route table. Look at the table



Common mistakes

Trusting the name. A subnet called  private  with a route to an internet gateway is public. Read the route table, not the label.
Auto-assign public IP left on. It's a subnet setting, and on public subnets it's often enabled by default. An instance you meant to keep private can end up with a public address. Check the setting when you launch.
Databases in public subnets. A good security group makes it safe on a normal day. It does nothing for you on the day someone edits the group by mistake.
Calling "private" the same as "secure". A private subnet is one layer. Security groups, patching, and sensible passwords still matter, and a compromised web server in front of your database is still a way in.
One private subnet. If that Availability Zone has an outage, so does everything in it.
Opening SSH to the world.  0.0.0.0/0  on port 22 invites constant scanning within minutes. Use My IP, or skip SSH entirely and use Session Manager.



Quick reference

  • Public vs private is decided by the route table, not the name
  • A public subnet has a route from  0.0.0.0/0  to an internet gateway
  • Reachable from the internet needs all three: a route, a public IP, and a security group rule
  • Put load balancers and NAT gateways in public subnets. Put app servers and databases in private ones
  • Private instances reach the internet through a NAT gateway, and reach AWS services through VPC endpoints
  • To log in to private instances: bastion host, or Session Manager
  • Use two or more Availability Zones for every layer



What comes next

We've used the internet gateway in every diagram so far and never looked at it. The next article opens it up: what it does, how it translates between private and public addresses, and what happens when you detach it. The NAT gateway article follows, since that's where private subnets finally get their way out.
Before you move on, rebuild the break-it exercise with a twist. Put the bastion in the private subnet, give it the public route table, and predict whether SSH works before you test it.


Stuck on a timeout? Paste the route table entries and the security group's inbound rules into a comment and I'll point at the one that's wrong.


<!-- SEO: paste these into the Builder Center fields, not the article body -->
Tags (5):
 amazon-vpc   networking   amazon-ec2   tutorials   aws-beginners 
SEO title (32 characters): Public vs Private Subnets in AWS
Description (about 140 characters): Learn the real difference between public and private subnets in AWS, how route tables decide it, and where to put web servers and databases.
Keywords: public vs private subnet aws, aws subnet, aws route table, internet gateway, nat gateway, aws vpc architecture, bastion host, private subnet ec2

Series: AWS Beginners Guide (9 articles)

  1. …
  2. 9
    Public vs Private Subnets in AWS: What's the Difference? This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article