What is the difference between a public subnet and a private subnet within a VPC?
Short answer
A public subnet has a route table entry directing traffic to an internet gateway, allowing resources within it to communicate directly with the internet, while a private subnet lacks this direct route, meaning resources within it cannot be reached from or initiate outbound…
Advertisement
AModel answer
A public subnet has a route table entry directing traffic to an internet gateway, allowing resources within it to communicate directly with the internet, while a private subnet lacks this direct route, meaning resources within it cannot be reached from or initiate outbound connections directly to the internet, unless traffic is routed through something like a NAT Gateway, making private subnets the appropriate choice for resources like databases that should never be directly exposed to the public internet.
Example
aws ec2 create-route --route-table-id rtb-12345 --destination-cidr-block 0.0.0.0/0 --gateway-id igw-12345
Real-world example
A company places its web servers in a public subnet so customers can reach them directly over the internet, while placing its database servers in a private subnet, ensuring the database can never be directly accessed from outside the VPC.
Likely follow-ups
- How does a resource in a private subnet access the internet for software updates if needed?
- What is the relationship between a subnet and an Availability Zone?
Related topics