I added a VPC endpoint to my VPC using CloudFormation, and allowed s3 usage. The routes are visible in the AWS console, but not in the local routing tables of the EC2 instances:
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.29.4.129 0.0.0.0 UG 0 0 0 eth0
169.254.169.254 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
172.29.4.128 0.0.0.0 255.255.255.128 U 0 0 0 eth0
How do I verify that the EC2 instances in the VPC actually uses the VPC endpoint for S3, and not the available internet connection?
I guess the straightfwd way is to actually probe those routes.
You can traceroute to s3 and see if the NAT Gateway's internal IP is anywhere in the output (eg. the first hop).
First, check the NAT Gateway internal IPs in the console.
Example output with the endpoint set - no gateway IP shown. This is what you want to see.
Example output of a different destination, going via NAT (see the first hop)
I have found a method to verify the VPC endpoint usage.
aws ec2 describe-prefix-lists
; for Windows PowerShell,Get-EC2PrefixList
The result should contain the the VPC endpoints prefix list ID in the attribute
PrefixListId
.For additional verification, you can apply the following policy to an S3 bucket:
with your vpc ID instead of vpc-121212. You should then only be able to access the S3 bucket from the given VPC
You can turn on S3 logging and check if the files are being accessed from your private IP rather than public. If your logging shows private IPs are accessing the buckets you've configured it correctly. Goodluck!
I would recommend to launch ec2 instance (with IAM role allowed to list s3 buckets) in subnet without internet access.
Basically only 2 active rules in route table (your VPC subnet range and s3 endpoint).
Connect to instance and run command:
It should fail with timeout because boto by default will create request to global s3 url (s3.amazonaws.com).
should list your buckets in us-east-1 region (vpc router will route your request to s3.us-east-1.amazonaws.com).
Your instance forwards packets destined to S3 to the local gateway, and from there the VPC 'router' forwards them to the S3 endpoint. No client configuration or knowledge is required.
You could configure the S3 endpoint with a very restrictive set of ACLs such that it denies all requests and observe your client receive the failure as well.
@m-glatki's answer (which for some reason is the accepted one) is factually incorrect.
First of all, you have to explicitly enable an ec2 VPC interface to even be able to perform the
aws ec2 describe-prefix-lists
call, otherwise you will get a timeout.Secondly, even if you can call that api, it won't tell you whether you are routing your traffic through that endpoint. It just provides details about a specific prefix list (PL) in the current region.
What you have to do is associate an S3 VPC endpoint to the subnet's route table and make sure your EC2 instance or service's security group allows egress connectivity to via that endpoint (you should be fine with the default "allow all" egress rule). This will route S3 traffic via the endpoint, even if you have a NAT gateway attached to it.
You can verify that your traffic is not routed through the NAT by checking its associated cloudwatch logs (See BytesOutToDestination, BytesOutToSource, BytesInFromDestination, and BytesInFromSource metrics)
Also check S3 bucket logs as @michael correctly pointed out.
Elaborating on @m-glatki solution, add a policy on the bucket that restricts S3 access to a particular VPC Endpoint:
You will only be able to list bucket contents from a process that uses the VPC endpoint. Otherwise, you will receive a message:
An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
To get the VPC Endpoint ID, use this command:
aws ec2 describe-vpc-endpoints
See this link