skip to content

Subnets, CIDR & Route Tables

How you carve a VPC CIDR into public and private subnets spread across Availability Zones, and how route tables decide where each subnet's traffic goes. Interviewers ask you to draw this from scratch — 'what actually makes a subnet public?' is the most common AWS networking question there is.

part ofAWSoverview, primer and where to startread it →
on this pageshow

questions

5

In an AWS VPC, what actually makes a subnet "public" rather than "private", and what else does an EC2 instance in that subnet need before it can reach the internet?

level: juniorimportance: must knowfreq 88%

answer

  1. look at the route table, not the subnet
  2. one route entry decides it
  3. 0.0.0.0/0 and its target
  4. attaching a gateway is not routing to it
  5. instance still needs a routable address

basics

~20 s

A subnet is public only because the route table associated with it sends 0.0.0.0/0 to an internet gateway. The instance also needs a public IPv4 or Elastic IP address, or its packets have no return path.

solid answer

~50 s

Nothing on the subnet object itself marks it public. The only thing that makes a subnet public is that the route table associated with it has a route for `0.0.0.0/0` whose target is an internet gateway attached to that VPC. A subnet whose default route points somewhere else, or that has no default route at all, is private — two subnets in the same VPC can differ purely by which route table they are associated with. Routing alone is not enough: the instance also needs a globally routable address, either a public IPv4 assigned at launch or an Elastic IP, because the internet gateway performs a one-to-one translation between that public address and the instance's private address. Security groups and network ACLs still have to permit the traffic, but they are not what defines "public".

code

bash · 12 lines
bash
# Attach a gateway to the VPC (the door)
aws ec2 attach-internet-gateway --vpc-id vpc-0abc --internet-gateway-id igw-0abc

# The route that makes a subnet public (the sign)
aws ec2 create-route --route-table-id rtb-0pub \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc

# Associate the subnet with that table
aws ec2 associate-route-table --route-table-id rtb-0pub --subnet-id subnet-0aaa

# Give launches a routable address
aws ec2 modify-subnet-attribute --subnet-id subnet-0aaa --map-public-ip-on-launch

go deeper

for a junior

Be able to say plainly that a subnet is public because its route table sends 0.0.0.0/0 to an internet gateway, and that the instance still needs a public or Elastic IP address.

for a middle

Explain the full chain — gateway attached, route present in the associated table, routable address on the interface, filters permitting — and why longest-prefix match means the local route always wins inside the VPC.

for a senior

Show how you would diagnose 'my instance cannot reach the internet' in order, and describe the public/private tier layout you would actually build per Availability Zone.

for a principal

Own the guardrail angle: how you prevent a subnet from becoming internet-facing by accident across many accounts, and what you standardise so the public tier stays deliberately small.

## "Public" is a convention, not an attribute When you create a subnet in an Amazon VPC you give it a CIDR block and an Availability Zone. There is no field, tag or checkbox on the subnet that says *public*. The distinction lives entirely in the **route table** that the subnet is associated with. Engineers say "public subnet" as shorthand for "its route table has a default route pointing at an internet gateway", and that shorthand is exactly what an interviewer is probing. ## Ingredient one: an internet gateway attached to the VPC An internet gateway (IGW) is a VPC-level object. You create it and attach it to the VPC, and a VPC can have at most one attached at a time. Attaching it changes nothing on its own — it is a door in the wall of the VPC that no subnet has been told to walk through yet. Candidates who stop here are the ones who later ask why their instance still cannot reach the internet. ## Ingredient two: a route to it Every route table starts with a `local` route covering the VPC's CIDR, which is what lets subnets in the same VPC talk to each other. To make a subnet public you add one more route: ``` Destination Target 10.0.0.0/16 local 0.0.0.0/0 igw-0abc123 ``` The subnet is then associated with that table. Traffic to an address inside the VPC matches the more specific `local` route; everything else matches `0.0.0.0/0` and is handed to the gateway. A private subnet is simply one whose table lacks that entry, or whose default route points at something that is not an IGW. Route lookup is longest-prefix-match, so `local` always wins for intra-VPC destinations. That is also why a route table cannot be used to firewall traffic between subnets of the same VPC — the local route always covers it. ## Ingredient three: a globally routable address The internet gateway does **not** hide your private addresses behind a shared address. It performs a one-to-one mapping between an instance's private IPv4 address and the public address associated with that instance's network interface. If the instance has no public IPv4 and no Elastic IP, the gateway has nothing to map, so outbound packets are dropped and no reply could ever find its way back. There are two ways to get that address. The subnet attribute `MapPublicIpOnLaunch` (set with `aws ec2 modify-subnet-attribute --map-public-ip-on-launch`) auto-assigns a public IPv4 to instances launched into it — this is on for subnets of the default VPC and off for subnets you create yourself. Or you allocate an Elastic IP and associate it with the interface, which gives a stable address that survives stop/start. Note that this attribute is a frequent source of confusion: enabling it on a subnet whose route table has no IGW route gives instances a public address that is completely unreachable, and the reverse — an IGW route with no public address — gives a subnet everyone calls public that nothing can leave. ## Ingredient four: the filters must agree Security groups and network ACLs still evaluate the traffic. They can block a perfectly routed packet, but they never *create* reachability, and no security-group rule turns a private subnet into a public one. ## What this buys you architecturally The pattern this enables is the one you will be asked to draw: per Availability Zone, a small public subnet holding only internet-facing entry points, and a larger private subnet holding application instances and databases. The public/private split is enforced purely by which of two route tables each subnet is associated with, which is why the split is cheap to get right and cheap to get catastrophically wrong — moving one association is all it takes to expose a tier that was meant to be internal. A useful diagnostic habit: when something in a VPC cannot reach the internet, check in this order — is an IGW attached, does the subnet's *associated* route table have `0.0.0.0/0` to it, does the instance have a public or Elastic IP, and only then look at security groups and NACLs.

  • If an instance in that subnet has only a private IP, what happens to its outbound traffic?
    It never reaches the internet. The internet gateway maps one private address to one public address; with no public IPv4 or Elastic IP on the interface there is nothing to map, so the packets are dropped at the gateway. The route table looks correct, which is what makes this failure confusing to diagnose.
  • Can a route table make one subnet public and leave another private in the same VPC?
    Yes — that is the normal design. A route table is associated with subnets, and each subnet has exactly one association. You keep one table carrying the default route to the internet gateway and associate only your public subnets with it; everything else uses a table without that route.
  • Does enabling auto-assign public IPv4 on a subnet make it public?
    No. It only means instances launched there receive a public IPv4 address. Without a default route to an internet gateway that address is unreachable and unusable. The two settings are independent, and getting only one of them right is the most common reason a 'public' subnet does not work.

The internet gateway is a door in the building's wall; the route table is the sign in each corridor telling traffic to use it. A door nobody is directed to is still a wall.

saying these in an interview costs you the question

  • Claims subnets have a public/private flag you toggle
  • Says attaching an internet gateway to the VPC is enough
  • Thinks a security group rule makes a subnet public
  • Believes the internet gateway masquerades private IPs behind one address
  • Forgets the instance needs a public or Elastic IP

context

open as a page

You create a new subnet in an existing AWS VPC and never associate it with a route table. Which route table governs that subnet's traffic, and why does this trip teams up?

level: middleimportance: must knowfreq 52%

basics

~20 s

It falls back to the VPC's main route table through an implicit association. If that table has a 0.0.0.0/0 route to an internet gateway the new subnet is silently public; if it holds only the local route, nothing in it can leave the VPC.

open as a page

In an AWS VPC, how many usable IP addresses does a /24 subnet give you, and which addresses does AWS reserve in every subnet?

level: middleimportance: should knowfreq 58%

basics

~20 s

AWS reserves five addresses in every VPC subnet — the network address, the VPC router, the DNS resolver, one held for future use, and the last (broadcast) address — so a /24 gives 251 usable addresses, not 256.

open as a page

Launches into an AWS VPC subnet start failing because it has run out of free IPv4 addresses. What are your options, and why can't you simply make that subnet bigger?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A subnet's CIDR is immutable, so it cannot grow. You add capacity instead: carve a new subnet in the same Availability Zone from unused VPC space, or attach a secondary CIDR block to the VPC and build subnets from that, then reclaim addresses held by idle interfaces.

open as a page

You are setting the IPv4 addressing standard for a new AWS organization that will hold dozens of accounts and VPCs. How do you allocate CIDR ranges, and what are you optimizing for?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Give every VPC a non-overlapping slice of one organization-wide private supernet, sized with headroom and grouped so ranges summarize per region and environment. Overlap is the one decision you cannot reverse cheaply, so a central registry governs allocation.

open as a page