AWS Builder Center
AWS VPC Made Simple: Public and Private Subnets

AWS VPC Made Simple: Public and Private Subnets

Follow a small website's traffic to understand VPCs, subnets, route tables, internet gateways, and security groups without getting lost in terminology.

Follow the traffic

Imagine a website with an application server and a database. Customers should reach the website, while the application should reach the database.
Those requirements introduce a network design question: which systems need a path to which other systems, and what traffic should be allowed along that path?
This article walks through a simplified IPv4 example. You do not need to create any AWS resources to follow it.

1. The VPC is the network you define

Amazon Virtual Private Cloud, or VPC, provides a logically isolated virtual network for AWS resources.
You choose an address range for it. For this example, use  10.0.0.0/16 .
The notation is called CIDR. For now, treat it as a compact way to describe a block of IP addresses. An address range identifies the space available to organize the example network.
The VPC itself does not mean that every resource inside it can communicate with every other resource or with the internet. Routing and traffic controls still matter.

2. Subnets divide the address space

A subnet uses part of the VPC's address range. Each subnet belongs to one Availability Zone, which is a location within an AWS Region.
For the example, plan two subnets:
Subnet
Example range
Intended resource
Public subnet
 10.0.1.0/24 
Application server
Private subnet
 10.0.2.0/24 
Database
Naming a subnet “public” does not create internet connectivity. Its routing configuration determines that property.
This two-subnet illustration explains connectivity. A production design also needs decisions about availability, scaling, and the requirements of its chosen database service.

3. Route tables direct traffic

A route table contains destinations and the next targets used to reach them. A subnet is associated with a route table.
Think of a route as a direction: “For traffic going to this address range, send it toward this target.”
A simplified public-subnet route table would include:
Destination
Target
Meaning
 10.0.0.0/16 
 local 
Route traffic within this VPC
 0.0.0.0/0 
Internet gateway
Send other IPv4 destinations toward the internet gateway
The more specific matching route takes precedence. Traffic to the example database address remains on the local route instead of taking the general internet route.

4. The internet gateway provides the internet path

An internet gateway attaches to the VPC and provides a route target for internet communication.
For direct internet communication through that gateway in this IPv4 example, the application server needs appropriate public addressing and routing. Applicable traffic controls and the server's application must also allow and handle the connection.
A subnet with a route to an internet gateway is public. A private subnet does not have that direct route.
The word “public” describes the subnet's routing. It does not automatically permit every incoming connection to every resource.

5. Security groups control permitted traffic

A security group controls allowed inbound and outbound traffic for associated resources.
For this example, you could design the rules around these requirements:
Resource
Intended inbound access
Application server
HTTPS on port 443 from the intended customer audience
Database
Its database port from the application server's security group
These are example requirements, not a complete deployable configuration. The application needs to be configured to serve HTTPS, and other applicable network controls still need to permit the connection.
Referencing the application's security group as the database rule's source expresses which application resources may connect. A local network route alone does not grant database access.

6. Walk through one customer request

In this simplified design:
  1. The customer's browser connects to the application's public HTTPS endpoint.
  1. The internet gateway provides the network path into the VPC.
  1. The applicable rules allow the connection to the application server.
  1. The application queries the database using its private address, permitted network access, and database credentials.
  1. The application prepares a response and returns it to the browser.
The customer does not need to connect directly to the database to use the website.

7. Where does a NAT gateway fit?

Sometimes a resource in a private subnet needs outbound internet access, for example to retrieve an update.
One IPv4 design sends that outbound traffic through a public NAT gateway in a public subnet and then through the internet gateway. The private resource can initiate the connection; the NAT gateway does not allow unsolicited internet connections back to that resource.
A NAT gateway is unnecessary for the VPC's ordinary local route between the example application server and database. It also introduces its own charges, so it is not something to create merely to follow this conceptual article.

Keep these two questions separate

Is there a route to the destination? Check route tables, gateways, and addressing.
Is the traffic permitted? Check security groups and other applicable network controls, followed by the destination application's access requirements.
Separating those questions makes a connection problem easier to investigate.
Discussion: Which part of VPC networking was most confusing at first: address ranges, route tables, or the meaning of a public subnet?

Further reading

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