Sub topic 2
Subnets and Availability Zones as the Foundation
This chapter assumes you can launch EC2 instances, know what a VPC and a route table are, and can run AWS CLI commands. It covers the network layout that load balancers depend on, then how to choose among the load balancer types.
Subnets and Availability Zones
An Availability Zone (AZ) is one or more isolated data centers inside an AWS Region. A subnet is a range of IP addresses in your VPC. Each subnet lives in exactly one AZ and cannot span two. To run in two AZs, you create at least two subnets.
Both services in this tutorial work through subnets:
- A load balancer is placed in the subnets you choose. It runs a load balancer node in each of those AZs.
- An Auto Scaling group launches instances into the subnets you give it, and balances them across the AZs.
If your subnets cover only one AZ, a failure of that AZ takes the whole application down. Use at least two AZs.
A subnet is public if its route table sends internet traffic (0.0.0.0/0) to an internet gateway. Otherwise it is private. A common layout for a web tier looks like this:
- Public subnets hold an internet-facing load balancer.
- Private subnets hold the application instances.
List your subnets and the AZ of each:
aws ec2 describe-subnets \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 \
--query 'Subnets[].[SubnetId,AvailabilityZone,CidrBlock,MapPublicIpOnLaunch]' \
--output tableElastic Load Balancing and choosing a type
Elastic Load Balancing (ELB) is the AWS service that spreads incoming traffic across healthy targets, such as EC2 instances, in one or more AZs. It is managed, so AWS scales and replaces the load balancer nodes for you.
You pick a type by the layer of traffic it handles:
| Type | OSI layer | Traffic | Typical use |
|---|---|---|---|
| Application Load Balancer (ALB) | 7 | HTTP, HTTPS, gRPC | Websites and APIs, routing by path, host or header |
| Network Load Balancer (NLB) | 4 | TCP, UDP, TLS | Very high throughput, low latency, static IPs per AZ |
| Gateway Load Balancer (GWLB) | 3 | IP packets (GENEVE) | Firewalls and inspection appliances |
The Classic Load Balancer is the previous generation. Use it only for existing setups.
Use these rules to decide:
- For HTTP or HTTPS and content-based routing, choose the ALB. It is the default for web tiers, and most of this tutorial uses it.
- If you need a fixed IP address per AZ, non-HTTP protocols such as raw TCP or UDP, or extreme connection rates, choose the NLB.
- If traffic must pass through a third-party security appliance before it reaches the application, choose the GWLB.
Exercise
A subnet-aaaa1111 is in one AZ. Why does a highly available load balancer need subnets like it in a second AZ, and which load balancer type would you choose for a public REST API that routes by URL path?
Show answer
A subnet exists in only one AZ, so a single subnet leaves the load balancer exposed to that AZ failing. Subnets in two AZs give the load balancer a node in each. For a REST API routed by URL path, choose an Application Load Balancer, because it works at layer 7 and supports path-based rules.