Follow a packet through public and private VPC subnets
Build or diagram a VPC with public and private subnets and trace traffic through routes, security groups, NACLs and NAT.
By Invicly Tech, , in AWS Cloud Engineering. About 14 minutes to read.
In short
A VPC (virtual private cloud) is your own private network inside an AWS Region, split into subnets that each live in one Availability Zone. Whether a packet gets through depends on four things in order: the route table of its subnet, the security group on the network interface, the network ACL on the subnet, and a gateway such as an internet gateway or NAT gateway. Learning to trace those four checks is how you design a safe layout and fix most connection failures.
Think of it like this: A VPC is an office building: subnets are floors, route tables are the signs to the exits, NACLs are guards at each floor's door, and security groups are the locks on each room.
Explained simply
When you create resources on AWS, they need a network to live in. That network is a VPC, short for virtual private cloud. It belongs to your account, sits in one Region, and starts completely closed. Nothing gets in or out until you add the pieces that allow it.
You cut a VPC into smaller ranges called subnets. Each subnet lives in exactly one Availability Zone, which is one group of data centres. For our file-processing service we use two public subnets for the load balancer and the NAT gateway, and two private subnets for the workers and the database, one of each in two zones.
A subnet is not public because of its name. It is public because its route table sends internet traffic to an internet gateway. A private subnet sends internet traffic to a NAT gateway instead. NAT lets the worker download packages or call an outside API, while nobody on the internet can start a connection to the worker.
Two firewalls check traffic. A security group sits on each network interface and only has allow rules. It remembers connections, so replies come back without extra rules. A network ACL sits on the whole subnet, has numbered allow and deny rules, and does not remember anything, so you must allow the replies too.
NAT is handy but not free. You pay for every hour each NAT gateway exists and for every gigabyte it processes. In a lab, delete it as soon as you finish. In production, send S3 and DynamoDB traffic through free gateway endpoints so it never touches NAT.
Press Play on a diagram to walk through it one step at a time.
- 1Worker in private subnet
10.0.11.25 opens an HTTPS connection
- 2Security group outbound
Allows TCP 443 to 0.0.0.0/0
- 3Private route table
0.0.0.0/0 points to the NAT gateway
- 4NAT gateway in public subnet
Swaps the source for its own address
- 5Internet gateway
Maps to the Elastic IP and sends it out
- 6Reply returns
NAT tracks the connection and hands it back
- Attached to a network interface
- Stateful: replies are allowed automatically
- Allow rules only
- Can reference other security groups
- Attached to a subnet
- Stateless: return traffic needs its own rule
- Allow and deny rules, checked by number
- Default NACL allows everything
- Public subnets
- 10.0.1.0/24 in zone a
- 10.0.2.0/24 in zone b
- ALB and NAT gateway
- Private app subnets
- 10.0.11.0/24 in zone a
- 10.0.12.0/24 in zone b
- Workers and containers
- Private data subnets
- 10.0.21.0/24 in zone a
- 10.0.22.0/24 in zone b
- Database, no internet route
- Gateways
- Internet gateway
- S3 gateway endpoint
In depth
Planning CIDR blocks you will not regret
Pick the VPC range first, because it is awkward to change later. A /16 such as 10.0.0.0/16 gives 65,536 addresses and plenty of room. VPC blocks can be between /16 and /28. Choose a range that does not overlap your office network, home VPN or any other VPC you might connect, because overlapping ranges cannot be routed to each other.
Then carve subnets. A simple, readable plan uses /24 subnets and puts the zone and tier in the third number: 10.0.1.0/24 and 10.0.2.0/24 for public, 10.0.11.0/24 and 10.0.12.0/24 for private apps, 10.0.21.0/24 and 10.0.22.0/24 for data. AWS keeps five addresses in each subnet: the network address, the VPC router, the DNS server, one reserved for future use and the broadcast address. So a /24 has 251 usable addresses, and a /28 has only 11.
- VPC size: /16 (largest) to /28 (smallest)
- Five reserved addresses per subnet
- Leave unused ranges for later tiers or zones
- Avoid 172.17.0.0/16 if you run Docker locally; it is Docker's default bridge
How route tables decide the next hop
Every subnet is associated with exactly one route table. If you do not choose one, it uses the VPC's main route table. Each route has a destination range and a target. AWS always picks the most specific match, the longest prefix. Every table has a local route for the VPC range, so all subnets can reach each other by default.
A public route table adds 0.0.0.0/0 to the internet gateway. A private one adds 0.0.0.0/0 to a NAT gateway in the same zone. When a gateway endpoint for S3 is attached, the table gets a route to the S3 prefix list, which is more specific than 0.0.0.0/0, so S3 traffic takes the endpoint and skips NAT. Reading a route table from top to bottom is not how it works; specificity wins.
- 110.0.0.0/16 to local
Traffic inside the VPC stays inside
- 2S3 prefix list to vpce-...
More specific than the default, so S3 skips NAT
- 30.0.0.0/0 to nat-...
Everything else leaves through NAT in this zone
Security groups first, NACLs as a backstop
Security groups do most of the work. Because they can reference other security groups, you can write rules by role instead of by IP: the database group allows 5432 only from the worker group, and the worker group allows 8080 only from the load balancer group. When instances scale in and out, the rules still hold.
Network ACLs are evaluated in rule-number order, and the first match wins. The default NACL allows all traffic both ways, which is fine for most designs. If you add a custom NACL, remember it is stateless. An inbound allow for 443 is not enough; you also need an outbound allow for the ephemeral ports the client used, typically 1024 to 65535. Use NACLs for broad rules, such as blocking a known bad range for a whole subnet.
- SG rule example: allow TCP 5432 from sg-workers
- NACL rules: lowest number checked first
- New custom NACLs deny everything until you add rules
Egress choices and what NAT really costs
A public NAT gateway lives in a public subnet with an Elastic IP. Pricing is per hour plus per GB processed; the AWS pricing examples show $0.045 per hour and $0.045 per GB in US East (Ohio), and public IPv4 addresses also cost $0.005 per hour. Always check the pricing page for your Region.
Do the sum for one month: two NAT gateways for high availability run about 730 hours each, roughly $66 before any data. If the workers pull 500 GB a month from S3 through NAT, that adds about $22 for traffic that a free gateway endpoint would carry. For labs, one NAT gateway, or none at all with endpoints for every AWS service you call, keeps the bill small. AWS also offers regional NAT gateways that span zones automatically; read the VPC guide before choosing one.
- One NAT per AZ: resilient, but costs per gateway
- Gateway endpoints for S3 and DynamoDB: free
- IPv6 outbound only: egress-only internet gateway, no NAT charge
Learn it in 5 short steps
About 40m each. Track them with a timer in your plan.
- 1Plan CIDR blocks and subnets~40m
- 2Read route tables~40m
- 3Compare security groups and NACLs~40m
- 4Route egress through NAT~40m
- 5Estimate NAT gateway cost~40m
Key points
- A subnet is public only because its route table sends 0.0.0.0/0 to an internet gateway.
- Private subnets reach out through a NAT gateway; nothing on the internet can start a connection to them.
- Security groups are your main firewall; NACLs are a coarse, stateless backstop.
- AWS reserves five addresses in every subnet, so a /24 gives 251 usable addresses.
- NAT gateways charge per hour and per GB processed, so keep heavy AWS traffic on VPC endpoints.
Key terms
Tap a card to flip it. 0/6 known
Examples
# Which route table does a subnet use, and where does 0.0.0.0/0 go?
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0abc1234example \
--query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,NatGatewayId]" \
--output table
# local route = inside the VPC, igw-... = public, nat-... = private with egressIf the subnet has no explicit association, it uses the VPC's main route table and the filter returns nothing.
Build a two-tier VPC with the CLI and clean it up
Prerequisites: a sandbox account, a budget alert and an SSO profile. Step 1: create the VPC and turn on DNS hostnames. Step 2: create one public and one private subnet. Step 3: create and attach an internet gateway, then a public route table with 0.0.0.0/0 to it. Evidence: describe-route-tables shows the igw route on the public subnet only. This lab skips NAT so it costs nothing. Cleanup: delete the subnets, detach and delete the internet gateway, delete the route table, then the VPC.
VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC --enable-dns-hostnames
PUB=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.1.0/24 \
--availability-zone eu-west-1a --query Subnet.SubnetId --output text)
PRIV=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.11.0/24 \
--availability-zone eu-west-1a --query Subnet.SubnetId --output text)
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --vpc-id $VPC --internet-gateway-id $IGW
RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 associate-route-table --route-table-id $RT --subnet-id $PUBA worker cannot reach the internet: trace it hop by hop
A worker in 10.0.11.0/24 times out calling an outside API. Step 1: check the subnet's route table. It shows 0.0.0.0/0 to a NAT gateway, good. Step 2: check the NAT gateway. It is in the private subnet, not a public one, so it has no path to the internet gateway. That is the fault. Step 3: create a new NAT gateway in 10.0.1.0/24, point the private route at it and delete the wrong one. Evidence: curl from the worker now returns a response, and the NAT gateway's BytesOutToDestination metric rises in CloudWatch. Also confirm the security group allows outbound 443.
aws ec2 describe-nat-gateways \
--query "NatGateways[].[NatGatewayId,SubnetId,State]" --output table
# Is that SubnetId one whose route table points 0.0.0.0/0 to an igw?
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0pub1example \
--query "RouteTables[].Routes[].GatewayId"Quick reference
- Public subnet = route 0.0.0.0/0 to an internet gateway; private = no such route
- AWS reserves 5 IPs per subnet: a /24 has 251 usable, a /28 has 11
- Longest prefix wins in a route table, not the order of the rows
- Security group: stateful, allow only, per interface; NACL: stateless, allow and deny, per subnet
- Custom NACL needs outbound ephemeral ports 1024-65535 for replies
- NAT gateway lives in a public subnet and bills per hour plus per GB processed
- Use free S3 and DynamoDB gateway endpoints to keep heavy traffic off NAT
- Delete NAT gateways and release Elastic IPs at the end of every lab
Common mistakes
- Adding an inbound NACL rule but forgetting the outbound rule for ephemeral ports 1024-65535, so replies are dropped.
- Running one NAT gateway per AZ in a lab and forgetting it keeps billing every hour after the lab ends.
- Choosing a VPC CIDR that overlaps the office network or another VPC you will later need to connect.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Plan a VPC for two zones with a public, private app and private data subnet in each. Give every CIDR.
- Exercise 2medium
Write the security group rules for an ALB, a worker fleet on port 8080 and a PostgreSQL database, using group references.
- Exercise 3hard
Estimate the monthly egress cost for two NAT gateways processing 300 GB in total, then redesign so S3 and SQS traffic (250 GB) avoids NAT. Use $0.045 per hour and per GB as sample prices.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I need a NAT gateway for every private subnet?
- No. One NAT gateway can serve many subnets. For resilience, production designs usually put one in each zone so a zone failure does not cut egress for the others.
- Can I use the default VPC for labs?
- Yes for quick tests, but its subnets are all public. Build your own VPC to practise the private subnet patterns real workloads use.
- Is a security group enough without a NACL?
- For most workloads, yes. Leave the default NACL in place and do fine-grained control with security groups. Add NACL rules only for broad subnet-wide blocks.
- Why can't I SSH to an instance in a private subnet?
- It has no public address and no inbound internet route, by design. Use Systems Manager Session Manager instead, which needs no open inbound port.