Sub topic 6
Cutting NAT traffic with VPC endpoints
This chapter assumes you know how NAT gateways bill (an hourly charge plus a per-GB data processing charge). It also assumes you can read a VPC route table and run the AWS CLI.
What an endpoint changes
A private subnet usually sends AWS API traffic through a NAT gateway. For S3 or DynamoDB, that means you pay NAT data processing on every gigabyte. A VPC endpoint gives the subnet a private path to the service, so the traffic skips the NAT gateway.
Two endpoint types
| Type | Services | Cost | How traffic reaches it |
|---|---|---|---|
| Gateway | S3 and DynamoDB only | No hourly or data processing charge | Route table entry |
| Interface (AWS PrivateLink) | Most other AWS services and partner services | Hourly charge per endpoint per AZ, plus a per-GB charge | Network interface in your subnet, reached through DNS |
Check current rates on the pricing pages for your Region. Interface endpoints are not free, so they pay off only when the NAT data charges they replace are larger than the endpoint charges.
Create a gateway endpoint
This command creates an S3 gateway endpoint and adds its route to one route table.
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-0123456789abcdef0AWS adds a route whose destination is the S3 prefix list (a managed list of S3 address ranges) and whose target is the endpoint. The route is more specific than the 0.0.0.0/0 route to the NAT gateway, so S3 traffic uses it. Nothing changes in your application. Repeat the command for DynamoDB with com.amazonaws.us-east-1.dynamodb.
To confirm, list the route table and look for a route with a vpce- target:
aws ec2 describe-route-tables \
--route-table-ids rtb-0123456789abcdef0 \
--query "RouteTables[].Routes[].[DestinationPrefixListId,GatewayId]" \
--output textWhen to use an interface endpoint
Use an interface endpoint when a private workload sends a lot of traffic to a service such as ECR, CloudWatch Logs, or Secrets Manager. Pulling container images through NAT is a common example. A few rules help control cost:
- Create the endpoint only in the AZs where workloads run. Each AZ adds an hourly charge.
- Share endpoints across many workloads in the VPC instead of creating one per application.
- Compare monthly NAT bytes to that service against the endpoint cost before you commit.
Common errors
- The route table is not associated. A gateway endpoint only affects the route tables you list. A new subnet that uses a different route table still goes through NAT.
- The bucket is in another Region. A gateway endpoint covers S3 in the same Region only. Cross-Region requests still use NAT.
- On-premises or peered clients cannot use it. Gateway endpoints are not reachable through VPN, Direct Connect, peering, or Transit Gateway.
- Requests fail after adding a policy. An endpoint policy or a bucket policy that uses
aws:SourceVpcecan deny traffic you meant to allow. Check these first when you seeAccessDenied. - An interface endpoint seems unused. Confirm that private DNS is enabled and that the endpoint's security group allows inbound 443 from your clients.
Check the result
Compare the NAT gateway's BytesOutToDestination CloudWatch metric before and after the change. Use the Cost Explorer NatGateway-Bytes usage type to confirm the monthly drop.
Exercise
A private subnet sends 20 TB a month to an S3 bucket in the same Region through a NAT gateway. You create a gateway endpoint for S3, but the NAT bytes barely change. Name two likely causes.
Show answer
The endpoint was not added to the route table used by that subnet, so S3 traffic still follows the default route to the NAT gateway. Alternatively, the bucket is in a different Region, which a gateway endpoint does not cover. Check the route table first, then the bucket Region.