Skip to main content

Cloud and deployment · Beginner · 22 min

Understand AWS VPCs: subnets, route tables, gateways and NAT

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · AWS CLI 2.42.4

You will have built a two-zone VPC with public and private subnets, an internet gateway, a NAT gateway and a free S3 endpoint using the AWS CLI, read every route table to see which subnets are public and why, and then deleted it all.

On this page

Almost everything you run in AWS, from a single EC2 server to a database or a container service, lives inside a VPC (virtual private cloud). When an app "cannot reach the internet", or a database is reachable by people who should not reach it, the cause is nearly always in a few pieces of the network: an address range, a subnet, a route table and a gateway. If you understand those pieces before you write any network code, you can read any AWS network and say where traffic will go.

Here is the whole model in five sentences:

  • A VPC is a private IPv4 address range, such as 10.20.0.0/16, in one AWS Region.
  • A subnet is a slice of that range, and it sits in exactly one availability zone (one data centre group within the Region).
  • Every subnet uses one route table, a list of "traffic for this destination goes to that target" rules.
  • A subnet is public when its route table sends 0.0.0.0/0 (everything) to an internet gateway. Nothing else makes it public.
  • A private subnet has no such route. Its servers can reach out through a NAT gateway in a public subnet, or reach some AWS services through a VPC endpoint with no internet path at all.

In this tutorial you build that layout across two availability zones with the AWS CLI, one piece at a time, and read the route tables after each change. Then you delete everything.

Diagram of a VPC 10.20.0.0/16 in Region eu-west-2 with two availability zones; each zone has a public subnet whose route table points to an internet gateway and a private subnet whose route table points to a NAT gateway in zone a and to an S3 gateway endpoint
The layout you build: two zones, two public and two private subnets

Cost. The VPC, subnets, route tables, internet gateway and the S3 gateway endpoint have no charge. Two things you create do bill by the hour: the NAT gateway and its public IPv4 address. On the AWS VPC pricing page, checked on 26 November 2026, a NAT gateway in US East (Ohio) costs $0.045 per hour plus $0.045 per GB it processes, and a public IPv4 address costs $0.005 per hour. Prices differ by Region, so read the page for yours. If you finish within an hour or two and run the clean-up step, the cost is a few US cents. If you stop part-way, jump to the clean-up step before you close your laptop.

Everything you create is tagged vpc-demo-..., and the IDs are saved to ~/vpc-demo/ids.env, so you can always find them again.

Step 1 · Point the CLI at your sandbox and choose a Region

This tutorial expects AWS CLI 2.42.4, installed from the signature-checked bundle in /tutorials/set-up-the-aws-cli-with-sso-and-named-profiles. If you have not done that yet, do it first; it also sets up the named profile you use here. Confirm the version:

Bash
aws --version

Point the CLI at your sandbox account. If you use a named profile, run export AWS_PROFILE=<your-sandbox-profile> first (replace the placeholder), and sign in with aws sso login if your session has expired. Then pick the Region to build in. This tutorial uses London (eu-west-2); replace it with the Region you use. Cape Town (af-south-1) works the same way, but it is an opt-in Region, so an account administrator has to enable it first.

Bash
export AWS_REGION=eu-west-2
aws sts get-caller-identity --query Account --output text
aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text

Check it worked

aws --version starts with aws-cli/2.42.4. get-caller-identity prints your 12-digit sandbox account ID. The last command lists the zones in your Region, such as eu-west-2a eu-west-2b eu-west-2c. AWS maps zone names to physical zones separately for each account, so your eu-west-2a may be a different building from someone else's, which is why you will read the names from AWS instead of typing them.

Step 2 · Look at the default VPC

Every Region in your account starts with a default VPC, made by AWS so you can launch a server without designing a network first. Look at it before you build your own, because it shows all the pieces at once:

Bash
DEFAULT_VPC=$(aws ec2 describe-vpcs --filters Name=is-default,Values=true \
  --query 'Vpcs[0].VpcId' --output text)
echo "$DEFAULT_VPC"
aws ec2 describe-vpcs --vpc-ids "$DEFAULT_VPC" --query 'Vpcs[0].CidrBlock' --output text
aws ec2 describe-subnets --filters Name=vpc-id,Values="$DEFAULT_VPC" \
  --query 'Subnets[].[AvailabilityZone,CidrBlock,MapPublicIpOnLaunch]' --output table
aws ec2 describe-route-tables --filters Name=vpc-id,Values="$DEFAULT_VPC" \
  --query 'RouteTables[].Routes[].[DestinationCidrBlock,GatewayId]' --output table

If the first command prints None, someone has deleted the default VPC in this Region; skip to the next step.

172.31.0.0/16 is written in CIDR notation. The /16 says the first 16 bits are fixed, which leaves 16 bits, or 65,536 addresses. Each default subnet is a /20 (4,096 addresses) in a different zone. The route table has two routes. local means "anything inside the VPC's own range stays inside the VPC"; every route table has it and you cannot remove it. 0.0.0.0/0 to an igw-... ID sends everything else to the internet gateway. That one route is why every default subnet is public, and with MapPublicIpOnLaunch set to True, a server you launch there gets a public address too.

That is convenient for a quick test and the wrong default for anything holding data: a database launched in the default VPC lands in a public subnet. The rest of this tutorial builds a network where you choose what is public.

Check it worked

You see the CIDR 172.31.0.0/16, one /20 subnet per zone, each with True in the last column, and a route table with local and 0.0.0.0/0 pointing at an igw- ID.

Step 3 · Create the VPC

Pick an address range that does not clash with networks you may later connect to, such as your office network or another VPC. Use one of the private ranges set aside in RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). A VPC's IPv4 range can be anything from /16 (65,536 addresses) down to /28 (16); a /16 leaves room to grow. This tutorial uses 10.20.0.0/16.

Make a working folder, create the VPC and save its ID:

Bash
mkdir -p ~/vpc-demo
cd ~/vpc-demo
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.20.0.0/16 \
  --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=vpc-demo}]' \
  --query Vpc.VpcId --output text)
echo "VPC_ID=$VPC_ID" > ids.env
echo "$VPC_ID"

AWS created more than you asked for. Every new VPC gets a main route table, and any subnet you do not explicitly attach to another table uses it. Read its routes:

Bash
aws ec2 describe-route-tables \
  --filters Name=vpc-id,Values="$VPC_ID" Name=association.main,Values=true \
  --query 'RouteTables[0].Routes[].[DestinationCidrBlock,GatewayId]' --output text

The only route is local. A subnet that uses this table can talk to other addresses in 10.20.0.0/16 and nowhere else. Leave the main table like this: if you later add a subnet and forget to associate it, it ends up private, which is the safe mistake.

Check it worked

echo "$VPC_ID" prints an ID starting vpc-, and cat ~/vpc-demo/ids.env shows the same ID. The route query prints one line: 10.20.0.0/16 followed by local.

Step 4 · Carve out subnets in two zones

An availability zone can fail on its own, so anything that matters runs in at least two. You need a public and a private subnet in each zone: four subnets. Read the first two zone names from AWS:

Bash
read -r AZ_A AZ_B <<< "$(aws ec2 describe-availability-zones \
  --filters Name=zone-type,Values=availability-zone \
  --query 'AvailabilityZones[0:2].ZoneName' --output text)"
echo "$AZ_A $AZ_B"

Plan the ranges before you create anything. Each subnet is a /24 (256 addresses), and the private ones start at 10.20.10.0 so the third number tells you the kind at a glance:

  • vpc-demo-public-a: first zone, 10.20.0.0/24
  • vpc-demo-public-b: second zone, 10.20.1.0/24
  • vpc-demo-private-a: first zone, 10.20.10.0/24
  • vpc-demo-private-b: second zone, 10.20.11.0/24

Create them and save each ID:

Bash
PUBLIC_A=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --availability-zone "$AZ_A" \
  --cidr-block 10.20.0.0/24 \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=vpc-demo-public-a}]' \
  --query Subnet.SubnetId --output text)
PUBLIC_B=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --availability-zone "$AZ_B" \
  --cidr-block 10.20.1.0/24 \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=vpc-demo-public-b}]' \
  --query Subnet.SubnetId --output text)
PRIVATE_A=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --availability-zone "$AZ_A" \
  --cidr-block 10.20.10.0/24 \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=vpc-demo-private-a}]' \
  --query Subnet.SubnetId --output text)
PRIVATE_B=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --availability-zone "$AZ_B" \
  --cidr-block 10.20.11.0/24 \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=vpc-demo-private-b}]' \
  --query Subnet.SubnetId --output text)
cat >> ~/vpc-demo/ids.env <<EOF
PUBLIC_A=$PUBLIC_A
PUBLIC_B=$PUBLIC_B
PRIVATE_A=$PRIVATE_A
PRIVATE_B=$PRIVATE_B
EOF
aws ec2 describe-subnets --filters Name=vpc-id,Values="$VPC_ID" \
  --query 'Subnets[].[Tags[?Key==`Name`].Value|[0],AvailabilityZone,CidrBlock,AvailableIpAddressCount]' \
  --output table

The names say "public" and "private", but AWS does not know that yet. Right now all four use the main route table, so all four are private. A name is a label for people; the route table decides.

The last column shows 251 free addresses in each /24, not 256. AWS keeps five in every subnet: the network address, the VPC router (.1), the DNS server (.2), one held for future use (.3) and the last address. Keep that in mind for small subnets: a /28 has 16 addresses but only 11 you can use.

Check it worked

The table has four rows, two in each zone, with the ranges from the plan and 251 in the last column.

Step 5 · Make two subnets public with an internet gateway

An internet gateway is the VPC's door to the internet. It is managed by AWS, has no size to choose, does not bill, and serves every zone. Create one and attach it to the VPC:

Bash
IGW_ID=$(aws ec2 create-internet-gateway \
  --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=vpc-demo-igw}]' \
  --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id "$IGW_ID" --vpc-id "$VPC_ID"
echo "IGW_ID=$IGW_ID" >> ~/vpc-demo/ids.env

Attaching it changes nothing yet: no route points at it. Make a route table for the public subnets, add the route, and associate both public subnets with it:

Bash
PUBLIC_RT=$(aws ec2 create-route-table --vpc-id "$VPC_ID" \
  --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=vpc-demo-public}]' \
  --query RouteTable.RouteTableId --output text)
echo "PUBLIC_RT=$PUBLIC_RT" >> ~/vpc-demo/ids.env
aws ec2 create-route --route-table-id "$PUBLIC_RT" \
  --destination-cidr-block 0.0.0.0/0 --gateway-id "$IGW_ID"
aws ec2 associate-route-table --route-table-id "$PUBLIC_RT" --subnet-id "$PUBLIC_A"
aws ec2 associate-route-table --route-table-id "$PUBLIC_RT" --subnet-id "$PUBLIC_B"

Those two associations are the moment the subnets become public. One more setting is separate from routing: whether a server launched there gets a public IPv4 address automatically. Without one, a server in a public subnet still cannot be reached from the internet. Turn it on for the public subnets only:

Bash
aws ec2 modify-subnet-attribute --subnet-id "$PUBLIC_A" --map-public-ip-on-launch
aws ec2 modify-subnet-attribute --subnet-id "$PUBLIC_B" --map-public-ip-on-launch

Each public IPv4 address bills by the hour while it exists, so in your own designs put only what must face the internet here: a load balancer, or a small jump host. Application servers and databases belong in the private subnets.

Read the table you made:

Bash
aws ec2 describe-route-tables --route-table-ids "$PUBLIC_RT" \
  --query 'RouteTables[0].{Routes: Routes[].[DestinationCidrBlock,GatewayId], Subnets: Associations[].SubnetId}'

Check it worked

The output lists two routes, 10.20.0.0/16 to local and 0.0.0.0/0 to your igw- ID, and two subnet IDs that match $PUBLIC_A and $PUBLIC_B. aws ec2 describe-subnets --subnet-ids "$PUBLIC_A" --query 'Subnets[0].MapPublicIpOnLaunch' prints true.

Step 6 · Let the private subnets out through a NAT gateway

A server in a private subnet still needs to download updates and call outside APIs. A NAT gateway lets it start connections out while nothing on the internet can start a connection in. The gateway sits in a public subnet with a public IPv4 address (an Elastic IP); the private subnets send their internet traffic to it, and it sends that traffic on through the internet gateway.

This step starts billing. The Elastic IP and the NAT gateway each cost money for every hour they exist, whether traffic flows or not (see the prices at the top). Run the clean-up step when you finish.

Allocate the Elastic IP and create the NAT gateway in the first public subnet. Each ID goes into ids.env straight away, so you can always find what is billing:

Bash
EIP_ALLOC=$(aws ec2 allocate-address --domain vpc \
  --tag-specifications 'ResourceType=elastic-ip,Tags=[{Key=Name,Value=vpc-demo-nat-a}]' \
  --query AllocationId --output text)
echo "EIP_ALLOC=$EIP_ALLOC" >> ~/vpc-demo/ids.env
NAT_A=$(aws ec2 create-nat-gateway --subnet-id "$PUBLIC_A" --allocation-id "$EIP_ALLOC" \
  --tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=vpc-demo-nat-a}]' \
  --query NatGateway.NatGatewayId --output text)
echo "NAT_A=$NAT_A" >> ~/vpc-demo/ids.env
aws ec2 wait nat-gateway-available --nat-gateway-ids "$NAT_A"

The wait command returns when the gateway is ready, which usually takes a few minutes. Now give the private subnets their own route table, with everything outside the VPC going to the NAT gateway:

Bash
PRIVATE_RT=$(aws ec2 create-route-table --vpc-id "$VPC_ID" \
  --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=vpc-demo-private}]' \
  --query RouteTable.RouteTableId --output text)
echo "PRIVATE_RT=$PRIVATE_RT" >> ~/vpc-demo/ids.env
aws ec2 create-route --route-table-id "$PRIVATE_RT" \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$NAT_A"
aws ec2 associate-route-table --route-table-id "$PRIVATE_RT" --subnet-id "$PRIVATE_A"
aws ec2 associate-route-table --route-table-id "$PRIVATE_RT" --subnet-id "$PRIVATE_B"

Notice the difference from the public table: the target is --nat-gateway-id, not --gateway-id with an igw- ID. Point a private subnet's 0.0.0.0/0 at the internet gateway and it is public, whatever it is called.

This layout has one NAT gateway, in the first zone, shared by both private subnets. That saves money, but if the first zone has an outage, the second zone's private servers lose their way out too. Production networks usually choose between one NAT gateway per zone, each with its own private route table (twice the hourly cost), or a regional NAT gateway (--availability-mode regional), which AWS spreads across the zones where you have workloads. Small projects often go without NAT altogether, as the next step shows.

Check it worked

aws ec2 describe-nat-gateways --nat-gateway-ids "$NAT_A" --query 'NatGateways[0].[State,SubnetId]' --output text prints available and the ID in $PUBLIC_A. aws ec2 describe-route-tables --route-table-ids "$PRIVATE_RT" --query 'RouteTables[0].Routes[].[DestinationCidrBlock,GatewayId || NatGatewayId]' --output text prints local for 10.20.0.0/16 and your nat- ID for 0.0.0.0/0.

Step 7 · Reach S3 through a gateway endpoint instead

A common surprise on the first AWS bill: private servers that read and write a lot of S3 data send it all through the NAT gateway, which charges for every GB it processes. A gateway endpoint gives a VPC a private path to S3 (or DynamoDB, the only other service that has one) with no hourly or per-GB charge. Other AWS services offer interface endpoints, which do bill by the hour.

Create one for S3 and attach it to the private route table:

Bash
S3_ENDPOINT=$(aws ec2 create-vpc-endpoint --vpc-id "$VPC_ID" \
  --vpc-endpoint-type Gateway \
  --service-name "com.amazonaws.${AWS_REGION}.s3" \
  --route-table-ids "$PRIVATE_RT" \
  --tag-specifications 'ResourceType=vpc-endpoint,Tags=[{Key=Name,Value=vpc-demo-s3}]' \
  --query VpcEndpoint.VpcEndpointId --output text)
echo "S3_ENDPOINT=$S3_ENDPOINT" >> ~/vpc-demo/ids.env
aws ec2 describe-route-tables --route-table-ids "$PRIVATE_RT" \
  --query 'RouteTables[0].Routes[].[DestinationCidrBlock || DestinationPrefixListId, GatewayId || NatGatewayId]' \
  --output text

The endpoint added a route for you. Its destination is a prefix list (pl-...), an AWS-maintained list of the address ranges S3 uses in this Region, and its target is the endpoint (vpce-...). Now the private table has two routes that both match a request to S3: the prefix list and 0.0.0.0/0. AWS picks the most specific route that matches, and a prefix list entry is always more specific than "everything", so S3 traffic goes through the endpoint and everything else still goes through NAT.

If the only thing your private servers need from outside is S3 (backups, uploads, static assets), a gateway endpoint without any NAT gateway may be all you need.

Check it worked

The route list has three lines: 10.20.0.0/16 to local, 0.0.0.0/0 to your nat- ID, and a pl- ID to your vpce- ID.

Step 8 · Read the route tables and classify every subnet

You now have three route tables in the VPC. Read them all at once:

Bash
aws ec2 describe-route-tables --filters Name=vpc-id,Values="$VPC_ID" \
  --query 'RouteTables[].{Table: Tags[?Key==`Name`].Value|[0], Main: Associations[?Main]|[0].Main, Routes: length(Routes), Subnets: length(Associations[?!Main])}' \
  --output table
Three route table cards: the main table has only the local route and is used by subnets with no association; the public table adds 0.0.0.0/0 to an internet gateway and serves the two public subnets; the private table adds 0.0.0.0/0 to a NAT gateway and an S3 prefix list to a gateway endpoint and serves the two private subnets
The three route tables in vpc-demo and the subnets that use them

The main table (no name tag, Main is True) has one route and no subnets of its own. The public table has two routes and two subnets. The private table has three routes and two subnets.

When you meet a VPC someone else built, ask one question per subnet: where does its 0.0.0.0/0 route go? This loop asks it for each of your four subnets. A subnet with no explicit association falls back to the main table, so the loop checks that too:

Bash
for SUBNET in "$PUBLIC_A" "$PUBLIC_B" "$PRIVATE_A" "$PRIVATE_B"; do
  NAME=$(aws ec2 describe-subnets --subnet-ids "$SUBNET" \
    --query 'Subnets[0].Tags[?Key==`Name`].Value|[0]' --output text)
  RT=$(aws ec2 describe-route-tables \
    --filters Name=association.subnet-id,Values="$SUBNET" \
    --query 'RouteTables[0].RouteTableId' --output text)
  if [ "$RT" = "None" ]; then
    RT=$(aws ec2 describe-route-tables \
      --filters Name=vpc-id,Values="$VPC_ID" Name=association.main,Values=true \
      --query 'RouteTables[0].RouteTableId' --output text)
  fi
  TARGET=$(aws ec2 describe-route-tables --route-table-ids "$RT" \
    --query 'RouteTables[0].Routes[?DestinationCidrBlock==`0.0.0.0/0`].[GatewayId || NatGatewayId]' \
    --output text)
  case "$TARGET" in
    igw-*) echo "$NAME: public (0.0.0.0/0 -> $TARGET)" ;;
    nat-*) echo "$NAME: private, out through $TARGET" ;;
    *)     echo "$NAME: private, no way out of the VPC" ;;
  esac
done | tee ~/vpc-demo/subnets.txt

Check it worked

The loop prints four lines: vpc-demo-public-a and vpc-demo-public-b are public with your igw- ID, and vpc-demo-private-a and vpc-demo-private-b go out through your nat- ID. If any line surprises you, the route table it used is the place to look.

Step 9 · Clean up

Delete in this order, because AWS refuses to delete anything another resource still depends on: the endpoint and NAT gateway first, then the Elastic IP once the NAT gateway has let go of it, then route tables, subnets, the internet gateway and finally the VPC. Every ID comes from ~/vpc-demo/ids.env; check with cat ~/vpc-demo/ids.env that each one is a vpc-demo resource before you run this, so nothing else in the account is touched. If you opened a new terminal, run export AWS_REGION=eu-west-2 (your Region) first.

Bash
cd ~/vpc-demo
source ./ids.env
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids "$S3_ENDPOINT"
aws ec2 delete-nat-gateway --nat-gateway-id "$NAT_A"
aws ec2 wait nat-gateway-deleted --nat-gateway-ids "$NAT_A"
aws ec2 release-address --allocation-id "$EIP_ALLOC"
for RT in "$PUBLIC_RT" "$PRIVATE_RT"; do
  for ASSOC in $(aws ec2 describe-route-tables --route-table-ids "$RT" \
      --query 'RouteTables[0].Associations[?!Main].RouteTableAssociationId' --output text); do
    aws ec2 disassociate-route-table --association-id "$ASSOC"
  done
  aws ec2 delete-route-table --route-table-id "$RT"
done
for SUBNET in "$PUBLIC_A" "$PUBLIC_B" "$PRIVATE_A" "$PRIVATE_B"; do
  aws ec2 delete-subnet --subnet-id "$SUBNET"
done
aws ec2 detach-internet-gateway --internet-gateway-id "$IGW_ID" --vpc-id "$VPC_ID"
aws ec2 delete-internet-gateway --internet-gateway-id "$IGW_ID"
aws ec2 delete-vpc --vpc-id "$VPC_ID"
cd ~
rm -rf ~/vpc-demo

Deleting the VPC also deletes its main route table. The NAT gateway stays visible with the state deleted for a while; it no longer bills.

Check it worked

aws ec2 describe-vpcs --filters Name=tag:Name,Values=vpc-demo --query Vpcs and aws ec2 describe-addresses --filters Name=tag:Name,Values=vpc-demo-nat-a --query Addresses both print [], and ~/vpc-demo is gone. In the VPC console, the NAT gateways page shows vpc-demo-nat-a as Deleted.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.