Learning center

Connections fail through a NAT gateway that has run out of ports

When new outbound connections fail while a NAT gateway carries heavy traffic to one destination, read its port allocation metrics first. On AWS a nonzero ErrorPortAllocation sum means the gateway could not allocate a source port; on Google Cloud, dropped_sent_packets_count with reason OUT_OF_RESOURCES means Cloud NAT ran out of addresses or ports. Both limits count connections to one destination address, port and protocol, so a single busy dependency can exhaust them while everything else works.

The symptom and its nearby causes#

Services in private subnets reach public APIs, partner endpoints and managed services with public addresses through a NAT gateway. When the gateway cannot translate a new connection, the client sees the connection fail to open, usually as a connect timeout. Existing connections keep working. Three other causes produce the same log line:

  • The destination is slow or dropping SYNs itself. Every client fails, including ones with their own public address.
  • The client host ran out of local ephemeral ports. The error is immediate and local: EADDRNOTAVAIL, which many runtimes print as Cannot assign requested address. Ephemeral ports on the client
  • A security group, firewall rule or route drops the SYN. That fails every new connection, at any load. Refused or timed out

The NAT case has a shape the others lack: it depends on how many connections are open to one destination at the same time, so it appears at peak traffic, often to one partner API, and clears when load falls.

The limits that apply#

An AWS NAT gateway supports up to 55,000 simultaneous connections per IPv4 address to each unique destination, where a destination is a combination of destination IP address, destination port and protocol. You can associate up to 8 IPv4 addresses with a NAT gateway (1 primary and 7 secondary), which raises the ceiling per destination accordingly. Connections to different destinations do not share this budget. NAT gateway basics · Cannot initiate more connections

Cloud NAT reserves a number of NAT source address and port pairs for each VM, and that number bounds the connections the VM can make to one destination 3-tuple. Public NAT uses static port allocation by default with a minimum of 64 ports per VM; dynamic port allocation defaults to a minimum of 32 and grows up to a configured maximum. Cloud NAT also holds a closed connection's mapping for its own TCP TIME_WAIT timeout (30 or 120 seconds by default, depending on when the gateway was created), so connection churn uses ports even when few connections are open at once. Cloud NAT ports and addresses

The two differ in scope. On AWS the budget belongs to the gateway and is shared by every client behind it. On Cloud NAT each VM has its own allocation, so one busy VM can fail while its neighbours behind the same gateway do not.

Read the AWS metrics#

Find the NAT gateway the client subnet routes through, then read its addresses and its metrics in namespace AWS/NATGateway. ErrorPortAllocation counts the times the gateway could not allocate a source port; its useful statistic is Sum. ActiveConnectionCount is concurrent active TCP connections; use Max. PacketsDropCount counts dropped packets. A ConnectionEstablishedCount below ConnectionAttemptCount means clients tried connections that got no response. The metrics arrive at 1-minute intervals. NAT gateway metrics · NAT gateways in CloudWatch · describe-nat-gateways · get-metric-statistics

aws ec2 describe-route-tables \
  --profile PROFILE --region REGION \
  --filters Name=association.subnet-id,Values=CLIENT_SUBNET_ID \
  --query 'RouteTables[].Routes[].{to:DestinationCidrBlock,nat:NatGatewayId}' \
  --output table --no-cli-pager

aws ec2 describe-nat-gateways \
  --profile PROFILE --region REGION --nat-gateway-ids NAT_GATEWAY_ID \
  --query 'NatGateways[0].NatGatewayAddresses[].{public:PublicIp,private:PrivateIp,primary:IsPrimary}' \
  --output table --no-cli-pager

aws cloudwatch get-metric-statistics \
  --profile PROFILE --region REGION \
  --namespace AWS/NATGateway --metric-name ErrorPortAllocation \
  --dimensions Name=NatGatewayId,Value=NAT_GATEWAY_ID \
  --start-time START --end-time END --period 60 --statistics Sum \
  --output table --no-cli-pager

Repeat the last command with ActiveConnectionCount and --statistics Maximum, and with PacketsDropCount. AWS treats a PacketsDropCount above 0.01 percent of PacketsInFromSource plus PacketsInFromDestination as a possible service issue, which is a different problem from port allocation. These metrics are per gateway and do not name the destination. To find which destination is busy, count connections by remote address on the clients (ss -tn inside a client) or read VPC Flow Logs for the NAT gateway's interface, if they are enabled.

Read the Cloud NAT metrics and logs#

Cloud NAT publishes per-VM metrics under compute.googleapis.com/ and per-gateway metrics under router.googleapis.com/. nat/dropped_sent_packets_count has a reason label: OUT_OF_RESOURCES when Cloud NAT runs out of NAT IP addresses or ports, ENDPOINT_INDEPENDENCE_CONFLICT for an endpoint-independent mapping conflict. The per-VM nat/port_usage is the highest number of connections from that VM to a single endpoint, which you can compare with nat/allocated_ports. nat/nat_allocation_failed reports a failure to allocate NAT IPs to any VM. Cloud NAT monitoring

Read the gateway's configuration, then the drop log if NAT logging is enabled. A log entry with allocation_status of DROPPED is written when a packet is dropped because no port was available, and it names the VM and the destination. gcloud compute routers nats describe · Router NAT fields · gcloud logging read

gcloud compute routers nats describe NAT_NAME \
  --router ROUTER --region REGION --project PROJECT \
  --format 'yaml(natIps,minPortsPerVm,maxPortsPerVm,enableDynamicPortAllocation,enableEndpointIndependentMapping,tcpTimeWaitTimeoutSec)'

gcloud logging read \
  'logName="projects/PROJECT/logs/compute.googleapis.com%2Fnat_flows" AND jsonPayload.allocation_status="DROPPED"' \
  --project PROJECT --freshness 1h --limit 20

What each reading means#

ObservationPoints to
ErrorPortAllocation above zero at the failure timesAWS NAT ports to one destination
OUT_OF_RESOURCES drops for the failing VMsCloud NAT allocation for those VMs
No NAT errors, client logs EADDRNOTAVAILClient ephemeral ports
No NAT errors, every client failsDestination or path rules
Packet drops without port errorsNot port allocation; check the provider's health

A worked fixture: two peaks, two causes#

This fixture is illustrative. The fictional service payments runs in account 111122223333 behind one NAT gateway with one public address and calls a card processor at 203.0.113.40:443. At 18:00 UTC on a sale day, payments logs connect timeouts to the processor while its calls to orders.example.internal succeed.

In the first case, ErrorPortAllocation sums 0 until 17:58 and then 1,200 to 4,000 per minute until 18:40. ActiveConnectionCount Max sits near 55,000. Client-side counts show nearly all of them going to 203.0.113.40:443: the payments HTTP client opens a new connection for each request and closes none of them before a 30-minute idle timeout. The gateway's budget for that one destination is full. Adding secondary addresses raises it, and reusing fewer, longer-lived connections lowers demand.

In the second case, ErrorPortAllocation stays at 0 throughout and ActiveConnectionCount peaks at 6,000. The payments tasks log the error immediately, with no wait, and ss -tan state time-wait inside a task lists about 28,000 sockets. The client host ran out of ephemeral ports before the gateway noticed anything; the client-side guide covers that reading.

Limits#

The AWS metrics are per gateway and per minute. They show that ports ran out, not which client or destination used them. CloudWatch does not split them by destination, so pair them with client socket counts or flow logs. Cloud NAT drop logs exist only when logging is enabled, and each VM can generate a limited number of log entries per second, so logs undercount during a large drop. Cloud NAT logging

The 350-second idle timeout on AWS NAT gateways is a separate failure, which shows as resets on idle pooled connections and in IdleTimeoutCount. Idle connection resets · All runtime dependency guides

Questions#

Does adding a second destination address help?

Only for traffic to that destination. The AWS limit counts connections per destination IP, port and protocol, so a partner that publishes several addresses spreads load across separate budgets. Adding secondary addresses to the NAT gateway raises the budget for every destination.

Why does only one VM fail behind Cloud NAT?

Cloud NAT allocates ports per VM. The VM that opens the most connections to one destination exhausts its own allocation first, while other VMs behind the same gateway still have ports. Compare nat/port_usage with nat/allocated_ports for that VM.

Do port allocation errors mean the NAT gateway is unhealthy?

No. They mean the clients asked for more simultaneous connections to one destination than the gateway's addresses can carry. Packet drops without port allocation errors are the signal AWS ties to a possible service issue.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading