Your own gated estate
Picture a gated housing estate in a big city. It's yours: nobody else's houses are inside the fence, and you choose the house numbers. You get one block of numbers, say every number that starts with 10.0, and every house in the estate gets one of them.
That estate is your VPC. The city has lots of other estates, but they can't wander into yours.
The estate is built on a few separate plots of land across the city, each with its own power and water. You lay out streets on each plot, and each street sits on exactly one plot. Put houses on streets on two plots, so that if one plot loses power, the houses on the other still have the lights on.
What a VPC is
A virtual private cloud (VPC) is a logically isolated virtual network in one AWS Region. You give it an
IPv4 CIDR block, such as 10.0.0.0/16 (65,536 addresses), and every resource inside gets a private address
from it. AWS allows VPC blocks from /16 to /28; use a private (RFC 1918) range, and pick one that won't
overlap with networks you may later connect to, because overlapping ranges can't be peered or routed.
| Term | Meaning |
|---|---|
| VPC | Your isolated network in one Region, with one or more CIDR blocks |
| Subnet | A slice of the VPC's range, such as 10.0.1.0/24, that lives in exactly one Availability Zone |
| Availability Zone (AZ) | One or more data centres in a Region with independent power and networking |
| ENI | An elastic network interface: the virtual network card that holds an instance's private IP |
A subnet can't span AZs (see Regions and Availability Zones), so high availability means at least one subnet per tier in each of two or more AZs.
AWS reserves five addresses in every subnet (the first four and the last), so a /24 gives you 251 usable
addresses. Every account starts with a default VPC (172.31.0.0/16) in each Region, convenient for
experiments but rarely what you want in production.
Streets with a gate, streets without
The estate has one main gate onto the city's roads. Every street has a signpost at its end that says which way to go for each address. Every signpost already says "anything inside the estate: just walk there".
On some streets, the signpost also says "anywhere else: go to the main gate". Those are the busy front streets, called public subnets. A house there also needs an outside address on its door, so letters from the city can find it.
The other streets have no road to the gate at all. Visitors from the city can't get in, and the houses there can't get out. That's where you keep the houses that should stay quiet.
Public and private subnets
Every subnet is associated with exactly one route table (the VPC's main route table unless you associate a
custom one). Every route table has a local route for the VPC's CIDR, which can't be removed, so all subnets in
a VPC can reach each other. When several routes match, the most specific one wins (longest prefix match).
An internet gateway (IGW) attaches to the VPC and connects it to the internet. It's managed by AWS, horizontally scaled and highly available, so it isn't a bottleneck or a single point of failure.
- A public subnet has a route
0.0.0.0/0 โ igw-โฆ. An instance there also needs a public IPv4 address or an Elastic IP; the IGW translates between that and the instance's private address. - A private subnet has no route to the IGW. Nothing on the internet can reach its instances, and they can't reach the internet either, unless you add another path out.
"Public" is a property of the route table, not a checkbox on the subnet. A subnet with an IGW route but instances without public addresses still can't be reached.
The post room
Houses on the quiet streets still need to send letters out sometimes, to order spare parts from a supplier, say. So you put a post room, called a NAT gateway, on a front street, and point the quiet streets' signposts at it: "anywhere else: go to the post room".
The post room sends each letter out with its own return address, and when the reply comes back it knows which house asked and passes it on. A stranger can't use the post room to reach a quiet house: it only passes on replies to letters a house sent first.
A post room sits on one plot. If that plot loses power, the post room shuts too, so busy estates build one on every plot.
NAT gateway
A NAT gateway gives instances in private subnets outbound-only internet access. The classic public NAT
gateway lives in a public subnet and has an Elastic IP. The private subnet's route table sends 0.0.0.0/0 to
it; the NAT gateway rewrites the source address to its own and forwards the traffic through the IGW. Replies
are matched back to the connection that started them. Connections started from the internet can't come in.
Trade-offs to know:
- Zonal by default. A zonal NAT gateway lives in one AZ. If that AZ fails, private subnets routed through it lose internet access, so the usual design is one NAT gateway per AZ, each subnet routed to the one in its own AZ. Since late 2025 AWS also offers a regional availability mode that spans the AZs where you have workloads.
- Cost. NAT gateways are billed per hour and per gigabyte processed. Heavy traffic to AWS services such as S3 or DynamoDB is cheaper through VPC endpoints, which keep it off the NAT gateway entirely.
- IPv6 doesn't need NAT; an egress-only internet gateway gives the same outbound-only behaviour.
A doorman at every door
Even inside the estate, not everyone should knock on every door. So each house has a doorman with a guest list, called a security group. The list mostly says who may come in: "the lodge (the front desk that greets visitors) may knock on the front door". It can say who may go out too, but by default anyone in the house may go out. There are no "keep out" lines. Anyone not on the list is quietly turned away.
The doorman has a good memory. When someone from the house goes out to visit a neighbour, the doorman remembers, and lets the answer back in without checking the list. Doormen can also share one guest list across many houses, and a list can name a whole team: "anyone guarded by the lodge's doormen may come in", so you don't need to list every visitor by number.
Security groups
A security group is a virtual firewall attached to an ENI, so it applies to the instances, load balancers, databases and Lambda functions behind that interface. An interface can have several security groups; their rules are combined.
- Allow rules only. There is no deny rule. A new security group has no inbound rules (everything inbound is dropped) and, unless you remove it, one outbound rule allowing all traffic.
- Stateful. If a connection is allowed in one direction, its return traffic is allowed automatically, whatever the rules say about the other direction.
- Rules can reference other security groups. "Allow TCP 5432 from
sg-app" admits any interface wearingsg-app, however many instances autoscaling adds. That's far more robust than listing IP addresses. - All rules are evaluated together, in no order: if any rule allows the traffic, it's allowed. Dropped traffic gets no reply, so a blocked port looks like a timeout, not a refusal.
The barrier at the end of the street
Each street also has a barrier keeper at its entrance. At first the keeper lets everyone through, but you can give them a stricter numbered rule card, called a network ACL. The keeper reads the card from the lowest number up and stops at the first line that fits. Unlike the doorman, the card can say "keep out" as well as "let in".
The keeper has no memory at all. Someone walking out is checked against the card just like someone walking in, even if they're carrying the reply to a visitor who came in a minute ago. Forget to write a line for the way out, and the replies get stuck at the barrier.
Network ACLs
A network ACL filters traffic at the subnet boundary. Each subnet is associated with exactly one network ACL; one ACL can serve many subnets.
| Security group | Network ACL | |
|---|---|---|
| Applies to | An ENI (instance level) | A subnet |
| Rules | Allow only | Allow and deny |
| State | Stateful: replies allowed automatically | Stateless: replies must be allowed explicitly |
| Evaluation | All rules, any match allows | In rule-number order, first match wins |
| Default | No inbound, all outbound | Default ACL allows all; a custom ACL denies all |
Each ACL ends with a * rule that denies anything no numbered rule matched. Number rules in steps of 10 or 100
so you can insert new ones later. Because ACLs are stateless, a web subnet that allows inbound TCP 443 must
also allow outbound traffic to the client's ephemeral ports (the high port the client picked, typically
somewhere in 1024โ65535), or responses are dropped.
In practice, security groups do most of the work. Network ACLs are a coarse second layer, useful for denying a known bad address range, which a security group can't do.
How a typical estate is laid out
Most estates end up looking alike. On the front streets, one on each plot, sits the lodge, called a load balancer: it greets every visitor from the city and walks them to a workshop house that isn't busy. The workshops sit on quiet streets, and the strongroom with all the records sits on even quieter streets behind them. A spare strongroom on another plot keeps a copy of every record, ready to take over.
Each doorman only lets in the one before it: the workshop's doorman only lets in the lodge, and the strongroom's doorman only lets in the workshops. A visitor from the city can't walk straight to the strongroom, because no road leads there. If a whole plot loses power, the lodge sends visitors to the workshops on the other plot.
You don't have to build all this by hand. You can write the estate's plan down once, and a builder lays out every street, signpost and guest list from it.
A typical three-tier layout
The standard layout uses two or three AZs, with three subnet tiers in each:
- Public subnets: the Application Load Balancer (and NAT gateways). Only the ALB's security group allows
443from0.0.0.0/0. - Private subnets with egress: the application instances or containers, routed to a NAT gateway for outbound calls. Their security group allows the app port only from the ALB's security group.
- Isolated private subnets: the database (for example RDS Multi-AZ, with a standby in another AZ). No route
out at all; its security group allows
5432only from the app's security group.
You'd normally describe this as infrastructure as code. With the AWS CDK in Java:
1Vpc vpc = Vpc.Builder.create(this, "AppVpc")2.ipAddresses(IpAddresses.cidr("10.0.0.0/16"))3.maxAzs(2)4.natGateways(2) // one per AZ; 1 is cheaper but zonal5.subnetConfiguration(List.of(6SubnetConfiguration.builder().name("public")7.subnetType(SubnetType.PUBLIC).cidrMask(24).build(),8SubnetConfiguration.builder().name("app")9.subnetType(SubnetType.PRIVATE_WITH_EGRESS).cidrMask(24).build(),10SubnetConfiguration.builder().name("db")11.subnetType(SubnetType.PRIVATE_ISOLATED).cidrMask(24).build()))12.build();1314SecurityGroup appSg = SecurityGroup.Builder.create(this, "AppSg").vpc(vpc).build();15SecurityGroup dbSg = SecurityGroup.Builder.create(this, "DbSg").vpc(vpc).build();16dbSg.addIngressRule(appSg, Port.tcp(5432), "app to Postgres");
The CDK creates the subnets in each AZ, the route tables, the internet gateway and the NAT gateways. Your Spring
Boot app then connects to the database's endpoint in spring.datasource.url, a DNS name that resolves to its private IP
inside the VPC; it never needs a public address.
Debugging "it times out": work outwards. Does the instance's security group allow the port from the caller? Does the subnet's network ACL allow it in and allow the reply out? Does the route table have a route to the destination (IGW, NAT gateway, peering or endpoint)? Does a public instance actually have a public IP? VPC Flow Logs and Reachability Analyzer show where packets are accepted or rejected.
Plan the range up front. You can add secondary CIDR blocks to a VPC later, but you can't shrink the primary one, and a VPC that overlaps your office network or another VPC can't be connected to it cleanly.