How to Ensure EC2 Accesses S3 via VPC Endpoint Instead of Public Internet?

August 22, 2025 4 answers
Asked by 6m diving board
I'm troubleshooting an issue where my EC2 instance is accessing an S3 bucket, but I believe the traffic is still going through the public internet instead of utilizing the VPC endpoint. I've confirmed that there's an S3 endpoint defined and the EC2's subnet is correctly routed to this endpoint. Here's a portion of the policy I'm using: { "Version": "2012-10-17", "Statement": [ {...} ] } I ran "dig s3.amazonaws.com" and it returns public IPs, which I thought wouldn't happen if everything was set up correctly. I also checked the debugging logs from "aws s3 ls" but couldn't find a trace of the endpoint being used. I noticed that the "Private DNS names enabled" option for my VPC endpoint is set to "No". I'm unsure if I've missed a configuration or if there's something wrong with my bucket policy. Any advice would be greatly appreciated!

Answers

frisbee on grass
What region is your setup in, and have you tried running `dig` with the specific S3 region endpoint? Sometimes, if your EC2 instance has special network settings defined, it can alter the expected behavior. It'd also be good to check if you've set any proxy variables affecting your calls to S3.

6m diving board: I'm testing in the us-east-1 region. I did the `dig` command with the regional S3 URLโ€”still returned public IPs. Someone mentioned that even if those addresses are public, traffic routing might still be handled privately.

Reply to this answer
~20 questions asked
It sounds like you're using an S3 gateway endpoint. These endpoints do some routing magic to avoid using public IPs, so it's normal for DNS queries to still yield public addresses. Just ensure that your subnet's route table includes an entry for the S3 prefix, which directs traffic through the endpoint. If you want to double-check, you could temporarily remove any routes to the internet; if your access still works, then it's definitely going through the endpoint. You might also consider tightening your bucket policy to explicitly deny access unless the request comes from your VPC or endpointโ€”this can further confirm your setup works as intended.

~!TakeACloserLook: Got it! If I want to make sure my traffic is secure and not via the public internet, are there safer methods to validate this without affecting other instances?

Reply to this answer
m8x1.25
Did you link your subnetโ€™s route table to the VPC endpoint? If it's not associated, traffic wonโ€™t route correctly. Also, be cautiousโ€”if you have a NAT gateway configured, that might be causing issues since traffic could be routed outside your VPC. You could consider making adjustments to your setup with that in mind.

6m diving board: Thanks for the heads up! I just watched a video on this, and it seems like the NAT gateway is indeed part of the problem. Iโ€™ll have to review my configurations carefully because changing it might disrupt other instances.

Reply to this answer
TERRACE GARDEN
You mentioned you're using a gateway endpoint, but just to clarify, you could also look into using an interface endpoint. Those can be specified in SDK/API calls if necessary. However, normally, the DNS should resolve correctly with the proper configuration youโ€™re using now, especially with PrivateLink.

~20 questions asked: Good idea! But I know with S3 Gateway endpoints, DNS should inherently handle that. It's always worth checking, though!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions