Sub topic 7
Private IPs and keeping traffic in one AZ
This chapter assumes you know what a VPC, subnet, and Availability Zone (AZ) are. It also assumes you can read basic route tables and use the AWS CLI.
The rule
Within one Region, AWS charges for data transfer based on the address you use and the AZ the traffic crosses.
- Traffic between two resources in the same AZ over private IPs is free.
- Traffic between AZs in the same Region costs about $0.01/GB in each direction, so $0.02/GB for a round trip.
- Traffic sent to a public or Elastic IP is billed as regional transfer, even when both instances sit in the same AZ.
The third rule catches teams most often. The traffic never leaves the building, but the address you chose decides the price.
Why traffic ends up on public IPs
Most cases come from a few causes:
- A configuration file or service registry holds an Elastic IP instead of a private address.
- A custom DNS name points at an Elastic IP. Inside a VPC, the default public DNS name of an instance resolves to its private IP when DNS hostnames and DNS resolution are enabled. Your own Route 53 record does not.
- Services call each other through a public load balancer when an internal one would do.
- Peered VPCs in the same Region talk over public addresses because nobody updated the target.
To fix this, point internal clients at private DNS names or private IPs. Create a private hosted zone in Route 53 if you need friendly names.
Check your AZ placement
Subnets belong to one AZ, so subnet placement controls where traffic can stay local. AZ names such as us-east-1a map to different physical zones in different accounts. When you compare placement across accounts, use the AZ ID.
aws ec2 describe-subnets \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 \
--query "Subnets[].[SubnetId,AvailabilityZone,AvailabilityZoneId,CidrBlock]" \
--output tableTwo accounts that both show us-east-1a may be in different physical zones. Traffic between them is then inter-AZ even though the names match.
Keep a request in one AZ
Free same-AZ traffic only helps if requests stay there. Common techniques:
- Run a full stack per AZ. Each AZ has its own web, app, and cache tier, and clients are routed to the local copy.
- Disable cross-zone load balancing where you can. A Network Load Balancer has it off by default, so each node sends traffic only to targets in its own AZ. An Application Load Balancer always has it on, but you can turn it off per target group. The trade-off is uneven load if AZs hold different numbers of targets.
- Prefer AZ-aware clients. Some databases and caches can read from a replica in the caller's AZ. Check your client library for a "local replica" or "rack-aware" option.
Find the leak
Run the Flow Logs query from the earlier chapter, then check whether each address pair is public or private. A pair of private addresses in different subnets is probably inter-AZ. A pair that includes a public address from your own account points to the problem in this chapter. In Cost Explorer, the usage types DataTransfer-Regional-Bytes (inter-AZ and public-IP regional traffic) and *-DataTransfer-Regional-Bytes are the ones to watch.
Exercise
Two EC2 instances sit in the same AZ and the same VPC. A client reaches the server through the server's Elastic IP. Is the traffic free, and what is the fix?
Show answer
No. Traffic sent to a public or Elastic IP is billed as regional data transfer even within one AZ. The fix is to have the client use the server's private IP or a private DNS name, which makes the same-AZ traffic free.
Exercise
Why compare AZ IDs instead of AZ names when checking placement across two accounts?
Show answer
AZ names are mapped to physical zones separately in each account, so us-east-1a may be a different zone in each. AZ IDs such as use1-az1 refer to the same physical zone in every account.