Sub topic 6
Private subnets and NAT gateways
Prerequisites
This chapter assumes you have read the earlier chapters on subnets, route tables, and the internet gateway. You should know that a subnet is public only because its route table has a default route to an internet gateway. You should also be able to run AWS CLI commands and read CIDR notation. The commands reuse the shell variables VPC_ID, PUBLIC_SUBNET_ID, and PRIVATE_SUBNET_ID from the earlier chapters.
Why private subnets need a NAT gateway
A private subnet has no route to an internet gateway, so nothing on the internet can start a connection to its instances. That is the goal. But private instances often still need outbound access, for example to install OS patches, call a third-party API, or pull a container image.
A NAT gateway solves this. NAT stands for network address translation. The gateway sits in a public subnet and has an Elastic IP address, which is a static public IPv4 address that you own until you release it. When a private instance sends traffic to the internet, the NAT gateway replaces the instance's private source address with its own Elastic IP and forwards the packet through the internet gateway. When the reply arrives, the gateway translates it back and delivers it to the instance.
Because the gateway tracks connections that originate inside the VPC, a host on the internet cannot open a new connection to a private instance. Only replies to outbound requests get through.
The traffic path
The diagram shows the path of one outbound request and its reply.
Two routes make this work:
- The private route table sends
0.0.0.0/0to the NAT gateway. - The route table of the NAT gateway's own subnet sends
0.0.0.0/0to the internet gateway.
If the second route is missing, the NAT gateway has no way out. That is why the gateway must live in a public subnet.
Creating a NAT gateway
The following commands allocate an Elastic IP, create the gateway in the public subnet, wait until it is available, and then route the private subnet through it.
EIP_ALLOC_ID=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT_ID=$(aws ec2 create-nat-gateway --subnet-id "$PUBLIC_SUBNET_ID" --allocation-id "$EIP_ALLOC_ID" --query NatGateway.NatGatewayId --output text)
aws ec2 wait nat-gateway-available --nat-gateway-ids "$NAT_ID"
PRIVATE_RT_ID=$(aws ec2 create-route-table --vpc-id "$VPC_ID" --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id "$PRIVATE_RT_ID" --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$NAT_ID"
aws ec2 associate-route-table --route-table-id "$PRIVATE_RT_ID" --subnet-id "$PRIVATE_SUBNET_ID"The wait command matters. A new NAT gateway starts in the pending state, and create-route fails if you point a route at a gateway that is not yet available.
After the last command, check the result:
aws ec2 describe-route-tables --route-table-ids "$PRIVATE_RT_ID" \
--query "RouteTables[0].Routes[].[DestinationCidrBlock,NatGatewayId,State]" \
--output tableYou should see the local route for the VPC CIDR and a 0.0.0.0/0 route to your nat- ID with state active.
Public and private NAT gateways
AWS offers two connectivity types.
| Type | Elastic IP | Placed in | Purpose |
|---|---|---|---|
| Public (default) | Required | Public subnet | Outbound internet access for private subnets |
| Private | None | Any subnet | Reach other VPCs or on-premises networks with address translation, for example to hide overlapping CIDRs |
A private NAT gateway cannot reach the internet, even if the subnet has an internet gateway route. This chapter covers the public type, which is what most designs need.
Availability, scaling, and cost
A NAT gateway lives in one Availability Zone (AZ). AWS makes it redundant inside that AZ, and it scales automatically up to 100 Gbps. It does not survive the loss of the whole AZ, though.
For resilience, create one NAT gateway per AZ and give each AZ's private subnets their own route table that points to the local gateway. This gives you two benefits:
- If one AZ fails, private subnets in other AZs keep their outbound access.
- Traffic stays inside its AZ, which avoids cross-AZ data transfer charges.
The trade-off is cost. A NAT gateway is billed for every hour it exists plus a per-GB charge for all data it processes. Both apply even when traffic is light. A single shared gateway is cheaper but creates a single-AZ dependency and cross-AZ traffic charges. A common compromise is one gateway in development environments and one per AZ in production.
Reducing NAT traffic
Because you pay per gigabyte, avoid sending traffic through the gateway that does not need to go there.
- Add a gateway endpoint for S3 and DynamoDB. Gateway endpoints are free and keep that traffic off the NAT gateway. A later chapter covers them.
- Use interface endpoints for other AWS services that you call heavily, such as ECR or CloudWatch Logs. They carry their own hourly and data charges, so compare costs.
- Cache OS packages and container images in a registry or repository inside the VPC.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Private instance cannot reach the internet | Private route table has no `0.0.0.0/0` route to the NAT gateway, or the subnet is not associated with that table | Check the route and the association |
| Route shows `blackhole` | The NAT gateway was deleted and the route still points to it | Replace the route with a new gateway |
| NAT gateway is available, but traffic still fails | The NAT gateway's subnet has no route to the internet gateway | Place it in a subnet whose route table has `0.0.0.0/0` to the internet gateway |
| Traffic works from one AZ only | Several AZs share a route table that points at a gateway in one AZ, and that AZ is impaired | Use one route table and gateway per AZ |
| Connection times out to a specific destination | A security group egress rule or a network ACL blocks the traffic, including the ephemeral return ports | Review both layers |
| `create-route` fails right after `create-nat-gateway` | The gateway is still `pending` | Run `aws ec2 wait nat-gateway-available` first |
A few points are easy to miss:
- IPv4 only. A NAT gateway translates IPv4. For IPv6, an egress-only internet gateway gives the same outbound-only behavior. A later chapter covers it.
- Port limits. One gateway can hold about 55,000 simultaneous connections to a single destination IP, port, and protocol. Very chatty workloads that hit one endpoint may exhaust this, which shows up as
ErrorPortAllocationin CloudWatch metrics. - Releasing the address. Deleting a NAT gateway does not release its Elastic IP. Release it yourself, or you keep paying for the idle address.
- Not a security control. NAT gateways have no security groups. You filter traffic with the security groups of your instances and the network ACLs of your subnets.
Cleaning up
Remove resources in this order, because a route cannot point to a deleted gateway and an address cannot be released while in use:
aws ec2 delete-nat-gateway --nat-gateway-id "$NAT_ID"
aws ec2 wait nat-gateway-deleted --nat-gateway-ids "$NAT_ID"
aws ec2 release-address --allocation-id "$EIP_ALLOC_ID"Design checklist
- Put the NAT gateway in a public subnet, and confirm that subnet routes to the internet gateway.
- Give each private subnet a route table with
0.0.0.0/0pointing to a NAT gateway. - Use one gateway per AZ for production workloads.
- Use gateway endpoints for S3 and DynamoDB to cut data processing costs.
- Do not rely on NAT as a firewall. Combine it with security groups and network ACLs.
Exercise
Your private instances in us-east-1a can reach the internet, but instances in us-east-1b cannot. Both private subnets are associated with the same route table, whose default route points to a NAT gateway in a public subnet in us-east-1a. The us-east-1a Availability Zone has just become impaired. What design change fixes this, and what cost effect does it have?
Show answer
Create a second NAT gateway in a public subnet in us-east-1b, and give each AZ's private subnets their own route table whose 0.0.0.0/0 route points to the NAT gateway in the same AZ. Then us-east-1b no longer depends on us-east-1a. The cost is one more hourly gateway charge and its own Elastic IP, but you avoid cross-AZ data transfer charges, because traffic stays inside each AZ.
Exercise
A NAT gateway shows state "available" and the private route table has an active 0.0.0.0/0 route to it, but private instances still cannot reach the internet. Name the first thing to check about the NAT gateway's own subnet.
Show answer
Check that the route table associated with the NAT gateway's subnet has a 0.0.0.0/0 route to an internet gateway. Without it, the gateway has no path out, because the subnet is not actually public.