A packet's walk through a VPC, in AWS
Open inbound port 80 on a security group. Delete every outbound rule. Can the server still reply?
It sounds like it shouldn't. It can.
That server answers web clients all day. It just can't open a connection to the internet on its own. One word hides the trick: stateful. To see it, follow one packet from the internet to an EC2 instance, and watch which walls it hits.
The five things a packet meets
A VPC has a small cast, and each member does exactly one job.
- Internet Gateway (
IGW) attaches to the VPC itself, one per VPC. It is the VPC's door to the internet. - Route Table (
RT) decides where traffic goes next. - Subnet is a box for resources. It carries one
RTand oneNACL. - Network Access Control List (
NACL) filters at the subnet edge. - Security Group (
SG) filters at the instance.
The "VPC router" you see in architecture diagrams is not a real box. It is a picture of the RTs.
What actually makes a subnet "public"
Not a checkbox. A route.
A subnet is public when its RT holds an entry that sends traffic to the IGW. That entry, and nothing else, is the definition:
0.0.0.0/0 igw-xxxxx # everything else: out to the internet
10.0.0.0/24 local # traffic inside the VPC (default, can't be removed)
Those /24 and /16 blocks are just numbers, not places. The same block can live in three clouds at once without colliding, which is its own surprise: see Same /16, the big 3 cloud providers differ on how far it reaches.
One more rule people trip on: a subnet cannot attach to an IGW directly. The link runs through the RT.
Subnet → RT → IGW
Many RTs can point at the same IGW. But the arrow is a routing path, not ownership: the IGW hangs off the VPC, and the RT only points at it.
Two walls, and only one remembers you
A packet heading for your instance passes two filters. They are not the same kind of wall.
SG | NACL | |
|---|---|---|
| Level | the instance (its network interface, ENI) | the subnet |
| Memory | stateful | stateless |
| Rule direction | inbound and outbound; outbound rules only apply to connections the instance opens | inbound and outbound; replies also need an outbound rule to get out |
| Rules | allow only | allow and deny |
| Sources | CIDR, IP, prefix list, or another SG | CIDR only |
| Evaluation | all rules at once | numbered 1–32766, first match wins |
That is stateful. The SG tracks the connection, not the port. Allow a ping in, and the pong goes back out for free, no reverse rule. AWS's security group page puts it plainly: responses to allowed inbound traffic leave "regardless of the outbound rules". That resolves the opening puzzle: inbound 80 alone is enough to serve clients.
The NACL tracks nothing. It judges every packet alone, ping and pong alike, so a working flow needs a rule in both directions. The return rule is not port 80: the reply goes to the client's ephemeral port, and AWS's NACL guide suggests opening 1024-65535 to cover every kind of client.
Caveat: the two-wall split is an AWS quirk. Azure has no stateless layer: its Network Security Group is stateful whether attached to a subnet or a NIC. GCP gives you one stateful VPC firewall, targeted by network tag or service account. With no stateless
NACLunderneath, the "open the return direction" chore never shows up.
The path in, and the part everyone forgets
Trace the inbound packet end to end:
IGW → VPC routing delivers it to the subnet → NACL → SG → instance
Apart from "Everything set up", each option along the top of the embed below removes one setting. Pick "NACL: no outbound 1024–65535", for example, and the request still reaches the instance, but the reply is dropped at the NACL on its way out. Each press of Next moves the packet one step, and the panel shows its header at that point and which rule or route the component it has reached (RT, NACL or SG) checks.
Here is the part that surprises people: the inbound packet never uses 0.0.0.0/0 → IGW. It arrives addressed to the instance's public IP, and the IGW translates that to the private IP, which sits inside the VPC's range, so routing inside the VPC delivers it. 0.0.0.0/0 → IGW speaks up only on the reply, when the destination is some internet IP. So the route that makes a subnet public is for the trip out, not the trip in.
Both the RT and the NACL attach to the subnet, which is why people mix them up. They do different jobs: the RT decides your next hop, the NACL decides yes or no. One picks a path, the other filters.
The model to keep
A public subnet is a routing fact, not a switch. The route to the IGW works on the way out, not the way in. The RT picks the next hop, the NACL says yes or no, and only the SG remembers you. Hold those three and the VPC stops being a maze.
References
- Azure network security groups overview — Microsoft Learn. NSG is stateful; attaches to a subnet or a NIC.
- VPC firewall rules — Google Cloud. Firewall rules are stateful; targeted by network tag or service account.