Public vs private subnet
Lesson 3 of 72 in AWS Interview Mastery. Free to read, with practice questions to try.
Public and private subnets
A public subnet is a subnet that can talk directly to the internet. A private subnet has no direct path to the internet. Both live inside your VPC, and each one is a slice of the VPC's address range, such as 10.0.1.0/24 in ap-south-1.
This split matters because it decides which resources can be reached from outside. A web server that customers visit belongs in a public subnet. A backend server that only other services should call belongs in a private subnet.
The route table decides
An internet gateway is the VPC component that connects the VPC to the internet. It does nothing on its own. A subnet uses it only when the subnet's route table has a default route, written 0.0.0.0/0, pointing at the gateway.
Take a VPC in ap-south-1 with two subnets. Subnet A's route table sends 0.0.0.0/0 to the internet gateway. Subnet B's route table has no such route.
| Subnet | Route table entries | Type |
|---|---|---|
| subnet-a (10.0.1.0/24) | 10.0.0.0/16 local; 0.0.0.0/0 to internet gateway | Public |
| subnet-b (10.0.2.0/24) | 10.0.0.0/16 local only | Private |
Names don't decide
It is easy to assume a subnet called public-web-subnet is public. The name is only a label. The route table is what makes a subnet public or private.
Here is a subnet in ap-south-1 named public-web-subnet. Its route table has only the local route for 10.0.0.0/16 and no 0.0.0.0/0 route. It is a private subnet, so a web server placed there cannot be reached directly from the internet, however it is named.
| Name | Route table | Actual type |
|---|---|---|
| public-web-subnet | 10.0.0.0/16 local only | Private |
| private-db-subnet | 10.0.0.0/16 local; 0.0.0.0/0 to internet gateway | Public |
The second row is a trap in the other direction: a name that sounds private does not make the subnet private either. Only the route table counts.
Public IP addresses
A resource in a public subnet also needs a public IP address before the internet can reach it. The route table provides the path, and the public IP gives the resource an address on that path. Both are needed.
Compare four resources in the same VPC. Only the first can be reached from the internet, because it has both a public subnet and a public IP, and its security group must also allow the traffic.
| Resource | Subnet | Public IP | Reachable from internet? |
|---|---|---|---|
| Web server | Public | Yes | Yes, if its security group allows the traffic |
| Web server | Public | No | No |
| Backend server | Private | No | No |
| Backend server | Private | Yes | No |
The last row shows that a public IP does not help in a private subnet. With no route to the internet gateway, the traffic has nowhere to go.
Where resources belong
Placement is a design choice. Put a resource in a public subnet only when it must accept traffic from the internet. Put backend resources in a private subnet when they should not be directly reachable.
A simple layout in ap-south-1 has two tiers. Customers reach the web server in a public subnet with a public IP. The backend server sits in a private subnet, so it has no direct internet path. The only way in from outside is through the web server.
- Web server: public subnet, public IP, internet-facing.
- Backend server: private subnet, no direct internet path.
You'll meet the rules that control which traffic is allowed later in the course.
Practice questions
- 1.
Subnet C's route table has only the local route for 10.0.0.0/16 and no default route. What type is subnet C?
- A.
Public
- B.
Private
- C.
Public, because it is inside the VPC
- D.
Public, because it has a local route
Show answer
Answer: B.Private
A subnet without a route to an internet gateway is private. The local route only lets subnets in the same VPC talk to each other, so it does not make the subnet public.
- A.
- 2.
Which resource should sit in a private subnet?
- A.
A backend server that only other services should call
- B.
A customer-facing web server
- C.
The internet gateway itself
- D.
A server in a public subnet with a public IP address
Show answer
Answer: A.A backend server that only other services should call
A backend server that only other services call should not be directly reachable, so it belongs in a private subnet. A web server that customers visit needs a public subnet, and an internet gateway is a VPC component, not a server that is placed in a subnet.
- A.
- 3.
Four resources sit in the same VPC. Which one can be reached from the internet?
- A.
A web server in a public subnet with no public IP address
- B.
A backend server in a private subnet with a public IP address
- C.
A web server in a public subnet with a public IP address
- D.
A backend server in a private subnet with no public IP address
Show answer
Answer: C.A web server in a public subnet with a public IP address
The web server needs both a public subnet, which provides the route, and a public IP, which provides the address. Removing the public IP or moving the server to a private subnet blocks inbound access, and a public IP does not help a server with no route to the gateway.
- A.
4 more questions and the timed quiz for this lesson open with a free account.
Lessons in How AWS Pieces Fit Together
- Region vs availability zone
- Vpc, subnet and route table
- Public vs private subnet
- Security group vs network acl
- Shared responsibility model
- Iam user vs role for workloads
- Aws organizations and service control policies
72 lessons across 9 topics. See the full course