Sub topic 1
VPC building blocks you will rely on
Transit Gateway and PrivateLink are both configured with the same few VPC objects: CIDR blocks, subnets, route tables, network interfaces, and security controls. This chapter reviews the details of each that matter later in the tutorial. It also lists the mistakes that cause most connectivity problems.
Prerequisites
This chapter assumes you have:
- Basic VPC concepts: CIDR blocks, subnets, and route tables.
- A working idea of security groups and network ACLs.
- The AWS CLI installed and configured, or access to the console.
- A general understanding of what a Network Load Balancer does. Later chapters use one to publish a service.
If you are comfortable with these, treat this chapter as a checklist. If a section feels unfamiliar, spend time there before moving on.
The objects and why they matter later
| Object | What it does | Where it matters later |
|---|---|---|
| VPC | A private network with one or more CIDR blocks, scoped to one Region | Each VPC becomes a transit gateway attachment or a PrivateLink consumer |
| Subnet | A slice of the VPC CIDR that lives in exactly one Availability Zone | Attachments and endpoints place network interfaces in subnets |
| Route table | A list of destination-to-target rules, associated with subnets | Every path to a transit gateway starts with a route here |
| Network interface (ENI) | A virtual network card with a private IP address | Attachments and interface endpoints are implemented as ENIs |
| Security group | A stateful, allow-only firewall attached to ENIs | Controls who can reach an interface endpoint |
| Network ACL | A stateless allow/deny filter attached to subnets | Can silently block return traffic if misconfigured |
CIDR blocks and address planning
A VPC has a primary IPv4 CIDR block between /16 and /28, and you can add secondary blocks. Subnets carve addresses out of those blocks. AWS reserves five addresses in every subnet: the network address, the VPC router, the DNS server, one reserved for future use, and the broadcast address. A /28 subnet therefore has 11 usable addresses.
Address planning matters more once VPCs connect. Transit Gateway routes by destination CIDR, so two attached VPCs with overlapping ranges cannot both be reached by the same route table. Peering rejects overlapping ranges outright. PrivateLink is the one option that tolerates overlap, which is why a later chapter covers it. Choose non-overlapping CIDRs from the start. A common approach is to give each VPC a distinct /16 out of 10.0.0.0/8, for example 10.0.0.0/16 for production and 10.1.0.0/16 for staging.
Subnets and Availability Zones
A subnet belongs to one Availability Zone (AZ). A subnet is called public or private based only on its route table. If the table has a route to an internet gateway, the subnet is public. Otherwise it is private.
AZ placement is important for two reasons:
- A transit gateway attachment needs at least one subnet per AZ you want to use. Traffic can only enter the transit gateway through an AZ where the VPC has an attachment subnet.
- An interface endpoint creates one network interface in each subnet you choose. For availability, choose subnets in several AZs.
Also remember that AZ names such as us-east-1a map to different physical zones in different accounts. When two accounts need to line up their AZs, compare AZ IDs such as use1-az1, not names.
Route tables and how a route is chosen
Every VPC has a main route table, and each subnet uses either an explicit association or the main table. Each table starts with a local route covering the VPC CIDR, and you cannot delete it. When several routes match a destination, AWS picks the most specific one, which is the longest prefix. If prefix lengths tie, a static route takes precedence over a propagated one.
For example, consider a table with these routes:
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 10.1.0.0/16 | tgw-0123456789abcdef0 |
| 0.0.0.0/0 | nat-0123456789abcdef0 |
A packet to 10.1.4.20 matches both 10.1.0.0/16 and 0.0.0.0/0. The /16 is more specific, so it goes to the transit gateway. A packet to 10.0.3.9 uses the local route and never leaves the VPC.
This example creates a VPC with two subnets in different AZs and one shared route table:
VPC_ID=$(aws ec2 create-vpc \
--cidr-block 10.0.0.0/16 \
--query 'Vpc.VpcId' --output text)
SUBNET_A=$(aws ec2 create-subnet \
--vpc-id "$VPC_ID" \
--cidr-block 10.0.1.0/24 \
--availability-zone us-east-1a \
--query 'Subnet.SubnetId' --output text)
SUBNET_B=$(aws ec2 create-subnet \
--vpc-id "$VPC_ID" \
--cidr-block 10.0.2.0/24 \
--availability-zone us-east-1b \
--query 'Subnet.SubnetId' --output text)
RTB_ID=$(aws ec2 create-route-table \
--vpc-id "$VPC_ID" \
--query 'RouteTable.RouteTableId' --output text)
aws ec2 associate-route-table --route-table-id "$RTB_ID" --subnet-id "$SUBNET_A"
aws ec2 associate-route-table --route-table-id "$RTB_ID" --subnet-id "$SUBNET_B"
aws ec2 describe-route-tables \
--route-table-ids "$RTB_ID" \
--query 'RouteTables[0].Routes'The last command prints one route, the local route for 10.0.0.0/16. Later chapters add routes to this table.
Traffic is evaluated at three points
For a connection between two resources, traffic passes several checks, and any one of them can drop it:
- The route table of the sender's subnet must have a route to the destination.
- The network ACLs on both subnets must allow the traffic. NACLs are stateless, so you must allow the return traffic explicitly, including ephemeral ports (1024-65535).
- The security groups on both network interfaces must allow it. Security groups are stateful, so return traffic is allowed automatically.
The return path is a frequent source of confusion. A route table is not a firewall, but a missing route in the destination VPC is the most common reason that a connection works one way and nothing comes back. Whenever you add a route from VPC A to VPC B, check that VPC B has a route back to VPC A.
Network interfaces and DNS
Both services you will use are built on ENIs. A transit gateway attachment places one ENI in each attachment subnet, and these ENIs are how the VPC reaches the transit gateway. An interface endpoint places ENIs in your subnets that have private IP addresses from your own CIDR. Because these addresses come from your subnets, they consume IPs, so leave room when sizing small subnets.
DNS has two VPC attributes you should know:
enableDnsSupportturns on the Amazon-provided resolver at the VPC base address plus two (for 10.0.0.0/16, that is 10.0.0.2).enableDnsHostnamesgives instances public and private DNS names, and interface endpoints need it for their private DNS names to work.
You can check and set them like this:
aws ec2 describe-vpc-attribute \
--vpc-id "$VPC_ID" --attribute enableDnsHostnames
aws ec2 modify-vpc-attribute \
--vpc-id "$VPC_ID" --enable-dns-hostnamesCommon errors to watch for
- Overlapping CIDRs. The VPC accepts them, but routing between the two VPCs fails later. Plan ranges before creating VPCs.
- A subnet that uses the wrong route table. A route you added never takes effect because the subnet is associated with a different table. Check associations with
describe-route-tables. - One AZ without an attachment subnet. Instances in that AZ cannot send traffic to the transit gateway through their own AZ's attachment.
- Missing return routes. Traffic leaves, but replies are dropped.
- Restrictive NACLs. They block reply traffic even when security groups allow it.
- Exhausted subnet addresses. New endpoint ENIs fail to create when the subnet has no free IPs.
Exercise
A subnet's route table has the routes 10.0.0.0/16 (local), 10.1.0.0/16 (transit gateway), and 0.0.0.0/0 (NAT gateway). Which target does a packet to 10.1.7.5 use, and why? What would happen to a packet to 10.0.9.9?
Show answer
The packet to 10.1.7.5 goes to the transit gateway. It matches both 10.1.0.0/16 and 0.0.0.0/0, and the route table chooses the most specific match (longest prefix), which is the /16. The packet to 10.0.9.9 matches the local route for 10.0.0.0/16 and stays inside the VPC, because the local route is more specific than 0.0.0.0/0.
Exercise
You added a route in VPC A to reach VPC B, and security groups allow the traffic in both directions. Connections from A to B still time out. Name two things to check.
Show answer
First, check that VPC B's route table has a route back to VPC A's CIDR, because replies need a return path. Second, check the network ACLs on both subnets, since they are stateless and must allow the return traffic on ephemeral ports. You should also confirm that the sending subnet is associated with the route table you edited.