Sub topic 6
Cutting NAT traffic with VPC endpoints
This chapter assumes you have read the previous chapter on NAT gateway costs. You should be comfortable with route tables, subnets, security groups, and the AWS CLI. The goal is to move traffic that currently crosses a NAT gateway onto VPC endpoints, and to know when that move saves money and when it costs more.
What a VPC endpoint changes
A NAT gateway charges per hour and per gigabyte processed. In us-east-1 the list price is about $0.045 for each. Every byte your private instances send to an AWS service through the NAT gateway pays that processing fee. S3 downloads, container image pulls, and log shipping all pay it.
A VPC endpoint gives your VPC a private path to an AWS service. The traffic stays on the AWS network and skips the NAT gateway. There are two types, and they are priced very differently.
| Property | Gateway endpoint | Interface endpoint |
|---|---|---|
| Supported services | S3 and DynamoDB only | Most other AWS services and PrivateLink services |
| How traffic reaches it | Route table entry that targets a prefix list | Elastic network interface (ENI) with a private IP in your subnet |
| Hourly charge | None | About $0.01 per AZ per endpoint |
| Data processing charge | None | About $0.01 per GB |
| Security groups | Not applicable | Attached to the ENI |
| Reachable from on premises or peered VPCs | No | Yes, through the ENI's private IP |
Prices are us-east-1 list prices at the time of writing. Check the current pricing pages before you commit to a design.
Gateway endpoints: the free win
Gateway endpoints have no hourly charge and no data processing charge. If a private subnet sends meaningful traffic to S3 or DynamoDB through a NAT gateway, adding the endpoint is almost always the right move.
The gateway endpoint works through routing. When you associate it with a route table, AWS adds a route whose destination is the AWS-managed prefix list for the service and whose target is the endpoint. Routing uses the most specific match, so S3 addresses match this route before they match the 0.0.0.0/0 route to the NAT gateway.
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids rtb-0123456789abcdef0DynamoDB works the same way with a different service name:
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.dynamodb \
--route-table-ids rtb-0123456789abcdef0Confirm that the route exists before you assume the traffic has moved:
aws ec2 describe-route-tables \
--route-table-ids rtb-0123456789abcdef0 \
--query "RouteTables[].Routes[?starts_with(GatewayId, 'vpce-')]"Limits of gateway endpoints
- Same region only. A gateway endpoint reaches S3 buckets in the VPC's own region. Traffic to a bucket in another region still goes through the NAT gateway, and the cross-region transfer charge applies on top.
- Not transitive. On-premises networks, VPC peering connections, and Transit Gateway attachments cannot use your endpoint. Each VPC needs its own.
- Route-table scoped. Only subnets whose route table includes the endpoint use it. A subnet that uses a different route table keeps paying NAT charges until you associate that table too.
- Prefix-list dependence. Anything that overrides routing for S3 addresses, such as a more specific route to a firewall appliance, can bypass the endpoint.
Controlling access with endpoint policies
An endpoint policy is an IAM-style resource policy attached to the endpoint. The default policy allows everything. Tightening it limits which buckets the VPC can reach through the endpoint, which also reduces the risk of data exfiltration.
Save this as s3-endpoint-policy.json:
{
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject", "s3:PutObject", "s3:ListBucket"],
"Resource": [
"arn:aws:s3:::example-app-bucket",
"arn:aws:s3:::example-app-bucket/*"
]
}
]
}Apply it:
aws ec2 modify-vpc-endpoint \
--vpc-endpoint-id vpce-0123456789abcdef0 \
--policy-document file://s3-endpoint-policy.jsonIf a workload suddenly fails after you apply a policy, check what it reads. Package repositories, ECR image layers, and AWS-managed buckets are all S3 buckets that your policy must allow.
Interface endpoints: a break-even calculation
Interface endpoints do cost money, so the decision needs arithmetic. An endpoint with ENIs in three AZs costs about 3 × $0.01 × 730 hours, or about $21.90 per month, before any traffic. Processing costs $0.01 per GB, against $0.045 per GB on the NAT gateway. Each gigabyte moved saves about $0.035.
The break-even volume per endpoint is the monthly fixed cost divided by the savings per GB:
- Three AZs: $21.90 ÷ $0.035 is about 626 GB per month.
- One AZ: $7.30 ÷ $0.035 is about 209 GB per month.
Below that volume, the endpoint costs more than it saves. Above it, the endpoint saves money and also takes load off the NAT gateway. These numbers are a first filter. Per-GB rates for interface endpoints drop at high volume, and some teams add endpoints for security reasons (no internet path at all) regardless of cost.
Services that commonly pass the break-even point are:
- CloudWatch Logs, for chatty applications or many nodes.
- ECR API and ECR Docker (
ecr.apiandecr.dkr), for frequent image pulls. - Secrets Manager, KMS, and STS, when many short-lived tasks call them.
- SQS and SNS, for high message volume.
Create an interface endpoint for CloudWatch Logs:
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Interface \
--service-name com.amazonaws.us-east-1.logs \
--subnet-ids subnet-0aaa1111bbb22222c subnet-0ddd3333eee44444f \
--security-group-ids sg-0123456789abcdef0 \
--private-dns-enabledThe security group on the endpoint must allow inbound TCP 443 from the clients that use it. A missing rule is the most common reason a new interface endpoint appears to do nothing: connections time out instead of being refused.
Private DNS
With --private-dns-enabled, the service's normal hostname (for example logs.us-east-1.amazonaws.com) resolves to the endpoint's private IPs inside your VPC. Applications need no code change. This requires enableDnsSupport and enableDnsHostnames to be true on the VPC. If either is off, the hostname still resolves to public addresses and traffic keeps flowing through the NAT gateway while you pay for an unused endpoint.
Interface endpoints and AZ placement
Place an endpoint ENI in each AZ that has clients. If a client in us-east-1a has no endpoint ENI in its AZ, its requests go to an ENI in another AZ and incur inter-AZ transfer charges, which can erase the savings. Conversely, adding ENIs in AZs with almost no clients adds $7.30 per month each for little benefit. Match the endpoint's subnets to where traffic originates.
Verifying that traffic moved
Do not rely on the console showing the endpoint as available. Check three things.
- Name resolution. From an instance in the subnet, run
nslookup logs.us-east-1.amazonaws.com. A private address in your VPC range means private DNS is working. - NAT metrics. In CloudWatch, compare the NAT gateway's
BytesOutToDestinationbefore and after the change. The drop should match the volume you expected to move. - Flow logs. Query VPC Flow Logs for traffic from your subnets to the NAT gateway's network interface. If a top talker is still a public S3 or service address, it still takes the NAT path.
A cost check one billing cycle later closes the loop. The usage type NatGateway-Bytes should fall. A new VpcEndpoint-Hours or VpcEndpoint-Bytes line item should appear only for interface endpoints. If VpcEndpoint-Hours grows and NatGateway-Bytes does not shrink, the endpoint is unused.
Common errors
- Forgetting a route table. The endpoint is associated with one route table while another subnet's table has none. Fix by listing all route tables used by private subnets.
- Cross-region S3 access. The gateway endpoint does not help. Use a bucket in the same region or accept the cost.
- Endpoint policy too strict. Builds and image pulls fail with access denied errors because they use buckets you did not list.
- Missing security group rule. Timeouts on port 443 to an interface endpoint.
- Private DNS conflicts. Only one endpoint per service per VPC can own the default hostname. A second endpoint with private DNS enabled for the same service fails to create.
- Endpoints everywhere by default. Dozens of low-traffic interface endpoints across several AZs can cost more per month than the NAT gateway traffic they replace.
Exercise
A private subnet in three AZs sends about 400 GB per month to CloudWatch Logs and about 2 TB per month to S3 in the same region, all through a NAT gateway. Using the list prices in this chapter, which endpoints would you add, and does the interface endpoint pay for itself?
Show answer
Add an S3 gateway endpoint first. It has no hourly or processing charge and removes about 2,000 GB × $0.045 = $90 per month of NAT processing. For CloudWatch Logs, an interface endpoint in three AZs costs about $21.90 per month plus 400 GB × $0.01 = $4 for processing, about $25.90 total. The NAT processing it replaces is 400 GB × $0.045 = $18. It costs more than it saves at this volume, so the endpoint does not pay for itself on cost alone. It would be justified only for security reasons, or if the log volume grew past roughly 626 GB per month. Placing the endpoint in fewer AZs lowers the fixed cost, but only if clients are in those AZs.