Call AWS APIs privately with VPC endpoints
Choose gateway or interface endpoints and fix a private call broken by policy, routing, DNS or a security group.
By Invicly Tech, , in AWS Cloud Engineering. About 12 minutes to read.
In short
VPC endpoints let resources in private subnets call AWS services such as S3, SQS or Secrets Manager without going through a NAT gateway or the internet. Gateway endpoints exist only for S3 and DynamoDB and work by adding a route; interface endpoints put a network interface with a private IP into your subnets, powered by AWS PrivateLink. When a private call fails, the cause is almost always one of four things: DNS, routing, a security group or a policy.
Think of it like this: A NAT gateway is leaving the building to post a letter at the public post office; a VPC endpoint is a private mail slot in your own wall that goes straight to the service.
Explained simply
Your worker runs in a private subnet. It still needs to talk to AWS services: read files from S3, take messages from SQS and fetch a secret. Those services have public endpoints on the internet, so without help the worker would need a NAT gateway to reach them.
A VPC endpoint gives the worker a private road instead. There are two kinds. A gateway endpoint works only for S3 and DynamoDB. It adds a route to your route table and costs nothing. An interface endpoint places a small network card with a private IP address in your subnet, and traffic to the service goes to that address.
Interface endpoints rely on DNS. With private DNS turned on, the normal name such as sqs.eu-west-1.amazonaws.com resolves to the private address inside your VPC. Your code does not change at all. It just starts using the private road.
When a private call breaks, check four places. Does the name resolve to a private address? Does the route table include the gateway endpoint? Does the endpoint's security group allow port 443 from the worker? Do the policies on the endpoint, the resource and the role all allow the action?
Press Play on a diagram to walk through it one step at a time.
- S3 and DynamoDB only
- Adds a prefix-list route to route tables
- No hourly or data charge
- Only usable from inside that VPC
- Many AWS services and partner services
- Network interface with a private IP per subnet
- Charged per hour per AZ and per GB
- Protected by a security group; reachable over VPN or peering
- DNS
- Private DNS turned off
- VPC DNS hostnames or support disabled
- Name still resolves to a public IP
- Routing
- Gateway endpoint not on this subnet's route table
- No NAT and no endpoint for the service
- Security group
- Endpoint SG does not allow 443 from the caller
- Policy
- Endpoint policy too narrow
- Bucket policy requires a different endpoint
- Role lacks the permission
- 1Turn on VPC DNS settings
DNS support and DNS hostnames
- 2S3 gateway endpoint
Associate with private route tables
- 3Interface endpoints
SQS and Secrets Manager in both zones
- 4Endpoint security group
Allow 443 from the worker SG
- 5Tighten policies
Endpoint policy plus aws:SourceVpce on the bucket
- 6Remove NAT if unused
Verify calls still work, then delete it
In depth
Gateway endpoints: a route, not an address
A gateway endpoint for S3 or DynamoDB does not create anything in your subnet. Instead, AWS adds a route to each route table you select. The destination is a managed prefix list (for example pl-6da54004 style IDs) that holds the service's IP ranges in that Region, and the target is the endpoint. Because that route is more specific than 0.0.0.0/0, traffic to S3 takes the endpoint even when a NAT route exists.
The limits matter. A gateway endpoint works only for traffic that starts in that VPC, in the same Region. Traffic from your office over VPN, or from a peered VPC, cannot use it. For those cases, S3 also offers interface endpoints. Gateway endpoints have no hourly or data charge, so add one for S3 in nearly every VPC that touches S3.
- Services: S3 and DynamoDB only
- Associate it with every private route table
- Same Region and same VPC only
- No security group: control access with policies
Interface endpoints and private DNS
An interface endpoint creates one elastic network interface in each subnet you choose, ideally one per zone. Each gets a private IP and a security group. AWS also gives the endpoint its own regional and zonal DNS names.
The smooth option is private DNS. When it is on, AWS creates a hidden private hosted zone so the standard service name resolves to the endpoint's IPs for anything using the VPC resolver. It needs two VPC settings: enableDnsSupport and enableDnsHostnames, both true. If either is off, the name keeps resolving to public addresses and, with no NAT, calls simply hang.
Interface endpoints are billed per hour in each zone plus per GB. Five services across three zones is fifteen endpoint-hours every hour, so in a lab create only the ones you need.
- Worker to VPC DNS resolver: Resolve sqs.eu-west-1.amazonaws.com1Resolve sqs.eu-west-1.amazonaws.com
- VPC DNS resolver to Worker: 10.0.11.87 (private DNS answer)210.0.11.87 (private DNS answer)
- Worker to Endpoint interface: HTTPS 443, allowed by endpoint SG3HTTPS 443, allowed by endpoint SG
- Endpoint interface to Amazon SQS: Request over PrivateLink, policy checked4Request over PrivateLink, policy checked
- Amazon SQS to Worker: 200 OK with MessageId5200 OK with MessageId
Endpoint policies and resource conditions
Every endpoint has a policy. The default allows everything, which means the endpoint does not limit anything. You can replace it with a narrower policy, for example allowing only s3:GetObject and s3:PutObject on the uploads bucket. This stops a compromised worker from copying data to an attacker's bucket through your endpoint.
The other direction is a condition on the resource. A bucket policy can deny any request whose aws:SourceVpce is not your endpoint ID, so the bucket can only be reached from inside your VPC. Be careful: that also blocks you in the console unless you add an exception for an admin role.
Remember the rule from IAM: every policy in the path must allow the call, and any explicit deny wins. An endpoint policy never grants access on its own.
- Default endpoint policy: full access
- Narrow it to your buckets and actions
- Bucket condition aws:SourceVpce pins access to one endpoint
- Test with a role that should fail, too
A short troubleshooting order
Start by separating network problems from permission problems. A timeout or connection refused points to the network. An AccessDenied error with a message means the request arrived and a policy said no.
For network faults, work outward from the caller. Resolve the name with dig and see whether you get private addresses. For gateway endpoints, check the subnet's route table for the prefix-list route. For interface endpoints, check that the endpoint has an interface in the caller's zone or subnet and that its security group allows TCP 443 from the caller's security group. For permission faults, read the error: newer AWS messages name the policy type that denied the call, such as an identity-based, resource-based or VPC endpoint policy. CloudTrail also records the vpcEndpointId on requests that came through an endpoint.
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Add an S3 gateway endpoint~35m
- 2Add an interface endpoint~35m
- 3Turn on private DNS~35m
- 4Write an endpoint policy~35m
- 5Diagnose a failing private call~35m
Key points
- Use a free gateway endpoint for S3 and DynamoDB traffic from the same VPC; it also cuts NAT data charges.
- Interface endpoints need private DNS so the normal service name resolves to the endpoint's private IPs.
- Private DNS only works when the VPC has both DNS support and DNS hostnames turned on.
- An endpoint policy limits what can pass through the endpoint; it never grants permissions by itself.
- A timeout usually means network (DNS, route, security group); AccessDenied means policy.
Key terms
Tap a card to flip it. 0/6 known
Examples
# Run on an instance in the private subnet
dig +short sqs.eu-west-1.amazonaws.com
# Expect 10.0.x.x addresses if the interface endpoint and private DNS work
aws sqs list-queues --region eu-west-1 --cli-read-timeout 10
# Timeout -> check DNS, endpoint security group, subnets
# AccessDenied -> check role, endpoint policy, queue policySplit the problem in two first: does the name resolve to a private IP, and does the call reach the service?
Add an S3 gateway endpoint and prove S3 traffic skips NAT
Prerequisites: the VPC from the previous topic with a private route table. Step 1: create the endpoint and attach it to the private route table. Step 2: check that the route table now shows a pl- destination. Step 3: from an instance in the private subnet, run aws s3 ls on a test bucket. Evidence: the command works, and if you removed NAT it still works, proving the endpoint carries the traffic. The endpoint is free, but delete it during teardown so the VPC can be removed.
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc1234example \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.eu-west-1.s3 \
--route-table-ids rtb-0priv1example
aws ec2 describe-route-tables --route-table-ids rtb-0priv1example \
--query "RouteTables[].Routes[].[DestinationPrefixListId,GatewayId]"
# Expect a pl-... row pointing to vpce-...
# Cleanup
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids vpce-0s3exampleFixing an SQS call that hangs in a private subnet
The worker hangs on ReceiveMessage. Step 1: dig shows a public address for sqs.eu-west-1.amazonaws.com, so DNS is the first suspect. Step 2: the interface endpoint exists, but private DNS is off. Turning it on fails, because the VPC has DNS hostnames disabled. Step 3: enable DNS hostnames, then private DNS. dig now returns 10.0.11.x. Step 4: the call still hangs, so check the endpoint security group: it allows 443 only from 10.0.1.0/24. Change it to allow 443 from the worker security group. Evidence: messages arrive.
aws ec2 modify-vpc-attribute --vpc-id vpc-0abc1234example --enable-dns-hostnames
aws ec2 modify-vpc-endpoint --vpc-endpoint-id vpce-0sqsexample --private-dns-enabled
aws ec2 authorize-security-group-ingress --group-id sg-0endpointexample \
--protocol tcp --port 443 --source-group sg-0workersexample
dig +short sqs.eu-west-1.amazonaws.com # now private 10.0.x.xQuick reference
- Gateway endpoint: S3 and DynamoDB, a route to a prefix list, free
- Interface endpoint: an ENI per subnet, a security group, billed per AZ-hour and per GB
- Private DNS needs enableDnsSupport and enableDnsHostnames on the VPC
- Timeout = network (DNS, route, SG); AccessDenied = a policy said no
- Endpoint policies only narrow access; they never grant it
- aws:SourceVpce in a bucket policy limits access to one endpoint
- Gateway endpoints do not work from on-premises or peered VPCs
Common mistakes
- Adding an S3 gateway endpoint but associating it with the public route table instead of the private one.
- Creating an interface endpoint with the default security group, which does not allow 443 from your workload.
- Writing an endpoint policy that lists only your bucket, which also blocks the AWS-owned buckets some tools download from.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List which endpoint type you would use for S3, DynamoDB, SQS, CloudWatch Logs and ECR image pulls from private subnets.
- Exercise 2medium
Write an S3 gateway endpoint policy that allows only GetObject and PutObject on the bucket example-uploads.
- Exercise 3hard
A private Fargate task fails to start with an error pulling its image. There is no NAT. Diagnose the likely gaps and give the fix list.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do VPC endpoints replace NAT gateways?
- They replace NAT for traffic to AWS services. If your code also calls outside APIs, you still need NAT or a proxy for that traffic.
- Are interface endpoints worth it in a small lab?
- Often not. Each costs money per zone per hour. For a short lab, use the free S3 gateway endpoint and a single NAT gateway, then delete both.
- Does my code need a special endpoint URL?
- Not if private DNS is on. The SDK keeps using the normal service name, which now resolves to the private addresses.