NAT gateway and egress
Give private subnets outbound-only internet access with a NAT gateway, calculate exactly what it costs, choose between zonal and regional NAT, and design NAT-free alternatives — with a C# tool that estimates your NAT bill from CloudWatch.
Assumes: AWS CLI v2 · AWS SDK for .NET v4 (AWSSDK.EC2 4.1.4, AWSSDK.CloudWatch 4.1.1.2) · .NET 10 LTS · CDK v2 (Amazon.CDK.Lib 2.277.0) · Terraform AWS provider v6 · Region us-east-1. Verified against official sources on 2026-11-18.
Learning objectives
By the end of this lesson you can:
- Explain what network address translation does and why a NAT gateway allows outbound but never unsolicited inbound connections.
- Compare public and private NAT gateways, and zonal and regional availability modes.
- Calculate a NAT gateway's monthly cost (hours, data processed, public IPv4, cross-AZ traffic) before you deploy it.
- Design NAT-free egress for AWS service traffic with VPC endpoints, and centralised egress for internet traffic.
- Deploy a NAT gateway, test it and delete it within the hour — and run a C# tool that estimates the bill from CloudWatch metrics.
Prerequisites
- Route tables and the internet gateway — default routes, blackholes and the IGW's one-to-one NAT.
- Public, private and isolated subnets — the app tier is "private" only once it has an outbound path.
- Tools: AWS CLI v2 (
learn-sandbox), .NET 10 SDK.
Why this matters in banking
Two stories, both common.
The bill. A team builds a dev environment from a popular template: three AZs, one NAT gateway per AZ "for resilience". The environment is used two days a month. Nobody deletes it. Three NAT gateways cost about USD 100 a month before a single byte flows; the container images and S3 data the apps pull through them add per-GB charges on top. Multiply by every team and every environment and NAT becomes one of the biggest line items on the networking bill — for traffic that mostly goes to AWS services and never needed the internet at all.
The breach path. A NAT gateway lets anything in a private subnet connect to anything on the internet. It does no filtering. If malware lands on an application container, the NAT gateway is the road it uses to fetch tools and send data out. Regulators and auditors increasingly ask banks "what can your workloads talk to on the internet, and how do you know?" — and "everything, through NAT" is not an acceptable answer for systems handling customer data.
This lesson teaches you to use NAT deliberately: only where internet egress is genuinely needed, filtered, logged, and with the cost understood up front.
The idea in plain language
Network address translation (NAT) rewrites the source address of outgoing packets. A NAT gateway sits in your VPC; private resources send their internet-bound traffic to it; it replaces their private source IP with its own address, keeps a table of open connections, and translates replies back. Because the translation table only contains connections started from inside, nothing on the internet can start a connection inward.
Analogy: the NAT gateway is the bank's post room. Staff (private instances) hand outgoing letters to the post room, which sends them with the bank's return address, not the employee's desk number. Replies come back to the post room, which looks up who sent the original letter and delivers the reply. A stranger cannot post a letter to "desk 4, third floor" — the outside world only knows the post room's address, and the post room only delivers replies it is expecting.
Two connectivity types
| Type | Lives in | Translates to | Used for |
|---|---|---|---|
| Public NAT gateway (default) | A public subnet (zonal mode), with an Elastic IP | Its Elastic IP, via the internet gateway | Private resources reaching the internet |
| Private NAT gateway | Any subnet; no Elastic IP | Its private IP | Reaching other VPCs or on-premises networks through Transit Gateway or a virtual private gateway — classically to solve overlapping CIDRs |
Two availability modes for public NAT
| Mode | How it works | Routes |
|---|---|---|
| Zonal (the original) | One NAT gateway in one AZ's public subnet. For resilience you create one per AZ | Each AZ's private route table points 0.0.0.0/0 at its own AZ's NAT gateway |
| Regional (newer) | One NAT gateway for the whole VPC that expands to and contracts from AZs where you have workloads. No public subnet needed; AWS creates a route table for it with a route to the IGW | Every private route table points at the single regional NAT gateway ID |
Regional mode has an automatic IP mode (AWS manages addresses and AZ expansion) and a manual mode (you assign Elastic IPs per AZ and manage expansion). It supports up to 32 IP addresses per AZ (zonal: 8), does not support private NAT, is not available in constrained AZs, and can take up to 60 minutes to expand into a newly used AZ — traffic is processed cross-zone until then. Pricing is per hour per AZ it is active in, plus per GB.
Facts and limits (checked 2026-11-18)
| Property | Value |
|---|---|
| Protocols | TCP, UDP, ICMP |
| Bandwidth | 5 Gbps, scaling automatically to 100 Gbps |
| Packets | 1 million pps, scaling to 10 million pps; beyond that, packets drop |
| Connections | Up to 55,000 simultaneous connections per IP address to each unique destination (destination IP + port + protocol) |
| IP addresses (zonal) | 1 primary + up to 7 secondary; 2 Elastic IPs per public NAT gateway by default (adjustable to 8) |
| NAT gateways per AZ | 5 (adjustable) |
| Security groups | Not supported on a NAT gateway — filter at the instances and with NACLs |
| Ports used | 1024–65535 (matters for NACLs on the NAT gateway's subnet) |
| Idle timeout | 350 seconds; after that the NAT gateway answers with a TCP RST — use keep-alives under 350 s |
| Peering | Traffic cannot reach a NAT gateway through a peering connection |
| VPN/Direct Connect | Cannot route into a NAT gateway through a virtual private gateway; possible through a Transit Gateway |
What it costs (us-east-1)
| Item | Price | Monthly (730 h) |
|---|---|---|
| NAT gateway hour (zonal) | USD 0.045 per hour ⚠ VERIFY us-east-1 rate in the Pricing Calculator | ≈ USD 32.85 per NAT gateway |
| Regional NAT gateway | USD 0.045 per hour per active AZ | ≈ USD 65.70 for two AZs |
| Data processed | USD 0.045 per GB, in both directions, regardless of destination | 500 GB ≈ USD 22.50 |
| Elastic IP on the NAT gateway | USD 0.005 per hour | ≈ USD 3.65 |
| Data transfer out to the internet | Standard EC2 data-transfer-out rates, on top | Depends on volume |
| Cross-AZ traffic to a NAT gateway in another AZ | Inter-AZ data transfer, on top | Avoid by keeping NAT per AZ |
A "three AZs, one NAT each" dev VPC costs roughly USD 110 per month idle (3 × 32.85 + 3 × 3.65). And here is the trap that matters most: traffic to S3 or DynamoDB through a NAT gateway pays USD 0.045 per GB, while the same traffic through a gateway endpoint is free.
What problem the NAT gateway solves
Private workloads often need some outbound access: OS and package updates, third-party APIs (a sanctions-screening provider, a card-scheme test endpoint, an Open Banking directory), SaaS monitoring. The NAT gateway gives them that without giving them public addresses or making them reachable. It is fully managed: no instances to patch, automatic scaling, built-in redundancy inside its AZ.
Its limitations are just as important:
- No filtering. It translates; it does not decide. Domain allow-lists need AWS Network Firewall, a proxy, or Route 53 Resolver DNS Firewall.
- It is zonal (in zonal mode). If its AZ fails, subnets that route to it lose egress — and a single NAT for many AZs also costs cross-AZ data charges.
- It is not cheap, and it charges for AWS-service traffic that endpoints would carry for free or less.
- Port exhaustion. Thousands of concurrent connections to one destination (one partner API IP and port) can hit the 55,000 limit per address; you see
ErrorPortAllocationin CloudWatch. Connection pooling in .NET (SocketsHttpHandler, a singletonHttpClient/IHttpClientFactory) is the real fix; extra IPs are the fallback.
NAT-free and NAT-light designs
| Need | Better option than NAT | Cost |
|---|---|---|
| S3 or DynamoDB | Gateway endpoints | Free |
| Other AWS APIs (Secrets Manager, KMS, SQS, ECR, CloudWatch Logs, STS…) | Interface endpoints (VPC endpoints and PrivateLink) | USD 0.01 per hour per AZ + USD 0.01 per GB — cheaper per GB, but hourly per endpoint |
| Container images | ECR via interface endpoints (ecr.api, ecr.dkr) plus the S3 gateway endpoint (image layers come from S3) | As above |
| OS packages | Internal mirror, or vendor repositories hosted in S3 reached via the gateway endpoint | Low |
| Partner APIs on the internet | Central egress VPC with NAT and AWS Network Firewall, shared through Transit Gateway | NAT hours paid once for many VPCs |
| IPv6-capable destinations | Egress-only internet gateway | Free (data transfer only) |
The decision rule Harbourline uses: if the destination is an AWS service, use an endpoint; if it is the internet, go through central, filtered egress; a NAT gateway in a workload VPC needs an exception.
Microsoft / Azure comparison
| You know (Microsoft/Azure) | AWS equivalent | What's different |
|---|---|---|
| Azure NAT Gateway | Public NAT gateway | Azure NAT Gateway attaches to subnets; AWS NAT is a route target in route tables |
| Zone-redundant NAT (Azure NAT Gateway v2 / zonal) | Regional NAT gateway vs zonal NAT gateway | AWS zonal NAT lives in one AZ; regional mode spans AZs automatically |
| Public IP prefix on NAT Gateway | Elastic IPs (primary + secondary) on the NAT gateway | Each address adds 55,000 connections per destination |
| Default outbound access (being retired) | No equivalent — no outbound without IGW/NAT routes | AWS custom VPCs were never "open by default" |
| Service endpoints for Storage | Gateway endpoints for S3 and DynamoDB | Both keep traffic off NAT; AWS gateway endpoints are free |
| Azure Firewall as egress hub (forced tunnelling) | Central egress VPC: Transit Gateway + Network Firewall + NAT | Same pattern; AWS needs explicit TGW route tables |
| SNAT port exhaustion on Load Balancer outbound rules | NAT gateway ErrorPortAllocation | Same cause and same fix: connection reuse |
Architecture
Security-boundary view: the NAT gateway is a one-way valve at the edge of the private trust zone — outbound connections only, translated to known Elastic IPs that partners can allow-list. It is not an inspection point: domain filtering, TLS inspection and egress logging must be added (Network Firewall, DNS Firewall, Flow Logs). AWS-service traffic bypasses the valve completely through endpoints, which have their own policy-based controls.
Step-by-step implementation
Price it first
Before you click anything, write down: hours you will keep it (target: under one hour), expected data (a few MB), Elastic IP hours. For this lab that is about USD 0.05.
Allocate an Elastic IP and create a zonal NAT gateway in a public subnet
Use hl-m02-public-us-east-1a. The subnet must already route 0.0.0.0/0 to the internet gateway (previous lesson). Wait until the state is available.
Route the app tier to it
Add 0.0.0.0/0 → nat-… to hl-m02-app-rt. The app subnets change from isolated to private — re-run the subnet auditor from lesson 4 to see it.
Prove outbound works and inbound does not
From an instance or container in an app subnet (for example via Session Manager, if you have one running — optional), curl https://checkip.amazonaws.com returns the NAT gateway's Elastic IP. Nothing on the internet can connect to the instance: it has no public address and the NAT has no entry for unsolicited connections.
Delete it — and the Elastic IP
Remove the route, delete the NAT gateway, wait for deleted, then release the Elastic IP. An unreleased Elastic IP keeps billing.
AWS Console instructions
- VPC → Elastic IPs → Allocate Elastic IP address → Amazon's pool of IPv4 addresses → add tags → Allocate.
- VPC → NAT gateways → Create NAT gateway. Name
hl-m02-nat; Availability mode: Zonal; Subnet:hl-m02-public-us-east-1a; Connectivity type: Public; Elastic IP allocation ID: the one you just allocated; tags → Create. - For a regional NAT gateway instead: Availability mode → Regional, choose the VPC — no subnet is needed. Then point private route tables at it.
- Route tables →
hl-m02-app-rt→ Edit routes → Add route0.0.0.0/0, Target NAT Gateway →hl-m02-nat. - CloudWatch → Metrics → NATGateway shows
BytesOutToDestination,ActiveConnectionCount,ErrorPortAllocation,PacketsDropCountandIdleTimeoutCountper gateway. - Delete: NAT gateways → Actions → Delete NAT gateway (type
delete); then Elastic IPs → Actions → Release Elastic IP addresses.
AWS CLI
$p = "--profile", "learn-sandbox", "--region", "us-east-1"
$tags = "{Key=Project,Value=cea-lab},{Key=Lab,Value=m02-secure-vpc},{Key=Environment,Value=learn},{Key=Owner,Value=alex}"
$vpcId = aws ec2 describe-vpcs --filters Name=tag:Name,Values=hl-m02-vpc @p --query "Vpcs[0].VpcId" --output text
$pubA = aws ec2 describe-subnets @p --filters Name=vpc-id,Values=$vpcId Name=tag:Name,Values=hl-m02-public-us-east-1a --query "Subnets[0].SubnetId" --output text
$appRt = aws ec2 describe-route-tables @p --filters Name=vpc-id,Values=$vpcId Name=tag:Name,Values=hl-m02-app-rt --query "RouteTables[0].RouteTableId" --output text
# 1. Elastic IP for the NAT gateway (bills USD 0.005/hour until released)
$eipAlloc = aws ec2 allocate-address --domain vpc @p `
--tag-specifications "ResourceType=elastic-ip,Tags=[{Key=Name,Value=hl-m02-nat-eip},$tags]" `
--query AllocationId --output text
# 2. Zonal public NAT gateway in public subnet A (billing starts now)
$natId = aws ec2 create-nat-gateway --subnet-id $pubA --allocation-id $eipAlloc --connectivity-type public @p `
--tag-specifications "ResourceType=natgateway,Tags=[{Key=Name,Value=hl-m02-nat},$tags]" `
--query NatGateway.NatGatewayId --output text
aws ec2 wait nat-gateway-available --nat-gateway-ids $natId @p
# 3. Default route for the app tier
aws ec2 create-route --route-table-id $appRt --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $natId @p
# 4. Inspect: state, addresses, subnet
aws ec2 describe-nat-gateways --nat-gateway-ids $natId @p `
--query "NatGateways[0].{State:State,Subnet:SubnetId,Public:NatGatewayAddresses[0].PublicIp,Private:NatGatewayAddresses[0].PrivateIp}"
# Alternative: a regional NAT gateway (no public subnet, one ID for all AZs)
# aws ec2 create-nat-gateway --vpc-id $vpcId --availability-mode regional @p
# 5. Port-exhaustion check for the last hour
$end = (Get-Date).ToUniversalTime().ToString("o"); $start = (Get-Date).ToUniversalTime().AddHours(-1).ToString("o")
aws cloudwatch get-metric-statistics --namespace AWS/NATGateway --metric-name ErrorPortAllocation `
--dimensions Name=NatGatewayId,Value=$natId --statistics Sum --period 300 `
--start-time $start --end-time $end @pInfrastructure as Code
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using Constructs;
namespace Harbourline.Networking.Examples;
public sealed class EgressVpcStack : Stack
{
public EgressVpcStack(Construct scope, string id, bool enableNat, IStackProps? props = null)
: base(scope, id, props)
{
var vpc = new Vpc(this, "Vpc", new VpcProps
{
IpAddresses = IpAddresses.Cidr("10.20.0.0/16"),
MaxAzs = 2,
// One zonal NAT gateway (learning). Production: one per AZ, or a regional NAT gateway.
NatGateways = enableNat ? 1 : 0,
NatGatewayProvider = NatProvider.Gateway(),
SubnetConfiguration =
[
new SubnetConfiguration { Name = "public", SubnetType = SubnetType.PUBLIC, CidrMask = 20, MapPublicIpOnLaunch = false },
new SubnetConfiguration
{
Name = "app",
SubnetType = enableNat ? SubnetType.PRIVATE_WITH_EGRESS : SubnetType.PRIVATE_ISOLATED,
CidrMask = 20,
},
new SubnetConfiguration { Name = "data", SubnetType = SubnetType.PRIVATE_ISOLATED, CidrMask = 20 },
],
});
// Keep S3 traffic off the NAT gateway: free, and no per-GB NAT charge.
vpc.AddGatewayEndpoint("S3", new GatewayVpcEndpointOptions
{
Service = GatewayVpcEndpointAwsService.S3,
Subnets = [new SubnetSelection { SubnetGroupName = "app" }],
});
Amazon.CDK.Tags.Of(this).Add("Project", "cea-lab");
Amazon.CDK.Tags.Of(this).Add("Lab", "m02-secure-vpc");
Amazon.CDK.Tags.Of(this).Add("Environment", "learn");
}
}With NatGateways = 1 and two AZs, CDK puts the NAT gateway in the first public subnet and points both app route tables at it — cheaper, but AZ b loses egress if AZ a fails and pays cross-AZ data charges. Set NatGateways = 2 for one per AZ. Regional NAT gateways are exposed through the L1 CfnNatGateway resource ⚠ VERIFY the property name for availability mode in your CDK version.
Complete code
A NAT bill estimator. It lists every NAT gateway in the Region, reads the bytes it processed from CloudWatch over a period, and estimates the cost of hours, data and Elastic IPs. It also flags port-allocation errors and gateways that process almost nothing (candidates for deletion).
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.EC2" Version="4.1.4" />
<PackageReference Include="AWSSDK.CloudWatch" Version="4.1.1.2" />
<PackageReference Include="AWSSDK.SSO" Version="4.1.0.2" />
<PackageReference Include="AWSSDK.SSOOIDC" Version="4.1.0.2" />
</ItemGroup>
</Project>using Amazon;
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
using Amazon.EC2;
using Amazon.EC2.Model;
using Ec2Filter = Amazon.EC2.Model.Filter;
// Usage: $env:AWS_PROFILE = "learn-sandbox"; dotnet run -- 7
// Prices are inputs, not constants: check the VPC pricing page for your Region before trusting the output.
const decimal HourlyRate = 0.045m; // USD per NAT gateway hour (zonal)
const decimal PerGbRate = 0.045m; // USD per GB processed
const decimal PublicIpv4Hourly = 0.005m; // USD per public IPv4 address hour
var days = args.Length == 1 && int.TryParse(args[0], out var d) && d is > 0 and <= 63 ? d : 7;
var end = DateTime.UtcNow;
var start = end.AddDays(-days);
using var ec2 = new AmazonEC2Client(RegionEndpoint.USEast1);
using var cloudWatch = new AmazonCloudWatchClient(RegionEndpoint.USEast1);
var gateways = new List<NatGateway>();
await foreach (var nat in ec2.Paginators.DescribeNatGateways(new DescribeNatGatewaysRequest
{
Filter = [new Ec2Filter { Name = "state", Values = ["available"] }],
}).NatGateways)
{
gateways.Add(nat);
}
Console.WriteLine($"NAT gateways in us-east-1, last {days} day(s):");
decimal total = 0m;
foreach (var nat in gateways)
{
var since = nat.CreateTime is { } created && created.ToUniversalTime() > start ? created.ToUniversalTime() : start;
var hours = (decimal)(end - since).TotalHours;
// NAT data processing is charged on bytes in from the source and bytes in from the destination.
var bytes = await SumAsync(cloudWatch, nat.NatGatewayId, "BytesInFromSource", start, end)
+ await SumAsync(cloudWatch, nat.NatGatewayId, "BytesInFromDestination", start, end);
var gb = (decimal)bytes / (1024m * 1024m * 1024m);
var portErrors = await SumAsync(cloudWatch, nat.NatGatewayId, "ErrorPortAllocation", start, end);
var publicIps = (nat.NatGatewayAddresses ?? []).Count(a => !string.IsNullOrEmpty(a.PublicIp));
var cost = hours * HourlyRate + gb * PerGbRate + hours * publicIps * PublicIpv4Hourly;
total += cost;
var name = nat.Tags?.FirstOrDefault(t => t.Key == "Name")?.Value ?? "(no name)";
Console.WriteLine($" {nat.NatGatewayId,-24} {name,-20} {nat.SubnetId,-26} {hours,8:N1} h {gb,10:N2} GB ~USD {cost,8:N2}");
if (portErrors > 0)
{
Console.WriteLine($" WARNING: {portErrors:N0} port allocation errors — check HTTP connection reuse or add NAT IPs.");
}
if (hours > 24 && gb < 0.1m)
{
Console.WriteLine(" NOTE: almost no traffic — does this NAT gateway need to exist?");
}
}
Console.WriteLine($"Estimated total: ~USD {total:N2} (excludes internet data-transfer-out and cross-AZ charges).");
return 0;
static async Task<double> SumAsync(IAmazonCloudWatch cw, string natId, string metric, DateTime start, DateTime end)
{
var response = await cw.GetMetricStatisticsAsync(new GetMetricStatisticsRequest
{
Namespace = "AWS/NATGateway",
MetricName = metric,
Dimensions = [new Dimension { Name = "NatGatewayId", Value = natId }],
StartTime = start,
EndTime = end,
Period = 86400,
Statistics = ["Sum"],
});
return (response.Datapoints ?? []).Sum(p => p.Sum ?? 0d);
}Example output after a one-hour lab:
NAT gateways in us-east-1, last 7 day(s):
nat-0e1… hl-m02-nat subnet-0a1… 0.8 h 0.01 GB ~USD 0.04
Estimated total: ~USD 0.04 (excludes internet data-transfer-out and cross-AZ charges).Code explained line by line
using Ec2Filter = Amazon.EC2.Model.Filter;— both the EC2 and CloudWatch model namespaces are imported; an alias removes any ambiguity about whichFilteryou mean.const decimal HourlyRate = 0.045m;— money isdecimal, neverdouble, even in a cost estimate. Rates are named constants so a reviewer can see and change them.ec2.Paginators.DescribeNatGateways(...)with astate = availablefilter — deleted gateways stay visible for a while indescribeoutput; they no longer bill.nat.CreateTime ... > start— a gateway created during the window only bills from its creation time.BytesInFromSource + BytesInFromDestination— the NAT gateway charges per GB it processes: traffic arriving from your instances plus replies arriving from the internet. Counting only outbound bytes underestimates downloads (for example container images).ErrorPortAllocation— non-zero means the gateway could not allocate a source port: too many simultaneous connections to one destination.GetMetricStatisticsAsync(... Period = 86400, Statistics = ["Sum"])— one datapoint per day; summing them gives the period total.Datapointscan benullin SDK v4, so it is guarded.hours * publicIps * PublicIpv4Hourly— each public IPv4 address on the gateway bills hourly too.- The estimate deliberately excludes internet data transfer out and cross-AZ transfer; the output says so. A cost tool that hides its assumptions is worse than none.
Security considerations
- NAT is not a firewall. Pair any internet egress with domain filtering (AWS Network Firewall or Route 53 Resolver DNS Firewall) and logging. See segmentation and egress control.
- Prefer endpoints for AWS APIs — they keep traffic private and let you apply endpoint policies (for example "only our own S3 buckets"), which a NAT path cannot.
- Stable source IPs for partners. Partners often allow-list your egress addresses. Use Elastic IPs you own (zonal, or regional manual mode) and record them in the partner onboarding pack; automatic-mode addresses are managed by AWS.
- NACLs on the NAT subnet must allow ephemeral ports 1024–65535 in both directions, or replies are dropped.
- Data tier never routes to NAT. That is the core rule from the subnet tiers lesson; check it in CI.
- Private NAT for overlaps is a workaround, not a design goal. It hides source addresses, which complicates audit trails; record the translation in your network documentation.
⚠ Learning shortcut — not for production
The lab creates one zonal NAT gateway in one AZ, shared by both app subnets, for at most an hour. Production at Harbourline uses central egress: workload VPCs route 0.0.0.0/0 to a Transit Gateway, and an egress VPC owned by the network team runs NAT gateways in every AZ (or a regional NAT gateway) behind AWS Network Firewall with a domain allow-list, Flow Logs and firewall logs retained for investigations. Workload accounts have no NAT gateways and no internet gateways.
Banking use case
Harbourline's Payments Service must call Clearwater Screening (a fictional sanctions-screening provider) at api.clearwater-screening.example. Clearwater only accepts connections from allow-listed IPs.
- The Payments VPC has no NAT gateway. Its app route table sends
0.0.0.0/0to the Transit Gateway. - The egress VPC (network account) has one NAT gateway per AZ with Elastic IPs registered with Clearwater, behind AWS Network Firewall. The firewall's domain allow-list for the payments segment contains
api.clearwater-screening.exampleand the card scheme's test endpoint — nothing else. - S3 (payment files, statements) and DynamoDB (idempotency keys) go through gateway endpoints in the Payments VPC; Secrets Manager, KMS and SQS through interface endpoints. None of that traffic reaches the NAT gateways — which cut the egress VPC's NAT data-processing bill by about 80% when it was introduced (fictional figure, typical pattern).
- The .NET
HttpClientfor Clearwater is created withIHttpClientFactoryandPooledConnectionLifetime = 5 minutes, so the service keeps a small pool of long-lived connections instead of opening thousands — no port exhaustion, and DNS changes are still picked up.
Common mistakes
- One NAT gateway per AZ in every dev environment, left running → USD 100+/month per environment → NAT only where needed, scheduled teardown, or central egress.
- S3, DynamoDB and ECR traffic through NAT → per-GB charges on every image pull and file transfer → gateway endpoints for S3/DynamoDB, interface endpoints for ECR.
- NAT gateway in a private subnet (zonal mode) → it has no route to the IGW, so nothing works → zonal public NAT goes in a public subnet (or use regional mode).
- One NAT gateway for all AZs in production → an AZ failure removes egress for every AZ, and cross-AZ data is billed → one per AZ, or regional mode.
- Forgetting to release the Elastic IP after deleting the NAT gateway → it keeps billing → release it.
- New
HttpClientper request in .NET → socket and NAT port exhaustion →IHttpClientFactoryor a sharedSocketsHttpHandler.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Private instances time out reaching the internet | No 0.0.0.0/0 → nat route, or NAT in a subnet without an IGW route | Check both route tables; the NAT's own subnet must route to the IGW |
ErrorPortAllocation > 0 | Over 55,000 concurrent connections to one destination per NAT IP | Reuse connections; add secondary IPs; spread across NAT gateways |
| Long-running idle connections drop | NAT idle timeout | TCP keep-alives shorter than the timeout; reconnect logic |
| Downloads fail but small requests work | MTU above 1500 on instances over the internet path | Keep instance MTU at 1500 for internet traffic |
Route shows blackhole | NAT gateway deleted | Replace the route target or remove the route |
NAT gateway stuck failed at creation | Elastic IP already associated, subnet has no free IPs, or EIP quota | Check FailureMessage in describe-nat-gateways |
| Unexpected NAT bill | AWS-service traffic through NAT | Flow Logs or the estimator above; add gateway/interface endpoints |
Cost and cleanup
Cost label: credits. One zonal NAT gateway plus one Elastic IP for one hour in us-east-1 ≈ USD 0.045 + USD 0.005 + data (a few MB ≈ USD 0.00) ≈ USD 0.05. Left running for a month: ≈ USD 36.50 plus data. Free alternative: do this lesson design-only — build the route tables and endpoints, and read the CLI and estimator output above instead of creating the gateway.
Teardown (in this order):
$p = "--profile", "learn-sandbox", "--region", "us-east-1"
# 1. Remove the default route from the app tier
aws ec2 delete-route --route-table-id $appRt --destination-cidr-block 0.0.0.0/0 @p
# 2. Delete the NAT gateway and wait until it is gone
aws ec2 delete-nat-gateway --nat-gateway-id $natId @p
aws ec2 wait nat-gateway-deleted --nat-gateway-ids $natId @p
# 3. Release the Elastic IP (it bills while allocated)
aws ec2 release-address --allocation-id $eipAlloc @p
# 4. Confirm nothing is still billing
aws ec2 describe-nat-gateways @p --filter Name=state,Values=pending,available --query "NatGateways[].NatGatewayId"
aws ec2 describe-addresses @p --query "Addresses[].[PublicIp,AllocationId]" --output table
aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=cea-lab @p --query "ResourceTagMappingList[].ResourceARN"Exercises
- 🟢 Guided: Create the NAT gateway, add the route, run the subnet auditor (app subnets become
private) and the NAT estimator, then tear everything down within 60 minutes and confirm with the final three commands. - 🟡 Challenge: Without creating anything, calculate the monthly cost of three designs for a dev VPC with two AZs that pulls 200 GB of container images and 300 GB of S3 data a month and sends 5 GB to a partner API: (a) one NAT per AZ for everything; (b) one NAT per AZ plus S3 gateway endpoint plus ECR interface endpoints; (c) no NAT, S3 gateway endpoint, ECR endpoints, partner traffic via a central egress VPC (assume the central NAT cost is shared and allocate 5 GB of it).
- 🔴 Live problem: "Since Black Friday the Payments Service logs intermittent
HttpRequestException: Cannot assign requested addressand timeouts calling the screening provider. CloudWatch showsErrorPortAllocationspikes on one NAT gateway. The screening provider says nothing is wrong." Diagnose and propose short- and long-term fixes.
Assignment
Write a one-page egress decision record for a fictional Harbourline service: list every outbound destination (AWS services and internet hosts), choose endpoint, central egress or NAT for each, estimate monthly cost with the formula from this lesson, and state how egress is filtered and logged. Commit it as docs/adr/0002-egress.md in your learning repository.
Solutions
The auditor changes the app subnets from isolated to private because their route table now has a NatGatewayId route. The estimator shows well under one hour and a few MB, so a few US cents. After teardown, describe-nat-gateways with the pending,available filter returns [], describe-addresses shows no hl-m02-nat-eip, and the tagging API lists no NAT or EIP ARNs. Why the order matters: you cannot release an Elastic IP while the NAT gateway still uses it, and a route left pointing at a deleted NAT becomes a blackhole.
Using USD 0.045/h, 0.045/GB, 0.005/h per IP, 0.01/h per interface endpoint per AZ and 0.01/GB, 730 h/month:
- (a) 2 NAT × 32.85 = 65.70; 2 EIPs = 7.30; data 505 GB × 0.045 = 22.73 → ≈ USD 95.73.
- (b) NAT + EIPs = 73.00; NAT data now only partner 5 GB = 0.23; S3 gateway endpoint free; ECR needs
ecr.apiandecr.dkr: 2 endpoints × 2 AZs × 0.01 × 730 = 29.20, plus 200 GB × 0.01 = 2.00 → ≈ USD 104.43. More expensive at this volume! - (c) No NAT; ECR endpoints 31.20 (hours plus data); S3 free; partner 5 GB through central egress ≈ 0.23 plus a share of the central hours → ≈ USD 31.43 plus a small shared cost.
Why: endpoints do not always save money on their own — interface endpoints bill hourly per AZ. The big saving comes from removing per-VPC NAT gateways (central egress) and from free gateway endpoints. Always do the arithmetic for your volumes; interface endpoints win as data volume grows and when they are shared centrally.
Cannot assign requested address plus ErrorPortAllocation means source-port exhaustion: more than 55,000 simultaneous connections from one NAT IP to the same destination IP, port and protocol. The provider sees nothing wrong because the failures happen before packets leave. Short term: add secondary IP addresses to the NAT gateway (each adds 55,000 per destination) — tell the provider to allow-list them first. Long term: fix the client: one HttpClient per named client via IHttpClientFactory, SocketsHttpHandler with MaxConnectionsPerServer and PooledConnectionLifetime, keep-alive on, and retries with jitter (Polly) so failures do not cause connection storms. Add a CloudWatch alarm on ErrorPortAllocation > 0.
Knowledge check
Answer: The instance has no public address, and the NAT gateway only translates replies for connections in its translation table, which are created only by outbound connections.
Answer: To let resources reach other VPCs or on-premises networks (through Transit Gateway or a virtual private gateway) using the NAT gateway's private IP — typically when CIDRs overlap. It cannot reach the internet.
Answer: It needs no public subnet, and one gateway ID serves all AZs, expanding automatically where workloads exist (high availability by default, simpler routes). It also supports more IPs per AZ (32 vs 8).
Answer: An S3 gateway endpoint on the app route tables. It is free and removes about USD 45 a month of NAT data processing (1,024 GB × 0.045).
Answer: No. Control traffic with security groups on the instances and NACLs on the subnets.
Interview questions
- "How do you give private instances internet access, and what does it cost?" — Route
0.0.0.0/0to a NAT gateway (zonal in a public subnet, or regional). Roughly USD 33 a month per zonal gateway plus USD 0.045 per GB processed in us-east-1, plus public IPv4. I'd first ask whether they really need the internet or just AWS APIs. - "How would you reduce a large NAT bill?" — Find the destinations with Flow Logs; move S3/DynamoDB to gateway endpoints, other AWS APIs to shared interface endpoints; centralise remaining egress; delete idle dev NAT gateways on a schedule.
- "What is NAT port exhaustion and how do you fix it in .NET?" — Each NAT IP supports 55,000 concurrent connections per destination; creating
HttpClientper request exhausts ports. UseIHttpClientFactory, pool connections, and add NAT IPs only as a fallback. - "How do you control where workloads can go on the internet?" — NAT doesn't; use AWS Network Firewall domain lists or DNS Firewall in a central egress VPC, with logging.
Certification practice
Applications in private subnets read 20 TB a month from Amazon S3 in the same Region through a NAT gateway. The company wants to cut cost without changing application code. What should a solutions architect do?
A. Replace the NAT gateway with a NAT instance B. Add an S3 gateway endpoint and associate it with the private subnets' route tables C. Move the instances to public subnets with public IPs D. Enable S3 Transfer Acceleration
Answer: B. The gateway endpoint is free and its prefix-list route is more specific than 0.0.0.0/0, so S3 traffic moves off the NAT gateway automatically. A adds operations work and still pays data transfer limits. C exposes the instances. D costs more and is for long-distance uploads.
A VPC has private subnets in three AZs, all routing 0.0.0.0/0 to a single zonal NAT gateway in AZ a. What happens if AZ a becomes impaired, and what is the recommended design?
A. Nothing — NAT gateways are multi-AZ B. Instances in AZ b and AZ c lose internet egress; use one NAT gateway per AZ with per-AZ route tables, or a regional NAT gateway C. Traffic fails over to the internet gateway automatically D. Instances in AZ b and AZ c lose all connectivity inside the VPC
Answer: B. A zonal NAT gateway is redundant only within its AZ. A is wrong for zonal mode. C — private instances have no public IPs and no IGW route. D — the local route is unaffected.
A CloudOps engineer sees ErrorPortAllocation increasing on a NAT gateway while an application makes many concurrent calls to one external API endpoint. Which action directly increases capacity for that destination?
A. Increase the NAT gateway's bandwidth setting B. Associate secondary IPv4 addresses with the NAT gateway C. Add a security group rule to the NAT gateway D. Change the subnet's network ACL
Answer: B. Each IPv4 address supports up to 55,000 simultaneous connections per unique destination. A — bandwidth is not configurable and isn't the constraint. C — NAT gateways do not take security groups. D does not add ports.
A bank must ensure workloads in private subnets can reach only three approved partner domains on the internet, and must log every attempt. Workloads currently use NAT gateways. What should the security engineer add?
A. Security group outbound rules with the partners' domain names B. AWS Network Firewall with a domain allow-list, placed in the egress path before the NAT gateway, with logging enabled C. A network ACL denying 0.0.0.0/0 D. Amazon GuardDuty
Answer: B. Network Firewall can filter by domain (SNI/Host) and log; routed in front of NAT it controls all egress. A — security groups accept IPs, prefix lists and SG references, not domain names. C would block everything, including approved partners. D detects threats but does not enforce an allow-list.
Completion checklist
- I can explain public vs private NAT and zonal vs regional mode
- I calculated the monthly cost of a NAT design before deploying it
- I created a NAT gateway, routed the app tier through it, and tested it (or did the design-only version)
- I deleted the NAT gateway, released the Elastic IP and confirmed nothing is billing
- I can name a NAT-free option for S3, DynamoDB, ECR and other AWS APIs
Key takeaways
- A NAT gateway gives outbound-only access by translating source addresses; it never accepts unsolicited inbound connections.
- It is not a firewall: add domain filtering and logging for internet egress.
- Zonal NAT lives in one AZ (one per AZ for resilience); regional NAT spans AZs and needs no public subnet.
- Cost = hours (per gateway or per active AZ) + USD 0.045/GB processed + public IPv4 — and AWS-service traffic through NAT is the classic waste.
- Prefer endpoints for AWS services and central, filtered egress for the internet; NAT in a workload VPC should be an exception.
Further reading
Independent learning resource — not affiliated with, endorsed or sponsored by Amazon Web Services. AWS and related marks are trademarks of Amazon.com, Inc. or its affiliates. All banking data in examples is fictional. Sample architectures are for learning and are not automatically compliant with GDPR, PCI DSS or any banking regulation.
Route tables and the internet gateway
Read any VPC route table like a network engineer, predict which route a packet takes with longest-prefix matching, attach an internet gateway safely, and explain a route decision with a C# tool.
Security groups vs network ACLs
Filter traffic in a VPC with stateful security groups that reference each other and stateless network ACLs as a subnet guardrail, know exactly which one to use when, and audit both with a C# tool.