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?
answer
- look at the route table, not the subnet
- one route entry decides it
- 0.0.0.0/0 and its target
- attaching a gateway is not routing to it
- instance still needs a routable address
basics
~20 sA 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 sNothing 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# 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-launchgo deeper
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.
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.
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.
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