Sub topic 7
Private IPs and keeping traffic in one AZ
This chapter assumes you know what Availability Zones (AZs), VPCs, and subnets are, and that you can read an EC2 network configuration. Earlier chapters covered the charge categories and how to find transfer spend in Cost Explorer. Here we look at one of the least visible charges: traffic between two instances in the same Region that is billed even though it never leaves AWS.
Prices below are the us-east-1 list prices at the time of writing. Check the EC2 pricing page for your Region.
The rule: the address you use decides the price
Two things decide the charge for traffic between two resources in the same Region:
- Whether the traffic crosses an AZ boundary.
- Whether the sender addresses the receiver by its private IP or by a public or Elastic IP.
| Source and destination | Address used | Charge |
|---|---|---|
| Same AZ | Private IPv4 | Free |
| Same AZ | Public or Elastic IPv4 | $0.01/GB in each direction |
| Different AZ, same Region | Private IPv4 | $0.01/GB in each direction |
| Different AZ, same Region | Public or Elastic IPv4 | $0.01/GB in each direction |
"Each direction" means that a gigabyte sent from AZ A to AZ B is charged on the way out of A and again on the way into B. The effective cost of a round trip is therefore about $0.02 per GB moved one way.
The second row is the surprise. Two instances that share a subnet pay for traffic if one of them calls the other through its Elastic IP. AWS treats a public address as traffic that leaves the private network path, even when the packets stay inside the Region.
How public addresses sneak into internal traffic
Teams rarely choose public IPs on purpose for internal calls. They arrive through a few common paths.
Custom DNS names that point at Elastic IPs
Suppose db.example.com has an A record in Route 53 that points to an Elastic IP. A client in the same VPC resolves the name, gets the public address, and sends traffic out through the internet gateway path and back in. You pay regional transfer charges on that traffic. You may also pay for the public IPv4 address itself.
The fix is to give internal consumers an internal name. Use a private hosted zone or a CNAME to the instance's private DNS name, so the answer is a private IP.
EC2 public DNS names resolve privately, but only if DNS is enabled
The default public DNS name of an instance, such as ec2-203-0-113-25.compute-1.amazonaws.com, resolves to the instance's private IP when queried from inside the same VPC. This only works when enableDnsSupport and enableDnsHostnames are both true on the VPC. If either one is off, that name resolves to the public IP and you pay for the traffic.
Check what a name resolves to from an instance in the VPC:
dig +short ec2-203-0-113-25.compute-1.amazonaws.com
dig +short db.example.comIf the first command returns a 10.x, 172.16-31.x, or 192.168.x address and the second returns a public one, you have found a configuration problem.
Hard-coded addresses in configuration
Old config files, service registries, and deploy scripts sometimes carry an Elastic IP that was used when the service was reachable from the internet. Search your configuration for public addresses that belong to your own account. Even one chatty service using such an address can generate a visible line item.
Traffic via a NAT gateway or load balancer
Calling your own public load balancer DNS name from inside the VPC still resolves to the load balancer's public addresses. The traffic goes through the internet gateway, not the NAT gateway, and is billed as regional transfer on top of load balancer processing charges. For service-to-service calls, prefer an internal load balancer.
Finding the public-address traffic in Flow Logs
In the default Flow Logs format, srcaddr and dstaddr show the private address of the network interface, even when the sender used a public address. That hides the problem. A custom format adds pkt-srcaddr and pkt-dstaddr, which hold the addresses in the packet itself, plus az-id.
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids vpc-0123456789abcdef0 \
--traffic-type ALL \
--log-destination-type cloud-watch-logs \
--log-group-name /vpc/flow-logs/custom \
--deliver-logs-permission-arn arn:aws:iam::111122223333:role/flow-logs-role \
--log-format '${srcaddr} ${dstaddr} ${pkt-srcaddr} ${pkt-dstaddr} ${az-id} ${bytes}'Once records arrive, look for rows where pkt-dstaddr is a public address that belongs to one of your own Elastic IPs while dstaddr is a private address in your VPC. Those are internal calls using a public path. Sort them by bytes to decide which to fix first.
Same-AZ is about subnets and placement
Every subnet lives in exactly one AZ. An instance's AZ is the AZ of its subnet, so you control placement by choosing subnets.
Check which AZ each subnet is in. Use the AZ ID as well as the name:
aws ec2 describe-subnets \
--query "Subnets[].[SubnetId,AvailabilityZone,AvailabilityZoneId,CidrBlock]" \
--output tableAZ names like us-east-1a are mapped to physical zones separately for each account. Your us-east-1a may be a different physical zone from another account's us-east-1a. The AZ ID, such as use1-az1, is the same everywhere. When you compare placement across accounts, for example a producer in one account and a consumer in another, compare AZ IDs. Two "us-east-1a" subnets in different accounts can still be in different zones and incur inter-AZ charges.
Load balancers and cross-zone behavior
Load balancers add their own wrinkle, because they can send a request to a target in another AZ.
- An Application Load Balancer has cross-zone load balancing on by default. AWS does not charge for the data it moves between AZs from the load balancer to targets. The client-to-load-balancer leg is still ordinary traffic, and so is any traffic between your own tiers.
- A Network Load Balancer has cross-zone load balancing off by default. If you turn it on, data that crosses AZs is billed as inter-AZ transfer.
Cross-zone balancing also affects the spread of load. If one AZ has fewer targets and cross-zone is off, those targets receive a disproportionate share. Check target counts per AZ before you change the setting.
Within your own tiers, a request from a web server to an application server can go to any AZ if you call a service name that resolves to all of them. Options that keep traffic local are zone-aware routing in your service mesh or client (for example, Envoy locality-weighted routing or Kubernetes topology-aware routing) and per-AZ endpoints.
Availability is the limit
Putting everything in one AZ is cheap and fragile. If that AZ fails, you lose the whole system. The goal is to keep each request's path inside one AZ, not to run in only one AZ.
- Keep running in at least two AZs for anything that must stay up.
- Prefer designs where each AZ has a full stack of the stateless tiers, so a request entering an AZ is served there.
- Accept inter-AZ charges where they buy resilience, such as database replication to a standby. Synchronous replication across AZs is a fixed, deliberate cost.
- Make failover work. Locality-preferring routing must fall back to other AZs when local targets are unhealthy, instead of failing requests.
A practical order of work
- Look for the
DataTransfer-Regional-Bytesusage type in Cost Explorer. It covers both inter-AZ traffic and traffic using public IPs inside the Region. - Turn on custom Flow Logs with
pkt-srcaddr,pkt-dstaddr, andaz-idfor the VPCs with the highest charge. - Separate flows into two groups: private IP across AZs, and public or Elastic IP within the Region.
- Fix the second group first. It needs only a DNS or configuration change, with no architecture change and no resilience loss.
- For the first group, apply zone-aware routing, review cross-zone load balancer settings, and move chatty pairs, such as an application and its cache, so they run in the same AZ.
- Re-check the usage type a week after each change.
Exercise
Two instances sit in the same subnet. Instance A calls instance B using B's Elastic IP, and the transfer shows up as a charge. Why, and what are two ways to remove it?
Show answer
Same-AZ traffic is free only when it uses private IPs. Using the Elastic IP makes AWS bill it as regional transfer at $0.01/GB in each direction, even in one subnet. Fixes: point A at B's private IP or private DNS name, for example through a Route 53 private hosted zone record; or use the instance's default EC2 DNS name, which resolves to the private IP inside the VPC when enableDnsSupport and enableDnsHostnames are on.
Exercise
Why compare AZ IDs, not AZ names, when you check whether a producer in one account and a consumer in another account share a zone?
Show answer
AZ names are mapped to physical zones separately for each account, so us-east-1a in two accounts can be different zones. AZ IDs such as use1-az1 refer to the same physical zone in every account, so they show whether traffic really stays in one AZ.