Lesson 1 of 4 Amazon VPC

Designing VPC Address Space, Subnets and Route Tables

This lesson explains how VPC address space, subnets and routing fit together, since every architecture question involving servers or databases involves a VPC.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how to plan VPC address space for growth and connectivity.
  • Explain how subnets are placed across Availability Zones and tiers.
  • Explain how route tables make a subnet public or private.

Address space

A VPC's address space must be planned carefully, because it limits how far the network can grow and determines whether it can connect to other networks. A VPC is Regional, with a primary IPv4 CIDR block between /16 and /28 in size, optional secondary CIDR blocks and optional IPv6.

VPCs should use private ranges, from RFC 1918, that do not overlap with on-premises networks or with other VPCs they may ever connect to, because overlapping ranges cannot be routed between; Amazon VPC IP Address Manager (IPAM) plans addresses across accounts to prevent overlaps. Room for growth matters too: a /16 VPC divided into /20 or /24 subnets for each tier in each Availability Zone is a common layout. Amazon EKS pods and ECS tasks in awsvpc mode each use one IP address, so container subnets need to be larger. A VPC that runs out of addresses can be extended with a secondary CIDR block and new subnets.

Fill in the value

Sizing subnets

Exam skills 3.4.2 · 1.2.1

Work out usable addresses and VPC sizes.

Log in to try this activity Interactive activities are free with an account.

Subnets

A subnet is a range of addresses within a VPC, and it lives in exactly one Availability Zone, so a design that must survive the loss of a zone needs subnets in at least two. AWS reserves five addresses in every subnet: the network address, the VPC router, the DNS server, one for future use and the broadcast address. A /24 subnet therefore has 251 usable addresses, not 256, which matters when sizing small subnets.

Subnets are usually organised in tiers by how much exposure their resources need. The web tier is public and holds only what must be reachable from the internet, such as load balancers and NAT gateways. The application tier is private and holds instances or containers, which receive traffic only from the load balancer. The data tier is private with no route to the internet at all and holds databases, reachable only from the application tier. The following table summarises the tiers.

TierSubnetContains
WebPublicLoad balancers, NAT gateways
ApplicationPrivateInstances or containers
DataPrivate, no internet routeDatabases

Repeat each tier in two or three AZs.

The following diagram shows the three tiers repeated in two Availability Zones.

A VPC with three subnet tiers repeated in two Availability Zones. The public web tier holds only the load balancer and NAT gateways and routes to the internet gateway. The private application tier holds instances or containers that receive traffic only from the load balancer. The data tier has no route to the internet and holds databases reachable only from the application tier.
The CIDR is illustrative. Each tier is a separate subnet in each zone, so the design has six subnets.
Sort into groups

Which tier?

Exam skills 1.2.2 · 1.2.1

Place each resource in the subnet tier it belongs to.

Log in to try this activity Interactive activities are free with an account.

Route tables

Every subnet is associated with a route table, which decides where traffic leaving the subnet goes. Whether a subnet is public or private is decided entirely by its default route, 0.0.0.0/0. In a public subnet, the default route points to an internet gateway, so resources with public IP addresses can reach, and be reached from, the internet. In a private subnet, the default route points to a NAT gateway, which allows outbound connections only, or there is no default route, so the subnet has no internet access at all. The following table summarises the rule.

Subnet type0.0.0.0/0 route
PublicInternet gateway
PrivateNAT gateway, or none

The following list describes further characteristics of route tables.

  • Every table has a local route for the VPC CIDR.
  • Routes can also target peering, transit gateways, virtual private gateways, gateway endpoints and appliances; the most specific route wins.
  • Public-subnet instances need a public IPv4 or Elastic IP to be reachable; every public IPv4 address is charged.

The following diagram shows the route tables for the three kinds of subnet.

How the default route decides a subnet's type. Every route table has a local route for the VPC CIDR. A public subnet's default route, 0.0.0.0/0, points to an internet gateway; a private subnet's points to a NAT gateway, which allows outbound connections only; and an isolated subnet has no default route at all.
Only the default route differs; the local route that connects the subnets inside the VPC is always present.
Worked example

Planning a VPC for growth

A company is building its first production VPC. It expects to add hundreds of instances and containers, and to connect to on premises (10.0.0.0/16) later.

DecisionChoiceReason
VPC CIDR10.20.0.0/16Does not overlap with on premises; 65,536 addresses
Availability ZonesThreeHigh availability
Public subnetsThree /24s (one per AZ)Load balancers and NAT gateways only
Private application subnetsThree /20sRoom for many instances and container tasks
Private database subnetsThree /24sDatabases isolated from the application tier

AWS reserves five addresses in every subnet, and containers using awsvpc networking consume one IP each, which is why application subnets are sized generously. Choosing a CIDR that overlaps the on-premises network would make the later VPN or Direct Connect connection impossible to route.

Check yourself

What makes a subnet public?

Show answer

Its route table has a route to an internet gateway. Instances in it also need public IP addresses to be reached from the internet.

Exam Tips

Remember for the exam

  • Five addresses are reserved in every subnet, so a /28 gives 11 usable addresses.
  • A subnet is public only if its route table points to an internet gateway.
  • Subnets belong to one Availability Zone, so spread each tier across at least two zones for high availability.
  • Plan non-overlapping CIDRs before connecting VPCs or on-premises networks.
  • When a VPC runs out of IP addresses, add a secondary CIDR and new subnets.
  • Databases belong in private subnets with no route to the internet.