Cloud networking basics
Cloud networking is how you build a private network inside AWS, Azure or GCP: a VPC split into public and private subnets, route tables that decide where traffic goes, gateways to reach the internet, and two layers of firewall. Most new apps are deployed in the cloud, so every developer and network engineer is expected to know this.
271.1 Why is it used?
Story: a startup moves its web app and database to the cloud.
- The web server must be reachable by every user on the internet: customers open the website on it.
- The database holds customer data, so nobody on the internet should connect to it directly.
- But the database still has to download security updates (outbound access).
- Cloud networking answers exactly this: web public, database private, with controlled outbound.
- Step 1
1.Internet user 198.51.100.50 sends an HTTP request
- Step 2
2.Internet gateway (IGW) forwards it into the VPC
- Step 3
3.Web server 10.0.1.10 in the public subnet replies via the IGW
- Step 4
4.Database 10.0.2.10 in the private subnet: no access from the internet
- Step 5
5.Database -> NAT gateway 10.0.1.20 -> IGW -> update server 198.51.100.80
Inbound only reaches the public subnet; the private subnet goes out only through the NAT gateway
271.2 VPC and subnets
- VPC (Virtual Private Cloud): a logically isolated virtual network, like a traditional network in your own data center (AWS docs).
- The VPC gets a CIDR range, here 10.0.0.0/16.
- A subnet is a slice of the VPC range and lives in exactly one Availability Zone (AZ).
- Public subnet 10.0.1.0/24: web server and NAT gateway.
- Private subnet 10.0.2.0/24: database.
271.3 How it works: route table + internet gateway
- What makes a subnet public is its route table, nothing else.
- Every route table has a local route: 10.0.0.0/16 stays inside the VPC.
- Public subnet route table: 0.0.0.0/0 -> internet gateway.
- The IGW is highly available and also does NAT for resources that have a public IPv4 address.
- User -> web server's public IP -> IGW -> web server; the reply also returns through the IGW.
| Route table | Destination | Target |
|---|---|---|
| Public RT | 10.0.0.0/16 | local |
| Public RT | 0.0.0.0/0 | Internet gateway |
| Private RT | 10.0.0.0/16 | local |
| Private RT | 0.0.0.0/0 | NAT gateway |
271.4 Private subnet: outbound only via NAT gateway
- The private route table sends 0.0.0.0/0 to the NAT gateway instead of the IGW.
- In AWS a public NAT gateway is created in a public subnet with an Elastic IP attached.
- The NAT gateway replaces the database's source IP with its own, then the packet goes via the IGW.
- It remembers who started the connection, so the reply comes back to the database.
- A new connection from the internet to the database is dropped at the IGW: the database has no public IP.
- Step 1
1.Database 10.0.2.10 sends a request to 198.51.100.80
- Step 2
2.Private RT: 0.0.0.0/0 -> NAT gateway 10.0.1.20
- Step 3
3.NAT gateway rewrites the source to its public IP 198.51.100.20
- Step 4
4.Packet goes through the IGW to the update server
- Step 5
5.Reply returns to the NAT gateway; conntrack maps it back to 10.0.2.10
271.5 Security group vs NACL
- Inbound path: user -> subnet NACL -> instance security group -> web server.
- Security group: instance level, allow rules only, stateful: the reply is allowed automatically.
- NACL: subnet level, allow and deny rules, checked in order from the lowest number, stateless.
- Because a NACL forgets the request, the reply is dropped unless there is an outbound rule for it.
- AWS docs: open the ephemeral ports 1024-65535 as an outbound NACL rule for replies.
| Point | Security group | Network ACL |
|---|---|---|
| Applies at | Instance | Subnet |
| Rules | Allow only | Allow + deny |
| Evaluation | All rules together | Numbered order, lowest first |
| State | Stateful: reply auto-allowed | Stateless: reply needs its own rule |
271.6 VPC peering
- Peering connects two VPCs so they route to each other using private IPs, even across accounts or regions.
- Both sides must add a route in their route tables.
- Not transitive: A-B and B-C peered does not mean A can reach C through B.
- CIDRs must not overlap: two VPCs both using 10.0.0.0/16 cannot be peered at all.
- Step 1
1.VPC A 10.0.0.0/16 <-> VPC B 10.1.0.0/16: peered, works
- Step 2
2.VPC B 10.1.0.0/16 <-> VPC C 10.2.0.0/16: peered, works
- Step 3
3.VPC A -> VPC C through B: not possible (no transit)
- Step 4
4.Fix: peer A and C directly
271.7 Same ideas in Azure and Google Cloud
| AWS | Azure | Google Cloud |
|---|---|---|
| VPC | VNet | VPC network: global, subnets regional |
| Security group / NACL | NSG: allow/deny, stateful, priority 100-4096 | Firewall rules, stateful |
| NAT gateway | Azure NAT Gateway: outbound only, no unsolicited inbound | Cloud NAT |
271.8 Practical: a route table per subnet (local Docker VPC model)
- Not a real cloud: one router container plays the VPC router + IGW; containers are joined only by veth links, so no packet leaves the lab.
- Policy routing (ip rule) picks a route table by incoming interface, just like a subnet's route table.
# router (VPC router + IGW model)ip rule show | grep -E 'rt'ip route show table public-rtip route show table private-rt# inet (fake internet 198.51.100.0/24)ip route$ ip rule show | grep -E 'rt'1001: from all iif br-pub lookup public-rt1002: from all iif r-priv lookup private-rt$ ip route show table public-rtdefault via 198.51.100.254 dev igw010.0.1.0/24 dev br-pub scope link10.0.2.0/24 dev r-priv scope link$ ip route show table private-rtdefault via 10.0.1.20 dev br-pub10.0.1.0/24 dev br-pub scope link10.0.2.0/24 dev r-priv scope link[inet]$ ip route198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.50- Public RT default -> igw0 (IGW); private RT default -> 10.0.1.20 (NAT gateway).
- inet only knows 198.51.100.0/24, so private 10.x addresses are unreachable from the internet.
271.9 Practical: web public, db private
# inet: internet user 198.51.100.50curl -sS -m 3 http://198.51.100.10/ping -c 2 -W 1 10.0.2.10# db: private subnet 10.0.2.10curl -sS -m 3 http://198.51.100.80/# natgw: connection memoryconntrack -L -p tcp 2>&1 | head -3$ curl -sS -m 3 http://198.51.100.10/web1 home page, client seen as 198.51.100.50$ ping -c 2 -W 1 10.0.2.10ping: connect: Network unreachable[db]$ curl -sS -m 3 http://198.51.100.80/update-pkg 2.1, client seen as 198.51.100.20[natgw]$ conntrack -L -p tcp 2>&1 | head -3tcp 6 116 TIME_WAIT src=10.0.2.10 dst=198.51.100.80 sport=57988 dport=80 src=198.51.100.80 dst=10.0.1.20 sport=80 dport=57988 [ASSURED] mark=0 use=1conntrack v1.4.8 (conntrack-tools): 1 flow entries have been shown.- The update server sees 198.51.100.20: the NAT gateway's public IP, not the database.
- The conntrack entry is the NAT gateway's memory that brings the reply back to 10.0.2.10.
271.10 Practical: a stateless NACL forgets the reply
- nft rules on the router are checked in order, like numbered NACL rules.
- Version 1: inbound port 80 allowed, outbound only port 443.
# NACL idea for the public subnet: statelesstable ip nacl { chain subnet { type filter hook forward priority 0; iifname igw0 tcp dport 80 counter accept iifname igw0 counter drop oifname igw0 tcp dport 443 counter accept oifname igw0 counter drop }}$ curl -sS -m 3 http://198.51.100.10/curl: (28) Connection timed out after 3001 milliseconds$ nft list table ip nacl | grep counteriifname "igw0" tcp dport 80 counter packets 3 bytes 180 acceptiifname "igw0" counter packets 0 bytes 0 dropoifname "igw0" tcp dport 443 counter packets 0 bytes 0 acceptoifname "igw0" counter packets 5 bytes 300 drop- The request got in (3 packets on the port 80 rule) but the 5 reply packets hit the last drop rule.
- Version 2 adds one outbound rule for the ephemeral ports, before the final drop:
oifname igw0 tcp dport 1024-65535 counter accept$ curl -sS -m 3 http://198.51.100.10/web1 home page, client seen as 198.51.100.50$ nft list table ip nacl | grep counteriifname "igw0" tcp dport 80 counter packets 7 bytes 436 acceptoifname "igw0" tcp dport 1024-65535 counter packets 5 bytes 360 acceptoifname "igw0" counter packets 0 bytes 0 drop- Now the curl works and the new rule counts the 5 reply packets.
- A stateful (security group style) rule would use ct state established,related accept, but the nft_ct module was not loaded in this lab, so it was not run.
271.11 Common mistakes
- Myth: a private subnet has no internet at all. Fact: it just has no direct IGW route; it can still go out via a NAT gateway.
- Deleting the private RT's 0.0.0.0/0 -> NAT route: only the local route remains, so the database cannot download updates.
- Opening only the request port in a NACL and forgetting the outbound ephemeral ports: requests time out.
- Expecting peering to be transitive, or planning VPCs with overlapping CIDRs.
- Putting the public NAT gateway in the private subnet: it must sit in a public subnet.
271.12 Interview tips
- SG vs NACL: SG is instance level, allow only, stateful; NACL is subnet level, allow + deny, numbered, stateless (needs ephemeral port rules).
- A subnet is public only because its route table sends 0.0.0.0/0 to an internet gateway.
- Private subnet = outbound via NAT gateway, no new inbound connections from the internet.
- VPC peering: not transitive, CIDRs must not overlap, routes needed on both sides.
- Know the Azure (VNet, NSG) and GCP (global VPC, regional subnets) equivalents.