Networking Tutorial · Cloud Networking · Lesson 23 of 28
Public and Private Subnets
Build the classic layout: public subnets with an internet gateway, private subnets with a NAT gateway, across availability zones.
The difference is the route table
| Public subnet | Private subnet | |
|---|---|---|
Default route (0.0.0.0/0) |
Internet gateway | NAT gateway (in a public subnet) |
| Resources get public IPs | Yes | No |
| Reachable from the internet | Yes, if security groups allow | No |
| Put here | Load balancers, NAT gateways, bastions | Apps, containers, databases |
An internet gateway connects a VPC to the internet for resources with public IPs. A NAT gateway lets private resources make outbound connections (to download packages or call APIs) without being reachable from outside.
Spread across availability zones
Create one public and one private subnet in each of two or three availability zones, and one NAT gateway per zone, so losing a zone doesn't take your outbound traffic with it.
import ipaddress
vpc = ipaddress.ip_network("10.20.0.0/16")
zones = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
blocks = vpc.subnets(new_prefix=20)
layout = []
for tier in ["public", "private"]:
for zone in zones:
net = next(blocks)
route = "igw" if tier == "public" else f"nat-{zone[-1]}"
layout.append((tier, zone, net, route))
for tier, zone, net, route in layout:
print(f"{tier:8} {zone} {str(net):15} 0.0.0.0/0 -> {route}")
print("remaining /20 blocks for later:", len(list(blocks)))
Output
public eu-west-1a 10.20.0.0/20 0.0.0.0/0 -> igw public eu-west-1b 10.20.16.0/20 0.0.0.0/0 -> igw public eu-west-1c 10.20.32.0/20 0.0.0.0/0 -> igw private eu-west-1a 10.20.48.0/20 0.0.0.0/0 -> nat-a private eu-west-1b 10.20.64.0/20 0.0.0.0/0 -> nat-b private eu-west-1c 10.20.80.0/20 0.0.0.0/0 -> nat-c remaining /20 blocks for later: 10
The same layout elsewhere
- Azure: subnets have no "public" flag; inbound internet access comes through a public load balancer or application gateway, and outbound through a NAT gateway attached to the subnet.
- Google Cloud: VMs without external IPs use Cloud NAT for outbound traffic; firewall rules control inbound access.
Test yourself
What makes a subnet 'public' on AWS?
Its route table sends 0.0.0.0/0 to an internet gatewayIt uses a public address rangeIt has no security groupsIt contains a NAT gateway
Show answer
Answer: Its route table sends 0.0.0.0/0 to an internet gateway
Public and private subnets use the same private address ranges; only the default route differs.