Skip to Content
Cloud Engineering Academy
2. AWS Networking & Infrastructure

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.

intermediate⏱ 1 h 20 min🟡 Low costSAA-C03SOA-C03SCS-C03

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

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

TypeLives inTranslates toUsed for
Public NAT gateway (default)A public subnet (zonal mode), with an Elastic IPIts Elastic IP, via the internet gatewayPrivate resources reaching the internet
Private NAT gatewayAny subnet; no Elastic IPIts private IPReaching 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

ModeHow it worksRoutes
Zonal (the original)One NAT gateway in one AZ's public subnet. For resilience you create one per AZEach 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 IGWEvery 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)

PropertyValue
ProtocolsTCP, UDP, ICMP
Bandwidth5 Gbps, scaling automatically to 100 Gbps
Packets1 million pps, scaling to 10 million pps; beyond that, packets drop
ConnectionsUp 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 AZ5 (adjustable)
Security groupsNot supported on a NAT gateway — filter at the instances and with NACLs
Ports used1024–65535 (matters for NACLs on the NAT gateway's subnet)
Idle timeout350 seconds; after that the NAT gateway answers with a TCP RST — use keep-alives under 350 s
PeeringTraffic cannot reach a NAT gateway through a peering connection
VPN/Direct ConnectCannot route into a NAT gateway through a virtual private gateway; possible through a Transit Gateway

What it costs (us-east-1)

ItemPriceMonthly (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 gatewayUSD 0.045 per hour per active AZ≈ USD 65.70 for two AZs
Data processedUSD 0.045 per GB, in both directions, regardless of destination500 GB ≈ USD 22.50
Elastic IP on the NAT gatewayUSD 0.005 per hour≈ USD 3.65
Data transfer out to the internetStandard EC2 data-transfer-out rates, on topDepends on volume
Cross-AZ traffic to a NAT gateway in another AZInter-AZ data transfer, on topAvoid 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 ErrorPortAllocation in CloudWatch. Connection pooling in .NET (SocketsHttpHandler, a singleton HttpClient / IHttpClientFactory) is the real fix; extra IPs are the fallback.

NAT-free and NAT-light designs

NeedBetter option than NATCost
S3 or DynamoDBGateway endpointsFree
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 imagesECR via interface endpoints (ecr.api, ecr.dkr) plus the S3 gateway endpoint (image layers come from S3)As above
OS packagesInternal mirror, or vendor repositories hosted in S3 reached via the gateway endpointLow
Partner APIs on the internetCentral egress VPC with NAT and AWS Network Firewall, shared through Transit GatewayNAT hours paid once for many VPCs
IPv6-capable destinationsEgress-only internet gatewayFree (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 equivalentWhat's different
Azure NAT GatewayPublic NAT gatewayAzure 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 gatewayAWS zonal NAT lives in one AZ; regional mode spans AZs automatically
Public IP prefix on NAT GatewayElastic IPs (primary + secondary) on the NAT gatewayEach address adds 55,000 connections per destination
Default outbound access (being retired)No equivalent — no outbound without IGW/NAT routesAWS custom VPCs were never "open by default"
Service endpoints for StorageGateway endpoints for S3 and DynamoDBBoth keep traffic off NAT; AWS gateway endpoints are free
Azure Firewall as egress hub (forced tunnelling)Central egress VPC: Transit Gateway + Network Firewall + NATSame pattern; AWS needs explicit TGW route tables
SNAT port exhaustion on Load Balancer outbound rulesNAT gateway ErrorPortAllocationSame 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 route 0.0.0.0/0, Target NAT Gateway → hl-m02-nat.
  • CloudWatch → Metrics → NATGateway shows BytesOutToDestination, ActiveConnectionCount, ErrorPortAllocation, PacketsDropCount and IdleTimeoutCount per 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 @p

Infrastructure as Code

EgressVpcStack.cs
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).

NatCost.csproj
<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>
Program.cs
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

  1. using Ec2Filter = Amazon.EC2.Model.Filter; — both the EC2 and CloudWatch model namespaces are imported; an alias removes any ambiguity about which Filter you mean.
  2. const decimal HourlyRate = 0.045m; — money is decimal, never double, even in a cost estimate. Rates are named constants so a reviewer can see and change them.
  3. ec2.Paginators.DescribeNatGateways(...) with a state = available filter — deleted gateways stay visible for a while in describe output; they no longer bill.
  4. nat.CreateTime ... > start — a gateway created during the window only bills from its creation time.
  5. 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).
  6. ErrorPortAllocation — non-zero means the gateway could not allocate a source port: too many simultaneous connections to one destination.
  7. GetMetricStatisticsAsync(... Period = 86400, Statistics = ["Sum"]) — one datapoint per day; summing them gives the period total. Datapoints can be null in SDK v4, so it is guarded.
  8. hours * publicIps * PublicIpv4Hourly — each public IPv4 address on the gateway bills hourly too.
  9. 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/0 to 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.example and 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 HttpClient for Clearwater is created with IHttpClientFactory and PooledConnectionLifetime = 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 HttpClient per request in .NET → socket and NAT port exhaustion → IHttpClientFactory or a shared SocketsHttpHandler.

Troubleshooting

SymptomLikely causeFix
Private instances time out reaching the internetNo 0.0.0.0/0 → nat route, or NAT in a subnet without an IGW routeCheck both route tables; the NAT's own subnet must route to the IGW
ErrorPortAllocation > 0Over 55,000 concurrent connections to one destination per NAT IPReuse connections; add secondary IPs; spread across NAT gateways
Long-running idle connections dropNAT idle timeoutTCP keep-alives shorter than the timeout; reconnect logic
Downloads fail but small requests workMTU above 1500 on instances over the internet pathKeep instance MTU at 1500 for internet traffic
Route shows blackholeNAT gateway deletedReplace the route target or remove the route
NAT gateway stuck failed at creationElastic IP already associated, subnet has no free IPs, or EIP quotaCheck FailureMessage in describe-nat-gateways
Unexpected NAT billAWS-service traffic through NATFlow 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 address and timeouts calling the screening provider. CloudWatch shows ErrorPortAllocation spikes 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

Knowledge check

Interview questions

  • "How do you give private instances internet access, and what does it cost?" — Route 0.0.0.0/0 to 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 HttpClient per request exhausts ports. Use IHttpClientFactory, 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

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

Next: Security groups vs network ACLs.

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.

On this page