Sub topic 6
Private subnets and NAT gateways
This chapter assumes you know CIDR notation, have read the earlier chapters on subnets, route tables, and internet gateways, and can run AWS CLI commands. The shell variables from those chapters (VPC_ID, PUBLIC_SUBNET_ID, PRIVATE_SUBNET_ID) are reused here.
What makes a subnet private
A private subnet is a subnet whose route table has no route to an internet gateway. Instances in it have only private IP addresses that the internet cannot route to, so nothing outside can start a connection to them.
Private instances often still need outbound access. They may install OS patches, call a third-party API, or pull container images. A NAT gateway provides this. NAT stands for network address translation. The gateway sits in a public subnet and has an Elastic IP, which is a static public IPv4 address. It rewrites the source address of outbound packets to that Elastic IP and maps the replies back to the original instance. It allows connections that start inside the VPC and drops connections that start outside.
Traffic flow
The NAT gateway lives in the public subnet because it needs a route to the internet gateway of its own. The private subnet's default route points at the NAT gateway, not at the internet gateway.
Create the NAT gateway and route
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 steps work as follows:
- Allocate an Elastic IP. The NAT gateway needs it as its public address.
- Create the NAT gateway in the public subnet. The
waitcommand blocks until its state isavailable, which usually takes a minute or two. - Create a separate route table for the private subnet and add a default route to the NAT gateway.
- Associate that route table with the private subnet.
Common errors
- Placing the NAT gateway in a private subnet. It then has no path to the internet gateway, and outbound traffic fails silently.
- Forgetting the route. The NAT gateway exists, but the private subnet still uses the VPC's main route table, which only has the local route.
- Using one NAT gateway for several Availability Zones. A NAT gateway is tied to one zone. If that zone fails, every private subnet routed through it loses outbound access. Cross-zone traffic is also billed as data transfer. For production, create one NAT gateway per zone, each with its own private route table.
- Ignoring cost. You pay an hourly charge for each NAT gateway plus a charge per gigabyte processed. Traffic to S3 or DynamoDB through NAT adds up quickly. Gateway endpoints, covered in a later chapter, avoid that charge.
- Expecting inbound access. A NAT gateway does not let the internet reach private instances. Use a load balancer in the public subnet, or a managed access method such as Systems Manager Session Manager, for that.
Verify
From an instance in the private subnet, run curl -s https://checkip.amazonaws.com. The address it prints should match the Elastic IP you allocated. If the command hangs, check the private route table first, then the NAT gateway's subnet and route table, then the security group's outbound rules.
Exercise
Your private instances can't reach the internet. The NAT gateway is in the available state and the private route table has 0.0.0.0/0 pointing to it. The NAT gateway sits in a subnet associated with a route table that has only the local route. What is wrong, and how do you fix it?
Show answer
The NAT gateway's subnet is not public, because its route table has no default route to an internet gateway. The gateway can't forward traffic out. Add a 0.0.0.0/0 route to the internet gateway in that subnet's route table, or move the NAT gateway to a real public subnet.
Exercise
Why would a production VPC with three Availability Zones usually have three NAT gateways instead of one?
Show answer
A NAT gateway exists in a single zone. With one gateway, a failure in that zone cuts off outbound access for the private subnets in the other zones, and their traffic also pays cross-zone data transfer charges. One gateway per zone, each used by its own zone's private route table, removes both problems.