How Route Tables and Internet Gateways Work in a VPC

How AWS VPC route tables and internet gateways work together: public subnets, route priority, NAT, troubleshooting and AWS CLI setup steps.

Aug 6, 2025 - 16:33
Nov 11, 2026 - 04:36
 0  39
How Route Tables and Internet Gateways Work in a VPC

In an Amazon VPC, the internet gateway is the door to the internet and the route table decides which subnets may use it. A subnet becomes public when its route table sends 0.0.0.0/0 (or ::/0 for IPv6) to the internet gateway, and an instance in it also needs a public IPv4 address, an Elastic IP or an IPv6 address.

This guide walks through how the two pieces interact, using the definitions in the Amazon VPC User Guide, then builds a working public subnet with the AWS CLI. It sits under our cloud tools comparison for AWS, Azure and GCP, and it assumes you already know what a VPC is. If not, read what a VPC is and how it secures AWS resources first.

Key takeaways

  • Every VPC gets a main route table. Any subnet you do not explicitly associate with another table uses the main one.
  • Every route table has a local route for the VPC CIDR, so traffic inside the VPC always stays inside it.
  • An internet gateway does nothing until it is attached to the VPC and a route table points traffic at it.
  • "Public subnet" is not a setting. It is simply a subnet whose route table has a route to an internet gateway.
  • The most specific matching route wins (longest prefix match). For equal prefixes, static routes beat propagated VPN routes.
  • For IPv4, the internet gateway performs one-to-one NAT between an instance's private address and its public or Elastic IP. It does not give private instances outbound access; that is a NAT gateway's job.

The two components in plain terms

AWS describes a route table as a set of rules, called routes, that decide where traffic from a subnet or gateway is sent. Each route has a destination, which is a CIDR block or a prefix list, and a target, such as an internet gateway, NAT gateway, peering connection, transit gateway or network interface. When a packet leaves an instance, the VPC router looks up its destination address in the subnet's route table and forwards it to the matching target.

An internet gateway is a VPC component that allows communication between the VPC and the internet. AWS describes it as horizontally scaled, redundant and highly available, and says it does not cause availability risks or bandwidth constraints on your traffic. You do not size it, patch it or place it in an Availability Zone. It supports both IPv4 and IPv6, and there is no charge for the gateway itself, although data transfer charges apply to the instances that use it.

The relationship is simple once you see it: the internet gateway is a possible target, and the route table decides who sends traffic to that target. Attaching a gateway to a VPC changes nothing for any subnet until a route points at it.

Route table concepts you need

TermWhat it means
Main route tableCreated with the VPC. Controls routing for every subnet not explicitly associated with another table.
Custom route tableA table you create, usually one for public subnets and one or more for private subnets.
Local routeA default route for the VPC CIDR. There is one for IPv4 and one for IPv6 if the VPC has both.
Subnet associationLinks a subnet to a route table. One table can serve many subnets, but a subnet is associated with only one table.
Gateway route tableA table associated with an internet gateway or virtual private gateway (an edge association) to steer inbound traffic, for example through a firewall appliance.
PropagationAutomatic addition of Site-to-Site VPN routes when a virtual private gateway is attached and propagation is enabled.

The main route table deserves extra care. Because any new subnet falls back to it, adding an internet gateway route to the main table quietly makes every future subnet public. The usual practice is to leave the main table with only the local route and create an explicit custom table for public subnets. A mistake in the private path then fails closed instead of exposing instances.

How a packet finds the internet

Follow one outbound request from an EC2 instance at 10.0.1.25 in a public subnet to a public web server.

  1. The instance sends the packet to the VPC router with its private source address.
  2. The router checks the route table associated with the subnet. The destination is not inside 10.0.0.0/16, so the local route does not match. The 0.0.0.0/0 route does, and its target is the internet gateway.
  3. The internet gateway translates the private source address to the instance's public IPv4 address or Elastic IP, one to one, and sends the packet out.
  4. The reply comes back to the public address. The gateway translates the destination back to 10.0.1.25 and delivers it into the VPC.
  5. Security groups and network ACLs are checked along the way. Security groups are stateful, so allowed outbound traffic gets its reply automatically. Network ACLs are stateless, so return traffic on ephemeral ports must also be allowed.

The AWS documentation stresses a point that trips up many beginners: the instance itself only knows its private address. You will not see the public IPv4 address on the network interface inside the operating system, because the translation happens at the gateway.

Why an instance still cannot reach the internet

When an instance in a "public" subnet cannot connect, it is almost always one of five things, and it helps to check them in order:

  • The internet gateway is not attached to the VPC, or it is attached to a different VPC.
  • The subnet is associated with the wrong route table, often the main table, which has no internet route.
  • The instance has no public IPv4 address or Elastic IP. AWS's own example notes that instances in a private subnet cannot reach the internet even if they have public IPs, and the reverse is also true: a route without a public address goes nowhere for IPv4.
  • A security group or network ACL blocks the traffic, often return traffic on ephemeral ports in an ACL.
  • A more specific route sends the traffic somewhere else, for example a peering or transit gateway route that overlaps the destination.

VPC Reachability Analyzer can test a path between two resources and tell you which component blocks it, which beats guessing. The Amazon VPC guide also has a troubleshooting section for route table reachability.

Route priority: which rule wins

A route table can hold overlapping routes, so the order of evaluation matters. AWS documents the priority as follows. First, the longest prefix match: a /32 route beats a /24 that covers the same address, and a /16 beats 0.0.0.0/0. Next, among routes with the same destination, static routes (internet gateway, NAT gateway, network interface, instance, gateway endpoint, transit gateway, peering, Gateway Load Balancer endpoint) win over prefix list routes, which win over propagated routes. Within propagated routes, Direct Connect BGP routes come before VPN static routes, which come before VPN BGP routes.

AWS's own example shows the practical effect. A table with 10.0.0.0/16 to local, 172.31.0.0/16 to a peering connection and 0.0.0.0/0 to an internet gateway sends 172.31.x.x traffic over the peering connection and everything else to the internet. IPv4 and IPv6 routes are evaluated independently, so you need separate 0.0.0.0/0 and ::/0 entries if you want both families to reach the gateway.

Public and private subnets together

The typical layout has public subnets for load balancers and bastion or NAT resources, and private subnets for application servers and databases. Each tier gets its own route table:

Route tableDestinationTarget
Public10.0.0.0/16local
Public0.0.0.0/0igw-xxxx
Private (AZ a)10.0.0.0/16local
Private (AZ a)0.0.0.0/0nat-xxxx (in public subnet, AZ a)

Private instances reach the internet for updates through a NAT gateway placed in a public subnet. The NAT gateway itself uses the public subnet's route to the internet gateway. Nothing on the internet can start a connection to the private instances, which is the point. Use one NAT gateway per Availability Zone and one private route table per zone so a zone outage does not cut off the others. Our posts on designing public and private subnets and why the NAT gateway matters in VPC design go deeper on that layout, and NAT instances versus NAT gateways compares the two options.

For IPv6, addresses are globally unique and public by default. If you want IPv6 instances to make outbound connections without being reachable from the internet, AWS provides an egress-only internet gateway, which you target with a ::/0 route from the private table.

Build it with the AWS CLI

The following commands create a VPC, a public subnet, an internet gateway and a custom route table, then wire them together. Replace the IDs with the values each command returns.

# 1. VPC and subnet
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-0abc --cidr-block 10.0.1.0/24 --availability-zone ap-south-1a

# 2. Internet gateway, then attach it
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --internet-gateway-id igw-0abc --vpc-id vpc-0abc

# 3. Custom route table with a default route to the gateway
aws ec2 create-route-table --vpc-id vpc-0abc
aws ec2 create-route --route-table-id rtb-0abc --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc

# 4. Associate the subnet and auto-assign public IPv4 addresses on launch
aws ec2 associate-route-table --route-table-id rtb-0abc --subnet-id subnet-0abc
aws ec2 modify-subnet-attribute --subnet-id subnet-0abc --map-public-ip-on-launch

# 5. Inspect the result
aws ec2 describe-route-tables --route-table-ids rtb-0abc

Step 4 matters more than it looks. Without the explicit association, the subnet stays on the main route table and has no internet route. And without public IP assignment, instances launched there get only private addresses. Note that AWS now charges for public IPv4 addresses, so check the current Amazon VPC pricing page before enabling auto-assignment across many subnets.

The default VPC that AWS creates in each region already has an internet gateway, a 0.0.0.0/0 route and public IPv4 auto-assignment on its default subnets. A nondefault VPC has none of these, which is why a fresh VPC built by hand cannot reach the internet until you complete the steps above.

Design patterns and common mistakes

A few habits keep VPC routing predictable as an environment grows. Name and tag every route table by tier and zone, for example prod-public and prod-private-ap-south-1a, so the console and CLI output make sense at a glance. Keep one public route table shared by all public subnets, because they all need the same internet route, and one private table per Availability Zone, because each zone should use its own NAT gateway.

Plan CIDR ranges before you build. Overlapping ranges cannot be peered and make transit gateway routing awkward, and they are painful to change later. Leave room for more subnets than you need today. The default quota is 200 subnets and 200 route tables per VPC, which is plenty for most designs, and routes per table default to 500 non-propagated entries, so if you find yourself near that limit, the design probably needs a transit gateway or prefix lists instead of more individual routes.

The mistakes that show up most often in reviews are predictable. Someone adds a 0.0.0.0/0 route to the internet gateway in the main route table "to fix a quick problem," and every subnet created afterwards becomes public. Someone places a database in a subnet associated with the public table because it was the default selection in the console. Someone deletes a NAT gateway to save money and forgets the private tables still point at it, leaving a blackhole route. The route table shows such routes with a blackhole state, which is worth checking for in automated audits.

Infrastructure as code prevents most of these by making associations explicit and reviewable. In Terraform, for example, every subnet should have an explicit route table association resource, so nothing falls back to the main table by accident.

Gateway route tables and inspection

Route tables normally control traffic leaving subnets. A gateway route table, associated with the internet gateway through an edge association, controls traffic entering the VPC. You use it to send inbound traffic for a subnet to a network interface of a firewall or inspection appliance first. The appliance inspects the traffic and forwards it on. This is how centralised inspection designs with third-party firewalls or AWS Network Firewall are built, and it is worth knowing for architecture interviews even if you never configure one by hand.

Monitoring and governance

Routing problems are easier to find with data. VPC Flow Logs record accepted and rejected traffic at the interface, subnet or VPC level and can be sent to CloudWatch Logs or S3. A spike in rejected flows after a change is a quick sign that an ACL or security group, not routing, is the cause. AWS Config can track changes to route tables and gateways, so you can answer "who added that 0.0.0.0/0 route" during an incident review. And defining the whole network in infrastructure as code, whether CloudFormation or Terraform, means a route table change goes through code review before it reaches production.

Connecting VPCs to each other adds more routes to the same tables. Peering needs a route in both VPCs, and overlapping CIDRs are not allowed. Our post on VPC peering across regions and accounts covers that case.

If you are preparing for the AWS Solutions Architect Associate exam, VPC routing questions appear in many forms. The AWS Solutions Architect Associate course covers these labs in the Kharadi, Pune classroom and live online across India.

FAQ

What makes a subnet public in AWS?

A subnet is public when its associated route table has a route to an internet gateway, usually 0.0.0.0/0 or ::/0. There is no separate "public" switch on the subnet.

Can a VPC have more than one internet gateway?

No. The Amazon VPC quotas page says you can attach only one internet gateway to a VPC at a time. It is horizontally scaled and highly available, so you do not need a second one for capacity.

Does attaching an internet gateway expose all my instances?

No. Only subnets whose route tables point at the gateway can use it, and instances also need a public IPv4 address, Elastic IP or IPv6 address, plus security group and network ACL rules that allow the traffic.

What is the main route table used for?

It controls routing for any subnet that is not explicitly associated with another route table. Keeping it private, with only the local route, prevents new subnets from becoming public by accident.

Which route wins when two routes overlap?

The most specific route (longest prefix) wins. For identical destinations, static routes take priority over prefix list routes, which take priority over propagated VPN and Direct Connect routes.

How do private instances reach the internet for updates?

Through a NAT gateway in a public subnet. The private route table sends 0.0.0.0/0 to the NAT gateway, which in turn uses the public subnet's route to the internet gateway.

Does an internet gateway cost money?

AWS says there is no charge for the internet gateway itself. Data transfer for instances using it is charged, and public IPv4 addresses have their own charge.

References

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0
Mridul I am a passionate technology enthusiast with a strong focus on DevOps, Cloud Computing, and Cybersecurity. Through my blogs at DevOps Training Institute, I aim to simplify complex concepts and share practical insights for learners and professionals. My goal is to empower readers with knowledge, hands-on tips, and industry best practices to stay ahead in the ever-evolving world of DevOps.