TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

All concepts ยท aws ยท networking ยท vpc ยท security ยท cloud

Amazon VPC: subnets, route tables and security groups

A VPC is your own isolated network in an AWS Region. You split its IP range into subnets in each Availability Zone, route tables decide which subnets reach the internet, and security groups and network ACLs filter the traffic.

15 min6 animations

๐Ÿ“š Builds on๐Ÿฅ– AWS Regions and Availability Zones

๐Ÿ–จ๏ธ Cheat sheet

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.

TermMeaning
VPCYour isolated network in one Region, with one or more CIDR blocks
SubnetA 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
ENIAn 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.

A VPC and its subnets4 steps
๐Ÿง‘โ€๐Ÿ’ผYou๐Ÿ˜๏ธYour estate
Press play and watch it happen!

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.

Public and private subnets5 steps
๐Ÿ›ฃ๏ธ Front street๐ŸŒณ Quiet streetFront signpost012Quiet signpost012๐ŸงVisitor๐ŸšชMain gate๐Ÿ Front house๐ŸกQuiet house
Press play and watch it happen!

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 NAT gateway for outbound-only access5 steps
๐ŸŒณ Quiet street, plot A๐Ÿ›ฃ๏ธ Front street, plot A๐ŸกQuiet house๐Ÿ“ฎPost room๐ŸšชMain gate๐ŸญSupplier๐Ÿ•ต๏ธStranger
Press play and watch it happen!

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 wearing sg-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.
Security groups4 steps
๐Ÿšช Workshop front door๐Ÿ›Ž๏ธLodge๐ŸงNeighbour๐Ÿ’‚Doorman๐Ÿ Workshop๐Ÿ”Strongroom
Press play and watch it happen!

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 groupNetwork ACL
Applies toAn ENI (instance level)A subnet
RulesAllow onlyAllow and deny
StateStateful: replies allowed automaticallyStateless: replies must be allowed explicitly
EvaluationAll rules, any match allowsIn rule-number order, first match wins
DefaultNo inbound, all outboundDefault 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.

Network ACLs5 steps
๐Ÿ›ฃ๏ธ Front streetBarrier card: way in012Barrier card: way out012๐ŸงVisitor๐Ÿ•ต๏ธBanned visitor๐Ÿ Front house
Press play and watch it happen!

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 443 from 0.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 5432 only 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(6                SubnetConfiguration.builder().name("public")7                        .subnetType(SubnetType.PUBLIC).cidrMask(24).build(),8                SubnetConfiguration.builder().name("app")9                        .subnetType(SubnetType.PRIVATE_WITH_EGRESS).cidrMask(24).build(),10                SubnetConfiguration.builder().name("db")11                        .subnetType(SubnetType.PRIVATE_ISOLATED).cidrMask(24).build()))12        .build();13 14SecurityGroup 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.

๐Ÿ”ง Going deeper

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.

A typical layout4 steps
๐Ÿ›ฃ๏ธ Front streets๐ŸŒณ Quiet streets๐ŸŒฒ Quietest streets๐Ÿ‘ฅVisitors๐Ÿ›Ž๏ธLodge๐Ÿ Workshop A๐Ÿ Workshop B๐Ÿ”Strongroom A๐Ÿ”Spare strongroom B
Press play and watch it happen!

๐Ÿงฉ Quick quiz

Quick quiz! 5 questions about Amazon VPC: subnets, route tables and security groups, told the same way as the story.
  1. A house sits on a street whose signpost has no line pointing to the main gate. What can visitors from the city do?
  2. The quiet houses need to order parts from a supplier in the city, but strangers must never reach them. What do you add?
  3. The front house's doorman lets a visitor in. On the way out, does the answer need its own line on the doorman's list?
  4. You must keep one troublesome address out of a whole street, even though the doormen let everyone in for the front house. Who can do that?
  5. You build only one post room, on plot A. Plot A loses power. What happens to quiet houses on plot B?
Answer every question to check.Passing marks Amazon VPC: subnets, route tables and security groups as done.
Got the hang of Amazon VPC: subnets, route tables and security groups? Mark it as done, and your paths show how far youโ€™ve got.

Saved in this browser.

โ˜๏ธ AWS from zeroStep 3 of 12

โ† Previous on the path๐Ÿชช AWS IAM: users, roles and policiesNext on the path โ†’๐Ÿšš Amazon EC2 and Auto Scaling

๐Ÿ‘‰ Read next

  • ๐ŸššAmazon EC2 and Auto ScalingHiring food trucks for a festival, with a manager who keeps the right number serving and replaces any that break.
  • ๐ŸชAmazon ElastiCacheHiring an agency to run your shop's express counter, staff and standby clerk included, while you choose what it stocks.
  • ๐Ÿ›ฌElastic Load Balancing: ALB and NLBAn airport arrivals hall: a guide reads tickets and walks people to gates, or a turnstile waves them through.
  • ๐ŸชงAmazon Route 53The city information kiosk that tells visitors which branch of your shop to go to, following your rules.