Sub topic 2
Subnets and Availability Zones as the foundation
This chapter assumes you know what an EC2 instance, a security group and a route table are, and that you can run AWS CLI commands. It covers the network layer that every later chapter relies on. Load balancers and Auto Scaling groups both ask you for subnets, and the subnets you choose decide how well your application survives a failure.
Regions, Availability Zones and subnets
A Region is a geographic area such as eu-west-1. An Availability Zone (AZ) is one or more isolated data centers inside a Region, with independent power and networking. AZs in a Region are connected by low-latency links. A failure in one AZ should not take down the others.
A subnet is a range of IP addresses inside your VPC. Each subnet lives in exactly one AZ and cannot span two. To use two AZs, you create at least two subnets.
This one-to-one link is the foundation of high availability on AWS. You never place a resource "in an AZ" directly. You place it in a subnet, and the subnet determines the AZ.
Public and private subnets
A subnet is "public" when its route table has a route to an internet gateway, usually 0.0.0.0/0. Otherwise it is private. The subnet has no public/private setting of its own. The route table decides.
A common layout for a web tier:
- An internet-facing load balancer goes in public subnets, because it must accept traffic from the internet.
- EC2 instances go in private subnets. They receive traffic only from the load balancer's security group.
- Private instances that need outbound internet access, for example to download packages, use a NAT gateway in a public subnet.
An internal load balancer, used between tiers of the same application, can sit entirely in private subnets.
What load balancers require from subnets
When you create a load balancer, you choose the subnets it uses. AWS places a load balancer node in each one. The rules are:
- An Application Load Balancer (ALB) needs subnets in at least two AZs.
- A Network Load Balancer (NLB) accepts one or more subnets.
- You can enable only one subnet per AZ for a given load balancer.
- Each subnet needs at least 8 free IP addresses for the load balancer's nodes. AWS recommends a
/27or larger so the nodes can grow as traffic grows.
The load balancer can send traffic only to targets in the AZs it is enabled for. Suppose you enable us-east-1a and us-east-1b, and an instance runs in us-east-1c. That instance can be registered, but it will not receive traffic from the load balancer. Registering targets this way is a common source of confusion, and the troubleshooting chapter returns to it.
What Auto Scaling groups require from subnets
An Auto Scaling group takes a list of subnets in its --vpc-zone-identifier setting, as in the example from the Topic Spine:
--vpc-zone-identifier "subnet-aaaa1111,subnet-bbbb2222"The group launches instances into these subnets and tries to keep the instance count balanced across the AZs they belong to. If one AZ becomes unavailable, the group launches replacements in the remaining AZs. When the AZ recovers, it rebalances.
The load balancer and the group should use the same set of AZs. If the group has an AZ that the load balancer lacks, instances there are healthy but idle. If the load balancer has an AZ with no instances, its node there has nothing to route to. Cross-zone load balancing, covered in a later chapter, softens this but does not remove the need to match.
Sizing subnets
AWS reserves 5 IP addresses in every subnet: the network address, the VPC router, the DNS server, one reserved for future use, and the broadcast address. A /24 has 256 addresses and 251 usable ones.
Plan for growth. An Auto Scaling group that scales out to 200 instances needs 200 free addresses in its subnets, plus addresses for load balancer nodes, network interfaces and other services. Subnet CIDR ranges cannot be resized after creation, so a subnet that is too small means creating new ones. Large private subnets such as /22 or /20 are common. Public subnets that hold only load balancer nodes and NAT gateways can stay small.
AZ names and AZ IDs
The name us-east-1a is mapped to a physical zone separately for each AWS account. Your us-east-1a may not be the same physical zone as another account's. The AZ ID, such as use1-az1, is the same for everyone. Use AZ IDs when you need to line up zones across accounts, for example when a service in one account must share an AZ with a service in another to avoid cross-AZ traffic.
Inspecting your subnets
This command lists the subnets in a VPC with their AZ and remaining free addresses:
aws ec2 describe-subnets \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 \
--query 'Subnets[].{Id:SubnetId,Az:AvailabilityZone,AzId:AvailabilityZoneId,Cidr:CidrBlock,Free:AvailableIpAddressCount}' \
--output tableThis script checks whether a list of subnets covers at least two AZs, which is the ALB requirement:
import boto3
ec2 = boto3.client("ec2")
def azs_for_subnets(subnet_ids):
response = ec2.describe_subnets(SubnetIds=subnet_ids)
return {s["AvailabilityZone"] for s in response["Subnets"]}
subnets = ["subnet-aaaa1111", "subnet-bbbb2222"]
zones = azs_for_subnets(subnets)
print("Availability Zones:", sorted(zones))
if len(zones) < 2:
print("An Application Load Balancer needs subnets in at least two AZs.")Common errors
| Symptom | Likely cause |
|---|---|
| `create-load-balancer` fails saying subnets must be in different AZs | Both subnets are in the same AZ |
| Load balancer creation fails for lack of free IP addresses | A subnet has fewer than 8 free addresses |
| Internet-facing load balancer is unreachable | Its subnets have no route to an internet gateway |
| Instances are healthy but receive no traffic | Their AZ is not enabled on the load balancer |
| Scale-out fails with insufficient capacity or address errors | Subnets are full, or the AZ lacks capacity for the instance type |
Exercise
A team creates an internet-facing ALB in two subnets, both in us-east-1a, and an Auto Scaling group across subnets in us-east-1a and us-east-1b. What goes wrong, and how do you fix it?
Show answer
The ALB creation fails because an ALB needs subnets in at least two different AZs. Even with a valid pair, instances in an AZ the load balancer is not enabled for would receive no traffic. Fix it by giving the ALB one public subnet in us-east-1a and one in us-east-1b, matching the Auto Scaling group's AZs.