Sub topic 5
Zonal NAT Gateways and Egress Routing
This chapter assumes you know VPC route tables, public and private subnets, and how an internet gateway works. It also assumes you have already laid out subnets in at least two Availability Zones (AZs). Here you will see why one NAT gateway is a hidden single point of failure, and how to route egress traffic so each AZ stays independent.
Why a NAT gateway is zonal
A NAT gateway lets instances in private subnets start outbound connections, for example to download patches or call a third-party API, without accepting inbound connections from the internet. A public NAT gateway lives in one subnet, so it lives in one AZ. AWS keeps it redundant inside that AZ, but if the AZ is impaired, the gateway is impaired too.
Instances in other AZs can still reach a NAT gateway in a different AZ, because route tables work across AZs in a VPC. That is what makes the single-gateway design easy to build and easy to get wrong.
The common mistake
A single NAT gateway in AZ A, with every private route table pointing to it, has two problems:
- If AZ A fails, private subnets in AZ B and AZ C lose outbound access even though their instances are healthy.
- Traffic from AZ B and AZ C to the gateway crosses AZ boundaries, which adds cross-AZ data transfer charges and a little latency.
The pattern: one NAT gateway per AZ
Create a NAT gateway in a public subnet in each AZ. Create one private route table per AZ, and point each table's default route at the NAT gateway in the same AZ.
An AZ failure now removes only that AZ's egress path. The other AZs keep working, and the design avoids cross-AZ NAT traffic.
CloudFormation example
This fragment creates the NAT gateway and route for AZ A. Repeat it for each AZ with its own subnet, Elastic IP, and route table.
Resources:
NatEipA:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGatewayA:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatEipA.AllocationId
SubnetId: !Ref PublicSubnetA
PrivateRouteTableA:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref Vpc
PrivateDefaultRouteA:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTableA
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGatewayA
PrivateSubnetARouteAssoc:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetA
RouteTableId: !Ref PrivateRouteTableACommon errors
- One shared private route table. A single table can only hold one default route, so all AZs end up using the same NAT gateway. Use one table per AZ.
- NAT gateway in a private subnet. A public NAT gateway must sit in a subnet whose route table sends
0.0.0.0/0to an internet gateway. - Failure of the gateway's own AZ. Routes to a NAT gateway in an unhealthy AZ do not move on their own. The per-AZ design means the affected AZ's instances are the ones losing egress, and those are usually already out of service.
- Avoidable traffic. Traffic to Amazon S3 and DynamoDB can use gateway VPC endpoints. These cost nothing to use and bypass the NAT gateway, which lowers both NAT data processing charges and your dependence on NAT.
Cost trade-off
Each NAT gateway has an hourly charge plus a per-GB processing charge. Three gateways cost three times the hourly fee of one. For production workloads, this is usually worth paying. For development environments, a single gateway is a reasonable, deliberate choice if you accept the risk.
Exercise
Your VPC has private subnets in three AZs, one NAT gateway in AZ A, and one private route table shared by all subnets. AZ A becomes unavailable. What happens to outbound internet access from AZ B, and how do you fix the design?
Show answer
Instances in AZ B lose outbound internet access, because their default route points to the NAT gateway in the failed AZ. Fix it by creating a NAT gateway in a public subnet in each AZ, creating one private route table per AZ, and pointing each table's 0.0.0.0/0 route to the NAT gateway in the same AZ.