Skip to content
Visual explainer 5h to learn 5 steps

VPC, subnets, route tables, IGW & NAT

Build a VPC with public and private subnets across two AZs.

By Invicly Tech, , in AWS Solutions Architect – Associate. About 12 minutes to read.

Topic 9 of 34

In short

A VPC is your private network in a Region, split into subnets that each live in one AZ. A subnet is public when its route table sends 0.0.0.0/0 to an internet gateway. Private subnets reach the internet outbound through a NAT gateway placed in a public subnet.

Think of it like this: The VPC is an office building, subnets are floors, and route tables are the signposts.

Explained simply

A VPC (Virtual Private Cloud) is your own private network inside AWS. You choose its address range, cut it into smaller pieces called subnets, and decide where traffic may go. Nothing can reach your servers from the internet unless you build a path for it.

Each subnet lives in one Availability Zone. A good layout puts one public subnet and one or more private subnets in each of at least two AZs. Public subnets hold things the internet must reach, such as a load balancer. Private subnets hold app servers and databases.

Route tables are the signposts. Every subnet follows one route table that says where each destination goes. "Public" simply means the route table sends internet traffic (0.0.0.0/0) to an internet gateway. Remove that route and the subnet is private.

Private servers still need to download updates. A NAT gateway sits in a public subnet and lets private servers start connections out to the internet, while blocking anyone outside from starting connections in. It works like a receptionist who makes calls for you but never puts strangers through.

Press Play on a diagram to walk through it one step at a time.

LayersTwo-AZ VPC layout
  1. 1
    Internet gateway

    Attached to the VPC; enables public internet traffic.

  2. 2
    VPC 10.0.0.0/16

    Regional private network spanning all AZs.

  3. 3
    Public subnets

    One per AZ; route 0.0.0.0/0 to the IGW. ALB, NAT.

  4. 4
    Private app subnets

    One per AZ; route 0.0.0.0/0 to that AZ's NAT gateway.

  5. 5
    Private data subnets

    Databases with no internet route at all.

FlowPrivate instance reaches the internet
  1. Step 1
    Private EC2

    Has only a private IP.

  2. Step 2
    Route table

    0.0.0.0/0 points to the NAT gateway.

  3. Step 3
    NAT gateway

    In a public subnet, with an Elastic IP.

  4. Step 4
    Internet gateway

    Sends traffic out; replies return through NAT.

SequenceHow the internet gateway translates a public IP
Internet client
Internet gateway
EC2 10.0.0.25
  1. 1Request to 203.0.113.10:443
    Internet client to Internet gateway: Request to 203.0.113.10:443
  2. 2Destination rewritten to 10.0.0.25
    Internet gateway to EC2 10.0.0.25: Destination rewritten to 10.0.0.25
  3. 3Reply from 10.0.0.25
    EC2 10.0.0.25 to Internet gateway: Reply from 10.0.0.25
  4. 4Source rewritten to 203.0.113.10
    Internet gateway to Internet client: Source rewritten to 203.0.113.10

In depth

Planning CIDR ranges

A CIDR block like 10.0.0.0/16 describes a range of addresses: the /16 means the first 16 bits are fixed, leaving 65,536 addresses. A VPC can be between /16 and /28. Use private ranges from RFC 1918: 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16.

The golden rule is no overlap. If two networks use the same range, you cannot peer them or route between them through Transit Gateway, and connecting to the office VPN breaks. Keep a simple register of which range each VPC and office uses, or use VPC IPAM to hand them out.

The primary CIDR cannot change, but you can add secondary CIDR blocks later. AWS reserves five addresses in every subnet, so a /24 gives 251 usable IPs. Size generously: Kubernetes on EKS gives every pod its own VPC IP and can exhaust small subnets quickly.

MapCarving up 10.0.0.0/16
VPC 10.0.0.0/16
  • Public tier
    • 10.0.0.0/24 in AZ a
    • 10.0.1.0/24 in AZ b
  • App tier
    • 10.0.10.0/23 in AZ a
    • 10.0.12.0/23 in AZ b
  • Data tier
    • 10.0.20.0/24 in AZ a
    • 10.0.21.0/24 in AZ b
  • Spare
    • Room for a third AZ
    • Room for new tiers

Route tables and longest prefix match

Every subnet is associated with exactly one route table. If you do not choose one, it uses the VPC's main route table, so keep the main table private and create custom tables for public subnets. Each table has a local route for the VPC's own CIDR, which you cannot delete; it lets all subnets in the VPC talk to each other.

When several routes match a destination, the most specific one wins. A route to 10.1.0.0/16 through a peering connection beats 0.0.0.0/0 through a NAT gateway for traffic to 10.1.5.9. That is called longest prefix match.

Route targets include an internet gateway (igw-), NAT gateway (nat-), Transit Gateway (tgw-), peering connection (pcx-), gateway endpoint (via a prefix list), or a network interface for appliances such as firewalls.

  • The five reserved IPs: network, router (+1), DNS (+2), future use (+3), last address
  • A subnet has one route table; a route table can serve many subnets
  • Most specific route wins
  • Keep the main route table private

Internet gateway and public addresses

An internet gateway is a highly available, horizontally scaled part of the VPC with no bandwidth limit to manage. A VPC can have only one. For IPv4 it performs one-to-one translation: the instance only knows its private IP, and the gateway maps it to the instance's public IPv4 address on the way out and back.

So an instance needs two things to be reachable: a route to the internet gateway, and a public IPv4 or Elastic IP. The subnet's auto-assign public IPv4 setting controls whether new instances get one. Since February 2024, every public IPv4 address costs about 0.005 USD per hour, so do not hand them out by default.

IPv6 addresses are globally unique and need no translation. For IPv6 outbound-only access, use an egress-only internet gateway, the IPv6 equivalent of NAT.

NAT gateway details

A NAT gateway is a managed service that lives in one AZ, in a public subnet, with an Elastic IP. Private subnets point 0.0.0.0/0 at it. It starts at 5 Gbps and scales automatically up to 100 Gbps, and supports up to 55,000 simultaneous connections to each unique destination. You pay per hour plus per GB processed, which can surprise teams pulling large container images or S3 data through it.

For resilience, run one NAT gateway per AZ and point each AZ's private route table at its own NAT. A single shared NAT is a single point of failure and adds cross-AZ data charges.

Two cost tips: S3 and DynamoDB traffic should use free gateway endpoints instead of NAT, and dev environments can share one NAT if downtime is acceptable. The older alternative, a NAT instance, is an EC2 instance you manage yourself; it needs the source/destination check disabled.

CompareNAT gateway vs NAT instance
NAT gateway
  • Managed, highly available in its AZ
  • Scales to 100 Gbps
  • No security groups on it
  • Hourly plus per-GB charge
NAT instance
  • EC2 you patch and scale
  • Limited by instance size
  • Uses security groups
  • Disable source/destination check

Testing connectivity

When a connection fails, check the path in order: route table, NACL, security group, then the operating system. VPC Reachability Analyzer automates this: give it a source and destination and it reports the first component that blocks the path, without sending real packets.

VPC Flow Logs record accepted and rejected connections for a VPC, subnet or network interface, and can go to CloudWatch Logs or S3. A REJECT entry tells you a security group or NACL dropped the traffic; no entry at all usually means a routing problem.

To reach private instances, skip bastion hosts. Systems Manager Session Manager opens a shell through the SSM agent, with IAM controlling access and no inbound ports. The instance needs an instance profile role with the AmazonSSMManagedInstanceCore policy and a path to the SSM service, through NAT or interface endpoints.

Learn it in 5 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Plan VPC and subnet CIDR ranges~1h
  2. 2
    Create public and private subnets in two AZs~1h
  3. 3
    Configure route tables and an internet gateway~1h
  4. 4
    Add NAT gateways for private egress~1h
  5. 5
    Launch instances and test connectivity~1h

Key points

  • A subnet lives in exactly one AZ; use one public and one private subnet per AZ.
  • AWS reserves five IP addresses in every subnet.
  • NAT gateways are AZ-scoped; deploy one per AZ for resilience.
  • NAT allows outbound-only connections; inbound connections from the internet are not possible.
  • VPC CIDRs cannot be changed later, so plan non-overlapping ranges up front.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 \
  --query Vpc.VpcId --output text)
aws ec2 create-subnet --vpc-id $VPC \
  --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
IGW=$(aws ec2 create-internet-gateway \
  --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --vpc-id $VPC \
  --internet-gateway-id $IGW

Create a VPC, a subnet and an attached internet gateway.

Wiring public and private routes

A VPC already has subnets in two AZs and an attached internet gateway. Step 1: create a public route table with 0.0.0.0/0 to the IGW and associate the public subnet. Step 2: allocate an Elastic IP and create a NAT gateway in that public subnet. Step 3: create a private route table with 0.0.0.0/0 to the NAT gateway and associate the private subnet. Repeat steps 2 and 3 for the second AZ so each AZ has its own NAT.

bash
PUB_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PUB_RT \
  --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_A

EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT=$(aws ec2 create-nat-gateway --subnet-id $PUB_A --allocation-id $EIP \
  --query NatGateway.NatGatewayId --output text)
aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT
PRIV_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PRIV_RT \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV_A

"My public instance can't reach the internet"

A new instance in a "public" subnet cannot run apt update. Step 1: check the subnet's route table. It has only the local route because the subnet still uses the main table, so add the public table association. Still failing. Step 2: check the instance's public IP. It has none because auto-assign was off, so attach an Elastic IP. Step 3: check the security group's outbound rules; the default allows all outbound, fine. Step 4: check the NACL; a custom NACL has no outbound allow for ports 80 and 443 or inbound allow for return ports 1024 to 65535, so add them. Now it works. Reachability Analyzer would have pointed to each blocker in turn.

Quick reference

  • VPC size: /16 to /28; plan non-overlapping RFC 1918 ranges for every VPC and office
  • AWS reserves 5 IPs per subnet: a /24 has 251 usable addresses
  • Public subnet = route 0.0.0.0/0 to an internet gateway, plus instances with public IPs
  • One internet gateway per VPC; NAT gateways are per AZ and must sit in a public subnet
  • NAT gateway: 5 to 100 Gbps, hourly plus per-GB charges; one per AZ for resilience
  • Each public IPv4 address costs about 0.005 USD per hour, attached or idle
  • Egress-only internet gateway = outbound-only access for IPv6
  • Reachability Analyzer finds the blocking hop; Flow Logs show ACCEPT and REJECT

Common mistakes

  • Putting the NAT gateway in a private subnet; it must sit in a public subnet.
  • Using one NAT gateway for all AZs; an AZ failure then breaks egress everywhere.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1How many usable IP addresses does a /26 subnet have in AWS?

Question 2

2A route table has 0.0.0.0/0 to a NAT gateway and 10.1.0.0/16 to a peering connection. Where does traffic to 10.1.4.7 go?

Question 3

3Private instances in two AZs share one NAT gateway in AZ a. What happens if AZ a fails?

Question 4

4Which component lets IPv6 instances start outbound connections while blocking inbound ones?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Split 10.20.0.0/16 into one public and one private /24 subnet in each of two AZs, and say how many usable IPs each has.

  2. Exercise 2medium

    Find every route table in a VPC and show, for each, its associated subnets and its default route target.

  3. Exercise 3hard

    Use Reachability Analyzer to check whether a private instance can reach the internet gateway on port 443, and read the result.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the default VPC?
Every Region in a new account has a default VPC (172.31.0.0/16) with a public subnet in each AZ. It is handy for quick tests, but build your own VPC for real workloads.
Can I change a VPC's CIDR after creating it?
You cannot change the primary CIDR, but you can add secondary CIDR blocks and create new subnets in them.
Why is my NAT gateway bill so high?
NAT gateways charge per GB processed. Large S3 or DynamoDB traffic should use gateway endpoints, and other AWS service traffic can use interface endpoints.
Do I still need a bastion host?
Usually not. Systems Manager Session Manager gives shell access to private instances with IAM control, logging and no open inbound ports.

Go deeper

ShareXLinkedInWhatsApp