A NAT Gateway connects to a specific Subnet, and a Subnet is in a specific Availability Zone.
Amazon EC2 instances in private subnets can use a NAT Gateway as follows:
- The NAT Gateway is launched in a public subnet in the same VPC
- The Route Table for the private subnet(s) require an additional entry that directs all Internet-bound traffic (
0.0.0.0/0
) to the NAT Gateway
Depending upon your appetite for risk, you might configure things differently.
Case 1: One public subnet, one private subnet in same AZ
- The NAT Gateway goes into the public subnet
- The EC2 Instances go into the private subnet
- The Route Table for the private subnet points to the NAT Gateway in the public subnet
Case 2: Two public subnets, two private subnets, one NAT Gateway
- The NAT Gateway goes into one public subnet (
Public-Subnet-A
)
- The EC2 instances are launched in private subnets across two AZs (
Private-Subnet-A
, Private-Subnet-B
)
- The Route Table for both of the private subnets point to the NAT Gateway
However, if there is a failure with Availability Zone A (rare, but can happen), then the NAT Gateway is not reachable from Private-Subnet-B
. Thus, the system may be impacted even though it is running across two AZs.
Case 3: Two public subnets, two private subnets, two NAT Gateways
- The NAT Gateway goes into both public subnets (
Public-Subnet-A
, Public-Subnet-B
)
- The EC2 instances are launched in private subnets across two AZs (
Private-Subnet-A
, Private-Subnet-B
)
- The Route Table
Private-Subnet-A
points to the NAT Gateway in Public-Subnet-A
- The Route Table
Private-Subnet-B
points to the NAT Gateway in Public-Subnet-B
If one of the AZs were to fail, then the EC2 instances in the other private subnet will still be able to communicate with the Internet because they have their own NAT Gateway in the same AZ.