Certopolis

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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A new IPv4-only VPC has an internet gateway, a public subnet whose route table sends 0.0.0.0/0 to it, and private subnets in the same Availability Zone. Instances in the private subnets have only private IPv4 addresses and must download operating system patches from repositories on the internet. Hosts on the internet must not be able to open connections to them. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: A is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A public NAT gateway does both. It is created in the public subnet, where its own traffic reaches the internet gateway, and it must be given an Elastic IP address when it is created. The private subnets then send 0.0.0.0/0 to it.

    Written from docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html

  2. Instances in a VPC reach an on-premises database at 172.16.8.20 on TCP 1433 through a private NAT gateway and a transit gateway, because the VPC's CIDR block overlaps the data center's. The NAT gateway was created with only its primary private IPv4 address. At month-end, new connections to the database fail while connections to other on-premises servers keep working, and ErrorPortAllocation rises above zero. Which action should the SysOps administrator take to resolve this issue?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: B is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The ceiling is about 55,000 simultaneous connections from each IPv4 address to each unique destination, and all of these connections go to the same address, port and protocol, which is why other servers are unaffected. A private NAT gateway translates to private addresses, so the fix is to assign it secondary private IPv4 addresses; each one adds another 55,000 connections to the database.

    Written from docs.aws.amazon.com/vpc/latest/userguide/nat-gateway-basics.html

  3. A SysOps administrator launches an EC2 instance into a subnet of a nondefault VPC that has no IPv6 CIDR block. The subnet's route table sends 0.0.0.0/0 to the VPC's internet gateway, the security group allows all outbound traffic, and the network ACL allows all traffic. The instance received only a private IPv4 address, and it cannot download packages from the internet. Which action should the SysOps administrator take to fix this issue?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: C is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A subnet in a nondefault VPC does not hand out public addresses automatically, which is why this instance has none. Associating an Elastic IP address gives the internet gateway an address to translate to, and the downloads work.

    Written from docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html

Ask for something