DEV Community

Abdul Waheed
Abdul Waheed

Posted on

NAT Gateway vs VPC Endpoints

NAT Gateway vs VPC Endpoints: Designing Smarter Connectivity for AWS Private Subnets

AWS Networking | VPC Architecture | Routing | Security | Cost Optimization

The Challenge: Keeping Private Workloads Connected

A private subnet is designed to keep AWS resources away from direct inbound internet access. But private resources still need to communicate with other systems.

For example, an EC2 instance might need to install security patches, send application logs to Amazon S3, or retrieve files from an AWS service.

The challenge is deciding which network path should handle each request.

AWS offers NAT Gateways and VPC Endpoints to address different connectivity needs. Although both can be part of a private subnet architecture, they are not interchangeable.

Let's examine three common traffic scenarios and build a practical understanding of when each solution makes sense.

  1. Scenario One: Your Application Needs the Internet

Imagine an EC2 instance running inside a private subnet. The instance needs to download updates from a public software repository, but you don't want to assign it a public IPv4 address.

A public NAT Gateway can provide the required outbound IPv4 connectivity.

What happens to the traffic?

  1. The EC2 instance sends a request to an external destination.
  2. The private subnet's route table directs the matching traffic to the NAT Gateway.
  3. The NAT Gateway translates the source address and forwards the request through an Internet Gateway.
  4. The response returns through the NAT Gateway to the original instance.

For this architecture, the NAT Gateway resides in a public subnet and uses an Elastic IP address.

When is this approach useful?

  • Linux package and operating system updates.
  • Public REST APIs and external application services.
  • Public software repositories.
  • Other internet destinations that require outbound connectivity.

What about cost?

NAT Gateway charges generally include gateway usage time and data processed. Applicable data-transfer charges can also affect the total cost.

For production systems, consider Availability Zone resilience and whether a NAT Gateway in each relevant Availability Zone is appropriate.

Design lesson: Use NAT for the outbound internet traffic that genuinely requires it.

  1. Scenario Two: Your Workload Only Needs Amazon S3

Now consider a different requirement.

Your EC2 instance processes application data and regularly uploads reports, logs and backup files to Amazon S3.

Does this traffic need to pass through the NAT Gateway?

Not necessarily. An S3 Gateway Endpoint can provide a direct, private route to S3 for supported workloads within the VPC.

How does the endpoint work?

When you associate an S3 Gateway Endpoint with a route table, AWS installs a route for the S3 managed prefix list.

This route is more specific than the usual default route to a NAT Gateway. Consequently, matching S3 traffic follows the endpoint route.

For example, the private subnet might use:

  • "0.0.0.0/0" → NAT Gateway
  • S3 managed prefix list → S3 Gateway Endpoint

The first route handles matching default traffic; the second handles matching S3 destinations.

Why is it useful?

  • No additional usage charge for the Gateway Endpoint itself.
  • No endpoint network interface or endpoint security group to manage.
  • Suitable for private S3 access within the same VPC and AWS Region as the bucket.
  • Helps avoid sending eligible S3 traffic through a NAT Gateway.

Normal S3 storage, requests and other applicable charges still apply.

One important limitation: a Gateway Endpoint cannot directly serve clients from on-premises networks, peered VPCs or networks connected through a Transit Gateway.

Design lesson: If the requirement is ordinary in-VPC access to S3, start by evaluating a Gateway Endpoint.

  1. Scenario Three: S3 Must Be Accessible From Outside the VPC

Suppose a company stores backups in S3 but also operates a server in its on-premises data center. That server must reach S3 privately over the company's existing network connection.

A Gateway Endpoint does not meet this requirement by itself.

This is where an S3 Interface Endpoint may be appropriate.

What makes it different?

An Interface Endpoint uses AWS PrivateLink to create elastic network interfaces with private IP addresses in selected subnets.

Clients access the endpoint using suitable DNS names and private routing. Security groups attached to the endpoint's network interfaces control permitted connections, commonly HTTPS on TCP port 443.

With appropriate network routes, DNS configuration and security permissions, connected on-premises systems or other VPCs can access the endpoint.

What should you plan for?

  • Endpoint network interfaces in the required Availability Zones.
  • Security groups that permit traffic only from intended sources.
  • DNS resolution that directs clients to the appropriate endpoint addresses.
  • Routing between connected networks and endpoint subnets.
  • Hourly charges per enabled Availability Zone and data-processing charges.

Interface Endpoints are useful when their additional connectivity capabilities are needed. For workloads that only require S3 access within one VPC, the extra expense may not be justified.

Design lesson: Choose an Interface Endpoint when the network-access requirements call for it, not simply because it is another endpoint option.

  1. Compare the Three Network Paths

Design factor| NAT Gateway| S3 Gateway Endpoint| S3 Interface Endpoint
Main role| Outbound connectivity| Private in-VPC S3 access| PrivateLink-based S3 access
Typical destination| Public internet| Amazon S3| Amazon S3
Route mechanism| Default route| S3 prefix-list route| Private IP addresses and DNS
Endpoint ENIs| No endpoint ENI| No| Yes
Endpoint security group| No| No| Yes
On-premises S3 access| Not the preferred private endpoint path| Not supported directly| Supported with suitable configuration
Endpoint-specific pricing| NAT usage charges| No additional endpoint charge| Hourly and data-processing charges
Common example| Installing public software updates| Uploading application logs to S3| Private S3 access from connected networks

A NAT Gateway also has a private connectivity option, but a private NAT Gateway is designed for routing to other private networks; it does not provide internet connectivity through an Internet Gateway.

  1. Routing: The Detail That Makes the Difference

Network architecture is not only about creating resources. It is also about understanding how traffic selects a route.

Consider a private subnet whose route table sends default traffic to a NAT Gateway.

When an S3 Gateway Endpoint is associated with that route table, the S3 prefix-list route provides a more specific match for eligible S3 traffic.

This allows both routes to coexist without requiring every destination to use the same path.

Interface Endpoints follow another model: clients reach private IP addresses assigned to endpoint network interfaces, with DNS, routing and security group rules determining whether the connection works.

When troubleshooting a connection, review the route table, endpoint configuration, security groups, DNS resolution and the relevant service permissions together.

  1. A Realistic Architecture Design

Consider a business running an application in two Availability Zones.

Its requirements include:

  • EC2 instances need public software updates.
  • Application logs and backup files must reach S3.
  • An on-premises backup server also needs private S3 access.
  • Networking costs should be controlled without compromising availability.

A suitable starting design is to use:

NAT Gateway for outbound internet traffic from the private subnets.

S3 Gateway Endpoint for S3 traffic generated by workloads inside the VPC.

S3 Interface Endpoint for the specific on-premises access requirement, with the required private routing and DNS setup.

This design gives each traffic category a suitable path. It does not mean every application requires all three components.

For production deployment, review the Availability Zone design, endpoint policies, IAM permissions, bucket policies, DNS configuration, monitoring and current AWS regional pricing.

  1. Security Checks Before Deployment

Private connectivity alone does not guarantee that an application is secure.

A good implementation should include:

  • IAM roles with least-privilege permissions for EC2 workloads.
  • Endpoint policies that restrict access to intended resources and actions.
  • Security groups that allow only required connections to Interface Endpoint network interfaces.
  • S3 bucket policies that enforce the intended access requirements.
  • DNS and routing checks to confirm that clients use the expected path.
  • Logging and monitoring to help investigate failed or unexpected requests.

Be careful with restrictive bucket policies: a condition that requires access through a particular endpoint can also block legitimate access through other paths.

  1. A Simple Way to Make the Decision

Before creating a resource, identify the actual destination and the required connectivity.

Need outbound accwith the most components. It is the one that meets the workload's requirements with the right combination of connectivity, security and cost.

NAT Gateways and VPC Endpoints solve different problems, and understanding their differences makes it easier to build an architecture that is both practical and efficient.

My key takeaway from studying AWS networking: Understand the destination first, select the appropriate traffic path, and validate the design through routing and connectivity tests.

Always check current AWS documentation and regional pricing before using these patterns in production.

Top comments (0)