DEV Community

Cover image for AWS VPC Deep Dive: Subnets, NAT Gateways & Security Groups
Shuvo Halder
Shuvo Halder

Posted on

AWS VPC Deep Dive: Subnets, NAT Gateways & Security Groups

Every resource you run on AWS — an EC2 instance, a database, a load balancer — lives inside a Virtual Private Cloud. The VPC is your isolated network: your IP ranges, your subnets, your firewalls, your route to the internet. Get the design right and everything else is easy. Get it wrong and you pay for it twice: once in outages, once in the bill.

Most VPC problems are not AWS problems. They are design problems: a CIDR block too small to grow, a database in a public subnet, a NAT gateway bill nobody predicted, a security group that allows the whole internet. This post covers how production teams actually design a VPC — subnets, route tables, NAT gateways, security groups and NACLs — and the pitfalls that only show up at scale.

What a VPC Actually Is

A VPC is a logically isolated section of the AWS cloud where you launch resources in a virtual network you define. You choose the IP address range (CIDR block), divide it into subnets, and control traffic with route tables and firewalls.

Three ideas carry the whole design:

  • Regions and Availability Zones. A VPC spans a region, and its subnets live in Availability Zones — physically separate data centres inside the region. A design that uses one AZ is a design with a single point of failure.
  • Subnets are the unit of placement. A resource does not live "in the VPC"; it lives in a subnet, in one AZ. The subnet decides whether the resource can reach the internet, and what can reach it.
  • Everything is deny by default. Security groups and NACLs start closed. Traffic flows only where you explicitly allow it — which is why a VPC that "just works" usually means someone opened it too wide.

CIDR Planning: The Decision You Cannot Undo

The first decision is the CIDR block, and it is the hardest to change later. A VPC's primary CIDR range cannot be resized — you can add secondary ranges, but the original range is fixed, and every subnet is carved from it.

VPC: 10.0.0.0/16          — 65,536 addresses
  Public subnet A:  10.0.0.0/24    — 256 addresses, AZ-a
  Public subnet B:  10.0.1.0/24    — 256 addresses, AZ-b
  Private subnet A: 10.0.10.0/24   — 256 addresses, AZ-a
  Private subnet B: 10.0.11.0/24   — 256 addresses, AZ-b
  Database subnet A: 10.0.20.0/24  — 256 addresses, AZ-a
  Database subnet B: 10.0.21.0/24  — 256 addresses, AZ-b
Enter fullscreen mode Exit fullscreen mode

The rules that keep this working:

  • Use a /16 for production. It leaves room for growth — new subnets, new services, new AZs — without re-addressing anything. A /20 or /22 feels generous on day one and runs out exactly when migration is most painful.
  • Never use a range that overlaps your office, your VPN, or another VPC you will peer with. Overlapping CIDRs make VPC peering and VPN routing impossible, and the fix is a new VPC.
  • Reserve space between subnets. Carving subnets back-to-back with no gaps means the next subnet you need does not fit. Leave gaps in the third octet.
  • Remember the AWS reserve. AWS reserves 5 addresses in every subnet (network, VPC router, DNS, future use, broadcast) — a /24 gives you 251 usable addresses, not 256.

Public vs Private Subnets: One Route Decides

A subnet is public or private based on one thing only: its route table.

  • A public subnet has a route to an Internet Gateway (0.0.0.0/0 → igw-xxxx). Resources in it can reach the internet, and the internet can reach them — if they have a public IP.
  • A private subnet has no route to the Internet Gateway. Resources in it cannot be reached from the internet at all, and can only reach out through a NAT gateway.

That is the whole distinction. A database in a "private" subnet that has an IGW route is not private — it is a public database waiting to be found. Check the route table, not the subnet name.

The standard production layout:

Subnet Route Lives here
Public 0.0.0.0/0 → Internet Gateway Load balancers, NAT gateways, bastion hosts
Private (app) 0.0.0.0/0 → NAT Gateway Application servers, containers, workers
Private (data) No internet route at all Databases, caches, internal services

Each subnet type exists in at least two Availability Zones. A load balancer in one AZ, an app in one AZ, a database in one AZ is a single-AZ design wearing a multi-tier costume — one AZ outage takes it all down.

Route Tables: The Traffic Cop

A route table is a set of rules that tells a subnet where to send traffic. The most specific route wins — a route to 10.1.0.0/16 beats the default 0.0.0.0/0 route — and traffic with no matching route is dropped.

Public route table:
  10.0.0.0/16   → local            (traffic inside the VPC)
  0.0.0.0/0     → igw-xxxx         (everything else → internet)

Private route table (AZ-a):
  10.0.0.0/16   → local
  0.0.0.0/0     → nat-xxxx (in AZ-a)

Database route table:
  10.0.0.0/16   → local            (no internet route at all)
Enter fullscreen mode Exit fullscreen mode

Three habits that prevent the classic mistakes:

  • One NAT gateway per AZ, and each private subnet routes to the NAT gateway in its own AZ. Routing AZ-a's private subnet through AZ-b's NAT gateway works — until AZ-b goes down, and then AZ-a loses its internet too. Cross-AZ NAT traffic also costs money on every byte.
  • Never share a route table between public and private subnets. One table, one purpose. A shared table is how a private subnet silently gains an IGW route.
  • The local route is automatic and cannot be deleted. Traffic inside the VPC always routes locally. You cannot break VPC-internal traffic with a route table — but you can break it with a security group, which is the next section.

NAT Gateway vs NAT Instance: The Cost Decision

Private subnets need a way out for outbound traffic — software updates, API calls, package downloads — without letting the internet in. That is what NAT does: it translates a private IP to a public one for outbound connections only.

NAT Gateway NAT instance
What it is Managed AWS service An EC2 instance you run yourself
Availability Highly available within its AZ Single instance — you manage failover
Bandwidth Up to 100 Gbps, scales automatically Limited by instance size
Maintenance None — AWS patches it You patch, monitor, and replace it
Cost Hourly charge + per-GB data processing Instance cost + data transfer only

The NAT gateway is the right choice for almost every production VPC: no patching, no single instance to babysit, bandwidth that scales. But its pricing surprises people, and the surprise is always the same:

  • Hourly charge per gateway, per AZ. Two AZs means two gateways, two hourly charges, 24 hours a day, whether traffic flows or not.
  • Per-GB data processing on top of normal data transfer. Every gigabyte that passes through the NAT gateway is charged — once for processing, once for transfer. A chatty application that downloads large files through NAT pays twice.
  • Cross-AZ NAT traffic costs extra. A private subnet in AZ-a using a NAT gateway in AZ-b pays inter-AZ transfer on every byte.

The cost-saving moves, in order of impact: put a NAT gateway in each AZ (and route locally), use VPC endpoints for AWS services so that traffic never touches NAT at all, and cache aggressively so the same data is not downloaded a hundred times. A NAT instance is cheaper on paper, but the moment it becomes a bottleneck or fails at 2 AM, the saving evaporates.

Security Groups vs NACLs: Two Firewalls, Different Jobs

This is the distinction interviewers love, because it tests whether you understand state:

Security groups are stateful. They operate at the instance (ENI) level, and if you allow an inbound request, the response is automatically allowed back out — you never write a return rule. Security groups are also the only firewall that can reference another security group: "allow traffic from the app security group" instead of "allow traffic from 10.0.10.0/24". That reference survives IP changes, which is why it is the production pattern.

Load balancer security group:
  Inbound:  0.0.0.0/0 on 443 (HTTPS from the internet)
  Outbound: to app security group on 8080

App security group:
  Inbound:  from load balancer security group on 8080
  Outbound: to database security group on 5432

Database security group:
  Inbound:  from app security group on 5432
  Outbound: none needed (return traffic is automatic)
Enter fullscreen mode Exit fullscreen mode

Network ACLs are stateless. They operate at the subnet level, and every direction needs its own rule — allow inbound on port 443, and you must also allow the outbound response on the ephemeral port range (1024–65535). NACLs evaluate rules in numbered order, lowest first, and the first match wins. They are a coarse second layer: block a bad IP range at the subnet, or deny a whole protocol, while security groups do the fine-grained work.

Security group Network ACL
Level Instance / ENI Subnet
Stateful Yes — return traffic automatic No — both directions need rules
Rule evaluation All rules evaluated, allow only Numbered order, first match wins
Referencing Can reference another security group IP ranges only
Default Deny all inbound, allow all outbound Allow all (default NACL) — change it

In practice: security groups do the real access control, NACLs are the backup. A team that relies on NACLs for its security model is fighting the tool — and a team with no NACL rules at all is fine, because the security groups are doing the work.

VPC Endpoints: Skip the Internet Entirely

A private subnet that needs to reach S3, DynamoDB, or the Systems Manager API has two options: go out through the NAT gateway (and pay for it), or use a VPC endpoint — a private connection inside AWS that never touches the internet.

  • Gateway endpoints (S3, DynamoDB) are free and work through a route table entry. Traffic to S3 from a private subnet goes over the AWS backbone, not through NAT. There is no reason not to use them.
  • Interface endpoints (most other AWS services) place an ENI in your subnet with a private IP, and cost an hourly charge plus per-GB processing. Use them for services your private resources call often — ECR, Secrets Manager, CloudWatch Logs — and skip them for services you call rarely.

The rule is simple: if a private resource talks to an AWS service, that traffic should go through an endpoint, not through NAT. It is cheaper, faster, and it keeps your traffic off the public internet entirely.

VPC Flow Logs: See What Actually Happened

Flow Logs record the IP traffic flowing to and from your VPC — source, destination, port, protocol, bytes, and whether the traffic was accepted or rejected. They are the first thing you check when something cannot connect, and the last thing teams enable — usually after the incident that needed them.

# A rejected connection tells the whole story
2 123456789010 eni-0abc123 10.0.10.5 10.0.20.9 52341 5432 6 1 52 1712345678 1712345738 REJECT OK
Enter fullscreen mode Exit fullscreen mode

That line says: an app server (10.0.10.5) tried to reach the database (10.0.20.9) on port 5432, and the traffic was rejected. The cause is almost always a security group — the database group does not allow the app group — and the flow log proves it in one line instead of an hour of guessing.

Enable flow logs at the VPC level, send them to CloudWatch Logs or S3, and keep them long enough to investigate: 30 days is a reasonable minimum. They cost little, and the one time you need them, they are worth a year of the bill.

The Mistakes That Cost Money and Cause Outages

  • A CIDR block too small, or overlapping with the office VPN — unfixable without a new VPC.
  • A database in a public subnet, or a "private" subnet with an IGW route.
  • One NAT gateway for the whole VPC — one AZ outage, no outbound traffic anywhere.
  • Private subnets routing through a NAT gateway in another AZ — paying inter-AZ transfer on every byte, and failing when that AZ fails.
  • Security groups allowing 0.0.0.0/0 to SSH (22) or RDP (3389) — the first thing every scanner on the internet finds.
  • No VPC endpoints — every call to S3 paying NAT processing charges.
  • No flow logs — debugging a connectivity failure by guessing.
  • A single-AZ design for a production workload — one AZ outage is a full outage.

The Production Checklist

  • VPC /16, no overlap with office, VPN, or peered VPCs; gaps left between subnets.
  • Public, private-app, and private-data subnets in at least two AZs.
  • Route tables: one per purpose, private subnets route to a NAT gateway in their own AZ, data subnets have no internet route.
  • One NAT gateway per AZ; VPC endpoints for S3, DynamoDB, and frequently used services.
  • Security groups referencing other security groups, least privilege, no 0.0.0.0/0 on 22 or 3389.
  • NACLs as a coarse second layer, not the primary control.
  • Flow logs enabled at the VPC level, retained 30+ days.
  • Every change in infrastructure as code, reviewed before it applies.

What Hiring Managers Actually Ask

If you are targeting remote DevOps, SRE, or cloud roles, VPC design is interview material because it separates people who have built a network from people who have only launched an instance:

  • "A database should not be reachable from the internet, but the app needs it — how do you design that?" (Answer: database in a private subnet with no internet route, security group allowing only the app security group on the database port, and flow logs to prove nothing else reaches it.)
  • "Your NAT gateway bill doubled this month. Where do you look?" (Answer: which AZs the traffic crosses, whether S3 and DynamoDB traffic is going through NAT instead of gateway endpoints, and which application is downloading the most through NAT.)
  • "An EC2 instance cannot reach the internet. Walk me through it." (Answer: check the subnet's route table for a NAT route, check the NAT gateway is in the same AZ and healthy, check the security group outbound rules, then check flow logs for REJECT entries.)
  • "Security group or NACL — which one do you use, and why?" (Answer: security groups for access control, because they are stateful and can reference other security groups; NACLs only as a coarse subnet-level backup, because they are stateless and every return path needs its own rule.)

The answer they want is never "I can create a VPC in the console." It is the system around it: CIDR planning, subnet separation, route tables, least-privilege groups, and the cost of every byte that crosses a boundary.

Key Takeaway

A VPC is a security boundary, not a network diagram. Plan the CIDR once, separate public from private with route tables, keep databases off the internet entirely, and make every byte justify the path it takes. The cheapest traffic is the traffic that never leaves AWS.


Your turn: How is your VPC laid out — how many AZs, and where do your databases live? And what surprised you most about your first NAT gateway bill? Tell me in the comments.

Top comments (0)