DEV Community

Cover image for AWS Public vs Private Subnets Explained: Design a Secure Network Step by Step
Vishal Patil
Vishal Patil

Posted on

AWS Public vs Private Subnets Explained: Design a Secure Network Step by Step

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/24 is subnet A in AZ 1
  • 10.0.2.0/24 is 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:

  1. An Internet Gateway is attached to the VPC
  2. The subnet's route table has a route 0.0.0.0/0 → igw-xxxx
  3. The instance has a public IP or Elastic IP
  4. 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
Enter fullscreen mode Exit fullscreen mode

How traffic flows:

  1. The user's request goes through the Internet Gateway to the Load Balancer (public subnet).
  2. The load balancer forwards it to the app servers (private app subnet).
  3. The app servers talk to the database (private DB subnet).
  4. 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

  1. Create a VPC demo-vpc with CIDR 10.0.0.0/16.
  2. 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)
  3. Create an Internet Gateway and attach it to demo-vpc.
  4. Create a public route table, add 0.0.0.0/0 → IGW, and associate it with both public subnets.
  5. Enable Auto-assign public IPv4 address on the public subnets.
  6. Create a NAT Gateway in public-subnet-1 with a new Elastic IP.
  7. Create a private route table, add 0.0.0.0/0 → NAT Gateway, and associate it with both private subnets.
  8. Launch one EC2 instance in a public subnet and one in a private subnet.
  9. Connect to the public instance, then SSH from it to the private instance using its private IP.
  10. From the private instance, run curl https://aws.amazon.com and confirm it works through the NAT Gateway.
  11. 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:

vishalcloudjourney

Top comments (0)