DEV Community

BytePatterns
BytePatterns

Posted on Originally published at bytepatterns.com

AWS VPC: Subnets, Route Tables, NAT Gateways & Firewalls Explained

If you are preparing for AWS SAA-C03 or CLF-C02, VPC networking questions are almost guaranteed to test one core distinction: routes decide where a packet can go; firewalls decide whether it may.

Here is the clean mental model for how subnets, gateways, Security Groups, and Network ACLs fit together.


1. Public vs. Private Subnets: It's the Route Table, Not the Name

A subnet is public if and only if its associated Route Table has a default route (0.0.0.0/0) pointing to an Internet Gateway (igw-...).

# Public subnet route table
aws ec2 create-route --route-table-id rtb-public \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc

# Private subnet route table (outbound-only via NAT Gateway)
aws ec2 create-route --route-table-id rtb-private \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id nat-0abc
Enter fullscreen mode Exit fullscreen mode
  • Public subnet: Hosts load balancers, bastion hosts, and NAT Gateways. Instances need a public IPv4 (or Elastic IP) to reach or be reached from the internet.
  • Private subnet: Hosts application servers and databases. To pull OS patches or call external APIs without accepting inbound internet connections, traffic routes through a NAT Gateway sitting in a public subnet.

(Exam tip: A NAT Gateway itself must live in a **public* subnet so it can reach the Internet Gateway!)*


2. Security Groups vs. Network ACLs (NACLs)

Both filter traffic, but they operate at different layers and behave completely differently:

Feature Security Group (SG) Network ACL (NACL)
Attached to Elastic Network Interface (instance/ENI) Subnet
State Stateful (return traffic auto-allowed) Stateless (must allow inbound & outbound)
Rule types Allow only (implicit deny at end) Allow and Deny (evaluated in rule-number order)
Source referencing Can reference another SG ID! CIDR blocks only

Because Security Groups are stateful and can reference other Security Groups, you can chain tiers cleanly without hardcoding IPs:

# Allow app servers in sg-app to reach Postgres on sg-db
aws ec2 authorize-security-group-ingress --group-id sg-db \
  --protocol tcp --port 5432 --source-group sg-app
Enter fullscreen mode Exit fullscreen mode

When you need to explicitly block a known malicious IP range across an entire subnet, use a NACL DENY rule (Security Groups cannot write explicit deny rules).


3. Don't Forget Gateway Endpoints for S3 & DynamoDB

If private-subnet instances read/write S3 or DynamoDB through a NAT Gateway, you pay NAT Gateway data-processing fees for every GB. Adding a VPC Gateway Endpoint (free) updates your private route table to send S3/DynamoDB traffic directly over the AWS backbone—faster, private, and $0/GB.


Quick Exam Recap

  • What makes a subnet public? A route to an Internet Gateway (igw-...).
  • How do private instances reach the internet outbound-only? Through a NAT Gateway placed in a public subnet.
  • Need to block a specific IP CIDR? Use a Network ACL (explicit Deny), not a Security Group.
  • Need to eliminate NAT Gateway charges for S3/DynamoDB? Use a Gateway Endpoint.

Want the interactive diagrams and mini-quiz? Check out the full lesson on VPC: Subnets, NAT & Firewalls or test yourself with our free 65-question AWS SAA-C03 practice exam (no signup required).

Top comments (0)