published: true
description: "What subnets are, what really makes a subnet public or private, and how to design a secure, highly available layout with NAT Gateway and multiple AZs."
tags: aws, devops, networking, beginners
canonical_url:https://builder.aws.com/content/3MXQwuqymaOqnqiH4c1ivRd4oHd/aws-public-vs-private-subnets-explained-design-a-secure-network-step-by-step
This article was first published on AWS Builder Center. Sharing it here for the dev.to community.
Introduction
In my last post I explained VPCs and CIDR blocks. Once you have a VPC, the next step is dividing it into subnets. This is where most security decisions are made: which servers can be reached from the internet, and which ones stay hidden.
Many beginners think a subnet is "public" or "private" because of its name. It is not. In this post I explain what subnets are, what really makes a subnet public or private, and how to design a secure, highly available layout.
What is a Subnet?
A subnet is a smaller network inside your VPC. You create it by taking a slice of the VPC's CIDR block.
Key facts:
- A subnet lives in one Availability Zone (it can't span AZs)
- Its CIDR must be inside the VPC CIDR and must not overlap other subnets
- AWS reserves 5 IPs in every subnet
- Every subnet is associated with one route table at a time
Example: VPC 10.0.0.0/16
-
10.0.1.0/24is subnet A in AZ 1 -
10.0.2.0/24is subnet B in AZ 2
Public vs Private Subnets
| Public subnet | Private subnet | |
|---|---|---|
| Route to Internet Gateway | Yes (0.0.0.0/0 → IGW) |
No |
| Resources get public IP | Yes (public IP or Elastic IP) | No (private IP only) |
| Reachable from internet | Yes (if security group allows) | No |
| Outbound internet access | Directly through IGW | Through NAT Gateway (if needed) |
| Typical resources | Load balancers, bastion hosts, NAT Gateway, web servers | App servers, databases, cache, Jenkins agents |
What Really Makes a Subnet Public?
The name doesn't matter. A subnet is public only when all of these are true:
- An Internet Gateway is attached to the VPC
- The subnet's route table has a route
0.0.0.0/0 → igw-xxxx - The instance has a public IP or Elastic IP
- The security group and NACL allow the traffic
If the route to the IGW is missing, the subnet is private, no matter what you named it.
Public subnet route table
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 0.0.0.0/0 | igw-xxxx |
Private subnet route table
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 0.0.0.0/0 | nat-xxxx (optional) |
The local route lets all resources inside the VPC talk to each other.
How Private Subnets Reach the Internet
Private servers often need to download OS updates, pull Docker images or call external APIs, but should not be reachable from outside. The solution is a NAT Gateway:
- Place the NAT Gateway in a public subnet with an Elastic IP
- Add a route in the private route table:
0.0.0.0/0 → NAT Gateway - Private instances can start outbound connections
- The internet cannot start connections to them
Cost tip: A NAT Gateway charges per hour and per GB of data. For learning labs, delete it when you're done. In production, use VPC Endpoints for S3 and DynamoDB to reduce NAT costs.
A Secure 3-Tier Subnet Design
A common and safe layout for a web application:
VPC 10.0.0.0/16
│
├── Public subnets (AZ-1: 10.0.1.0/24, AZ-2: 10.0.2.0/24)
│ ├── Application Load Balancer
│ ├── NAT Gateway
│ └── Bastion host (optional)
│
├── Private app subnets (AZ-1: 10.0.11.0/24, AZ-2: 10.0.12.0/24)
│ └── Application servers (EC2 / ECS / EKS)
│
└── Private DB subnets (AZ-1: 10.0.21.0/24, AZ-2: 10.0.22.0/24)
└── RDS database
How traffic flows:
- The user's request goes through the Internet Gateway to the Load Balancer (public subnet).
- The load balancer forwards it to the app servers (private app subnet).
- The app servers talk to the database (private DB subnet).
- The database is never exposed to the internet, and only the app's security group can reach it.
Hands-On: Build Public and Private Subnets
- Create a VPC
demo-vpcwith CIDR10.0.0.0/16. - Create 4 subnets:
-
public-subnet-1:10.0.1.0/24(AZ 1) -
public-subnet-2:10.0.2.0/24(AZ 2) -
private-subnet-1:10.0.11.0/24(AZ 1) -
private-subnet-2:10.0.12.0/24(AZ 2)
-
- Create an Internet Gateway and attach it to
demo-vpc. - Create a public route table, add
0.0.0.0/0 → IGW, and associate it with both public subnets. - Enable Auto-assign public IPv4 address on the public subnets.
- Create a NAT Gateway in
public-subnet-1with a new Elastic IP. - Create a private route table, add
0.0.0.0/0 → NAT Gateway, and associate it with both private subnets. - Launch one EC2 instance in a public subnet and one in a private subnet.
- Connect to the public instance, then SSH from it to the private instance using its private IP.
- From the private instance, run
curl https://aws.amazon.comand confirm it works through the NAT Gateway. - Clean up: terminate the instances, delete the NAT Gateway, release the Elastic IP, then delete the IGW, subnets and VPC.
Best Practices
- Keep databases and app servers in private subnets
- Use at least 2 AZs with a public and private subnet in each
- Use one NAT Gateway per AZ in production for high availability
- Don't open SSH (port 22) to
0.0.0.0/0; use your own IP, a bastion host or Session Manager - Use separate subnets for each tier (web, app, database)
- Use VPC Endpoints to reach S3 and DynamoDB privately
- Turn on VPC Flow Logs for troubleshooting and auditing
- Plan CIDR ranges so you have spare room for future subnets
Common Mistakes to Avoid
- Naming a subnet "public" but forgetting the IGW route
- Giving an instance no public IP in a public subnet and wondering why it's unreachable
- Putting the NAT Gateway in a private subnet (it must be in a public one)
- Using only one AZ, so one failure takes everything down
- Leaving the NAT Gateway running after a lab and paying for it
- Placing a database in a public subnet
Quick Summary
| Concept | Remember this |
|---|---|
| Subnet | A slice of the VPC CIDR, in one AZ |
| Public subnet | Route to an Internet Gateway |
| Private subnet | No direct route to the internet |
| NAT Gateway | Lives in a public subnet; gives private subnets outbound-only internet |
| Golden rule | Databases and app servers go in private subnets |
Conclusion
Subnets are where network design meets security. Understand that a subnet is public only because of its route table, keep your sensitive resources in private subnets, and spread everything across multiple Availability Zones. Get this right and your applications will be both secure and highly available.
In my next post I'll cover Route Tables, Internet Gateway and NAT Gateway in more detail. Follow along if you're learning AWS networking too.
Connect with me:
- LinkedIn: Vishal Patil
- GitHub: VishalPatil-DevOps-AWS
- dev.to: @vishal_patil_aws
Top comments (0)