AWS Certified CloudOps Engineer – Associate
NAT gateway vs internet gateway: what each needs to work
The Amazon VPC User Guide says an internet gateway gives two-way internet access to instances that have a public IPv4 address in a subnet routed to it. A NAT gateway lets instances in a private subnet start connections out while nothing outside can connect in. A public NAT gateway sits in a public subnet with an Elastic IP address.
What makes a subnet public?
A subnet is public when its route table has a route to an internet gateway, for example 0.0.0.0/0. Without that route the subnet is private, and its instances cannot reach the internet even if they have public IP addresses.
The route is not enough. For IPv4, the instance must also have a public IPv4 address or an Elastic IP address. The internet gateway does one-to-one NAT between that address and the instance's private address.
A default VPC comes with an internet gateway, the route, and automatic public IPv4 addresses in its default subnets. A nondefault VPC has none of these, and its subnets do not assign public IPv4 addresses automatically.
How do you set up a NAT gateway?
A public NAT gateway is the default type. You create it in a public subnet and must associate an Elastic IP address with it at creation. The private subnet's route table sends internet traffic to the NAT gateway, and the NAT gateway's own subnet routes to the internet gateway.
A private NAT gateway connects private subnets to other VPCs or an on-premises network through a transit gateway or virtual private gateway. It cannot have an Elastic IP address, and if you route its traffic to an internet gateway, the internet gateway drops it.
Each NAT gateway lives in one Availability Zone. If several zones share one and its zone goes down, the others lose internet access, so AWS advises one NAT gateway per zone with routing that keeps each zone on its own. You cannot attach a security group to a NAT gateway. It supports TCP, UDP and ICMP.
Why do connections through a NAT gateway fail?
When instances cannot reach the internet, check that the NAT gateway is in the Available state, that it is in a public subnet whose route table points to an internet gateway, that the private subnet routes to the NAT gateway, and that no security group or network ACL blocks the traffic.
Each IPv4 address on a NAT gateway supports up to 55,000 simultaneous connections to one unique destination, meaning one combination of destination address, port and protocol. If no more connections can be opened, add secondary IPv4 addresses (up to 8 addresses in total), add NAT gateways, or close idle connections.
A connection that is idle for 350 seconds or more times out, and the NAT gateway answers later packets with an RST. Send traffic more often or enable TCP keepalive with a value under 350 seconds.
Where the exam catches people
- Putting the NAT gateway in the private subnet it serves: a public NAT gateway must be in a subnet that routes to the internet gateway.
- Believing a route to the internet gateway is enough: the instance also needs a public IPv4 or Elastic IP address.
- Using a private NAT gateway for internet access: the internet gateway drops traffic routed from a private NAT gateway.
- Assuming subnets in a nondefault VPC assign public IPv4 addresses: they do not by default.
- Adding bandwidth when new connections to one destination fail: the limit is 55,000 connections per IPv4 address per destination, so add addresses.