Sub topic 5
Zonal NAT gateways and egress routing
This chapter assumes you have already built a VPC with a public and a private subnet in each of at least two Availability Zones (AZs). It also assumes you know what a route table is and how an internet gateway works. The previous chapter covered the subnet layout. This one covers how instances in private subnets reach the internet without becoming a hidden single point of failure.
Why egress is a failure domain
Instances in private subnets often need outbound access. They pull OS patches, call third-party APIs, or reach AWS services that have no private endpoint. A NAT gateway provides that access. It translates the private source address to its own public address, and it only allows connections that were started from inside the VPC.
A NAT gateway is a zonal resource. You create it in one public subnet, so it runs in one AZ. AWS keeps it redundant inside that AZ, but if the AZ is impaired, the gateway is impaired with it.
The common mistake is to build a two-AZ application tier and put one NAT gateway in AZ A. Every private route table points to it. The design looks highly available, but a failure in AZ A removes outbound access for the instances in AZ B as well, even though AZ B is healthy. The NAT gateway is a single point of failure that spans the whole VPC.
The pattern: one NAT gateway per AZ, one route table per AZ
The fix has three parts:
- Create a NAT gateway in the public subnet of every AZ that has private workloads.
- Create a separate private route table for each AZ.
- In each route table, point the default route (
0.0.0.0/0) at the NAT gateway in the same AZ.
The internet gateway is a regional, highly available VPC component, so it does not need duplicating. Only the NAT gateways need to be per-AZ.
A CloudFormation template
The template below creates the Elastic IPs, the NAT gateways, and the per-AZ route tables for two AZs. It takes the VPC, subnets, and internet-gateway-routed public subnets as parameters. The public subnets must already have a route to the internet gateway.
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
PublicSubnetA:
Type: AWS::EC2::Subnet::Id
PublicSubnetB:
Type: AWS::EC2::Subnet::Id
PrivateSubnetA:
Type: AWS::EC2::Subnet::Id
PrivateSubnetB:
Type: AWS::EC2::Subnet::Id
Resources:
EipA:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
EipB:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatA:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EipA.AllocationId
SubnetId: !Ref PublicSubnetA
NatB:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EipB.AllocationId
SubnetId: !Ref PublicSubnetB
PrivateRouteTableA:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VpcId
PrivateRouteTableB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VpcId
DefaultRouteA:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTableA
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatA
DefaultRouteB:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTableB
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatB
AssocA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetA
RouteTableId: !Ref PrivateRouteTableA
AssocB:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetB
RouteTableId: !Ref PrivateRouteTableBAfter deployment, confirm that each gateway sits in a different AZ. NAT gateways report a subnet, not a zone, so compare subnet IDs against your subnet list:
aws ec2 describe-nat-gateways \
--filter Name=state,Values=available \
--query 'NatGateways[].[NatGatewayId,SubnetId,NatGatewayAddresses[0].PublicIp]' \
--output tableWhy per-AZ route tables matter for static stability
Suppose you keep one NAT gateway and, during an AZ A outage, edit the route table to point at a new NAT gateway in AZ B. That recovery needs a control-plane action at the worst moment: someone or something must notice the failure, then call the EC2 API. If that call is slow, throttled, or blocked, recovery stalls.
With one NAT per AZ and one route table per AZ, nothing changes during a failure. AZ B's route already points to AZ B's NAT gateway. This is static stability, which a later chapter covers in full. The healthy AZ keeps working because it never depended on the failed one.
Cost: what you pay and where it surprises you
NAT gateways have two charges: an hourly charge for each gateway, and a per-GB charge for data processed. Prices vary by Region, so check the current pricing page. Running one gateway per AZ multiplies the hourly cost by the AZ count. Three AZs cost three times as much per hour as one.
Cross-AZ routing adds a second cost. If an instance in AZ B sends traffic through a NAT gateway in AZ A, you pay the NAT data-processing charge and also cross-AZ data transfer. Per-AZ gateways remove the cross-AZ transfer for egress. For traffic-heavy workloads, this saving can offset much of the extra hourly cost.
| Design | Survives loss of one AZ | Cross-AZ egress traffic | Relative hourly cost |
|---|---|---|---|
| One shared NAT gateway | No, other AZs lose egress | Yes, from every other AZ | Lowest |
| One NAT gateway per AZ | Yes, only the failed AZ loses egress | No | Highest |
| Gateway endpoints and interface endpoints for AWS services | Yes, if interface endpoints span AZs | No for gateway endpoints | Varies |
Reduce what goes through NAT
The cheapest and most resilient egress path is one that does not need NAT.
- Gateway endpoints for S3 and DynamoDB. They add a route-table entry, have no hourly or per-GB charge, and are regional, so they do not tie traffic to one AZ.
- Interface endpoints (AWS PrivateLink) for services such as ECR, CloudWatch Logs, STS, and Secrets Manager. Create endpoint network interfaces in each AZ you use. Each one has an hourly charge, so compare it with your NAT data volume.
- Egress-only internet gateway for IPv6. It allows outbound-only IPv6 traffic, needs no NAT, and is regional.
Container image pulls and log shipping are common sources of large NAT bills. Check them first.
Failure modes and limits to know
- Port exhaustion. A NAT gateway supports up to 55,000 simultaneous connections to each unique destination (IP, port, protocol). When you hit the limit, new connections fail and the
ErrorPortAllocationCloudWatch metric rises. Spreading workloads across more AZs, each with its own gateway, also spreads this load. - Bandwidth. A gateway scales automatically, starting at 5 Gbps and growing to 100 Gbps. A sudden traffic spike may see short scaling delays.
- Fixed public addresses. Each gateway has its own Elastic IP. If a partner allowlists your source addresses, give them the addresses of every gateway, or traffic from one AZ will be blocked.
- Wrong association. If a new private subnet is left on the main route table, it has no NAT route, or it inherits the wrong one. Associate every private subnet with its AZ's table explicitly.
- Deletion. Deleting a NAT gateway releases its connection state immediately. The Elastic IP stays allocated, and bills, until you release it.
Check your work
To test egress from each AZ, run a short command on one instance in each private subnet, for example through Session Manager:
curl -s --max-time 5 https://checkip.amazonaws.comThe two instances should print different addresses, each matching its own AZ's Elastic IP. If both print the same address, your route tables still share one NAT gateway.
Exercise
Your VPC has private subnets in three AZs and one NAT gateway in AZ A. All private route tables point to it. An AZ A outage begins. Which instances lose outbound internet access, and what design change removes the problem without a control-plane action during failure?
Show answer
All instances in all three AZs lose outbound access, because every private route table depends on the NAT gateway in AZ A. Create a NAT gateway in a public subnet in each AZ and give each AZ its own private route table whose 0.0.0.0/0 route targets the same-AZ gateway. Then an AZ A outage affects only AZ A's instances, and the other AZs need no route changes.
Exercise
A team replaces three per-AZ NAT gateways with one to cut cost. Name two ways the bill might not drop as expected, and one way to reduce NAT cost without losing AZ independence.
Show answer
Cross-AZ data transfer charges now apply to traffic from two AZs to the single gateway, and the NAT per-GB processing charge is unchanged because the same data still passes through. To cut cost while keeping one gateway per AZ, move traffic off NAT: add a gateway endpoint for S3 and DynamoDB, and add interface endpoints for high-volume AWS services such as ECR and CloudWatch Logs.