AWS Logo
Menu

How to configure a split-traffic VPN to access private S3 buckets

This post will help you route Amazon S3 traffic through your VPN.

Published Sep 14, 2024
Last Modified Sep 16, 2024
If you maintain a VPN that allows users to access private VPC resources, then you might also want to use it to allow access to private S3 buckets. This post will help you configure your VPN accordingly.
This is only relevant if your VPN configuration is splitting the traffic in a way where only part of the traffic goes through the VPN. If you route all of the user's network traffic through the VPN then this post doesn't apply to your use case.
If you have a VPC Interface Endpoint set up (with private DNS enabled), then likewise this is should not be an issue. However, since interface endpoints cost $0.01/per AZ/per hour, I prefer using Gateway Endpoints since they are FREE and can even lower your AWS bill.
Now that we have defined the specific use case when this applies, please join me in figuring out what IP ranges you need to configure to be sent through the VPN.
Your bucket policy should look something like this (taken from this documentation page):
As mentioned in my other post, Four surprising IPv6 gotchas with Amazon S3, you may also want to add a NotIpAddress to the condition to allow for access over IPv6. Here's what that would look like (replace the IPv6 CIDR range with the CIDR range for your own VPC):
With this in place, you are now required to route the traffic through your VPN to access this S3 bucket.
In this example I am configuring a WireGuard VPN client config using the AllowedIPs setting. This might look like the following to send both IPv4 and IPv6 traffic through to the VPN:
However, since we're using a VPC Gateway Endpoint, we need to manually configure the IP ranges that Amazon S3 use here as well. Amazon publishes an ip-ranges.json file that makes it easy to automate some of this work.
Start by downloading the ip-ranges.json file:
My S3 bucket and VPC are both located in us-west-2, so I can use these jq commands to retrieve the IP ranges that are appropriate for me:
All you need to do is add all of these IP ranges to your AllowedIPs list. Here is what it looks like for me:
With these changes, my VPN configuration allows me to access both my private VPC resources, and my private S3 buckets. It's a little bit messy, but it works!
If you have experience in how this is simplified using a VPC Interface Endpoint, please let us know in the comments. It would be an interesting read.
Please let me know if this helped you, or if anything needs clarification. :)
 

Comments