Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 70 ยท Networking with VPC

NAT Gateways and VPC Endpoints

Give private subnets outbound internet access with a NAT gateway, keep AWS traffic off the internet with gateway and interface endpoints, and avoid the cost traps.

Servers in a private subnet have no route to the internet, which is the point. But they still need to do things: install security updates, call a payment API, send logs, read files from S3. There are two ways to give them that reach without opening them to inbound traffic. A NAT gateway lets them start connections to the internet. VPC endpoints let them reach AWS services without using the internet at all.

Both are easy to set up and both can quietly become a large line on the bill. This lesson explains how each works, when to prefer which, and what to watch for. The CLI commands are read-only; the runnable simulator is a stand-in that models route tables and shows which path a request takes, building on the lookup rule from VPC Fundamentals.

NAT: outbound-only internet access

Network Address Translation (NAT) replaces a private source address with a public one on the way out and reverses it on the way back. The effect is one-directional: a private instance can open a connection to the internet and receive the replies, but the internet cannot start a connection to it.

A NAT gateway is the managed AWS version. You create it in a public subnet and give it an Elastic IP address. Then you add a default route to it in the private subnets' route table:

Destination Target
10.0.0.0/16 local
0.0.0.0/0 nat-0abc123

The NAT gateway itself sends traffic out through the internet gateway, which is why it must sit in a subnet whose route table points to one. A frequent mistake is to place it in a private subnet, where it cannot reach the internet at all.

Terminal
# read-only: allocate an address, create the NAT gateway, add the route
aws ec2 allocate-address --domain vpc
aws ec2 create-nat-gateway --subnet-id subnet-0pub1 \
  --allocation-id eipalloc-0123456789abcdef0
aws ec2 create-route --route-table-id rtb-0private \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id nat-0abc123

A NAT gateway lives in one Availability Zone. If that zone fails, private subnets routed through it lose their internet access. For availability, create one NAT gateway per zone and point each zone's private route table at its own gateway. That also avoids cross-zone data charges.

flowchart LR
    APP["App server<br/>private subnet"] -->|"0.0.0.0/0"| NAT["NAT gateway<br/>public subnet"]
    NAT --> IGW["Internet gateway"]
    IGW --> NET(["Internet"])
    NET -.->|"no inbound connections"| APP

The private server starts connections outward through NAT; the internet cannot start one in return.

The cost trap

NAT gateways are charged per hour while they exist and per gigabyte of data they process, in both directions. At typical prices that is roughly the cost of a small server per month for the hourly part alone, plus a per-gigabyte fee that is easy to forget. A busy private fleet pulling container images, backups or logs through a NAT gateway can generate a bill larger than the servers themselves. Prices vary by Region and change, so check the current pricing page; the point is the shape of the cost, not the exact number. See Cost Management and Optimization for how to find this on a bill.

The most common cause of a big NAT bill is traffic to AWS services such as S3 or DynamoDB flowing through NAT when it need not. Endpoints fix that.

VPC endpoints: reach AWS privately

A VPC endpoint connects your VPC to an AWS service (or to a service from another account) without an internet gateway, NAT device, VPN or public IP. Traffic stays on the AWS network. There are two common families.

Gateway endpoints

A gateway endpoint works only for S3 and DynamoDB. It is not a network interface; it is a target you place in route tables. AWS adds a route whose destination is a prefix list (a managed list of the service's public address ranges) and whose target is the endpoint. Gateway endpoints have no hourly or data charge, so for S3 and DynamoDB there is rarely a reason not to use one.

Destination Target
10.0.0.0/16 local
pl-63a5400a (S3 prefix list) vpce-0123abcd
0.0.0.0/0 nat-0abc123

The prefix list covers many addresses, each more specific than 0.0.0.0/0, so S3 traffic matches the endpoint route first and bypasses NAT.

An interface endpoint is an elastic network interface with a private IP address in your subnet. It is powered by AWS PrivateLink. Requests to the service's DNS name (for example sqs.eu-west-1.amazonaws.com) resolve to that private address when private DNS is on, so existing code works unchanged. Interface endpoints support most AWS services, such as SQS, Secrets Manager, KMS and CloudWatch Logs.

They cost an hourly fee per Availability Zone plus a per-gigabyte fee, so ten services across three zones is thirty billed endpoints. They are protected with security groups (allow HTTPS from your instances) and you can attach an endpoint policy, a resource policy that limits what can be done through that endpoint.

PrivateLink is also how you can expose your own service to other VPCs and accounts: you put a Network Load Balancer in front of it, create an endpoint service, and consumers create interface endpoints to it. It works across overlapping CIDR ranges, because only one-way connections to a single service are allowed, not full network access.

Terminal
# read-only: a gateway endpoint for S3 and an interface endpoint for SQS
aws ec2 create-vpc-endpoint --vpc-id vpc-0aaa \
  --service-name com.amazonaws.eu-west-1.s3 \
  --route-table-ids rtb-0private

aws ec2 create-vpc-endpoint --vpc-id vpc-0aaa --vpc-endpoint-type Interface \
  --service-name com.amazonaws.eu-west-1.sqs \
  --subnet-ids subnet-0priv1 subnet-0priv2 \
  --security-group-ids sg-0endpoints --private-dns-enabled

Choosing between them

Need Use
Private instances call S3 or DynamoDB Gateway endpoint (free)
Private instances call other AWS APIs without internet Interface endpoint
Private instances call third-party websites or download OS packages NAT gateway
Expose your service privately to other VPCs or accounts PrivateLink endpoint service
Fully isolated subnets with no internet at all Endpoints only, no NAT

You can use both at once. A typical private subnet has a gateway endpoint for S3, a few interface endpoints for the services it uses heavily, and one NAT gateway per zone for everything else.

A route-table simulator

The model below builds a private route table with a NAT route and an S3 gateway endpoint, then reports which target each request would use and what it would cost on a simplified meter. The destinations and prices are made up for the exercise; only the longest-prefix selection is real.

Python NAT versus endpoint: which path is used?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try removing the S3 entries and compare the totals. The two S3 transfers move 180 GB, and without the endpoint they join the NAT bill.

Seeing the difference from an instance

You can tell which path a request takes by looking at its source. Through a NAT gateway the remote server sees the gateway's Elastic IP; through a gateway endpoint it sees the instance's private address, and for S3 you can write a bucket policy that allows access only from a given endpoint. A simple stand-in shows how a policy condition on the endpoint ID works. The real condition key is aws:SourceVpce; the check below is only a model of its effect.

Python Restricting a bucket to one endpoint (model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This pattern, combined with the policy tools in S3 Security, lets you build data buckets that are unreachable from anywhere outside your network.

Common pitfalls

  • NAT in the wrong subnet. It belongs in a public subnet. In a private one it silently has no outbound path.
  • One NAT gateway for all zones. Cheap, but a single point of failure and a source of cross-zone charges.
  • Forgetting the route. A gateway endpoint does nothing until it is associated with the route tables of the subnets that need it.
  • Interface endpoint without private DNS. Code still resolves the public name and goes through NAT, so the endpoint sits idle while you pay for it.
  • Security group on the endpoint blocks HTTPS. Allow port 443 from your instances' security group.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where must a NAT gateway be placed so that private subnets can reach the internet through it?

    The NAT gateway forwards traffic out through the internet gateway, so it needs a public subnet with a route to it.
  2. 2. Which services can use a gateway endpoint, which has no hourly or data processing charge?

    Gateway endpoints exist only for S3 and DynamoDB. Other services use interface endpoints, which are billed per hour per zone and per gigabyte.
  3. 3. A private fleet sends 5 TB a month to S3 through a NAT gateway. What is the cheapest fix?

    A gateway endpoint adds a more specific route for S3's prefix list, so the traffic skips NAT and its per-gigabyte charge.
  4. 4. What does AWS PrivateLink do?

    PrivateLink exposes a single service to consumers through interface endpoints, without full network connectivity and even when address ranges overlap.

Summary

  • A NAT gateway gives private subnets outbound-only internet access; place it in a public subnet and use one per zone for availability.
  • NAT is billed per hour and per gigabyte processed, so heavy traffic to AWS services should avoid it.
  • Gateway endpoints for S3 and DynamoDB are free and work through route table entries and prefix lists.
  • Interface endpoints (PrivateLink) put a private network interface in your subnets, cost per zone and per gigabyte, and support most other services.
  • Pair endpoints with endpoint policies and bucket policies to keep data traffic inside your network.

Next: NACLs, Flow Logs and Network Security.