S3 Proxy to EC2 Instance


I am running static hosting on s3 for my website. Eg. www.somedomain.com points to an S3 bucket.

I have a subdomain api.somedomain.com for my api, but it has been annoying to deal with cross domain issues. I want to map www.somedomain.com/api/… -> api.somedomain.com/… but without doing a full redirect (301) since I want to be able to post.

I understand that Cloudfront allows this behavior, but it’s a bit overkill since I do not need the CDN.

I have gotten the routing rules to work with a 301 redirect, but is there anyway to configure s3 to pass through my requests to ec2? Thanks!


To solve this problem all you need to do is switch the paradigm around. On your EC2 instance, just run a reverse proxy like varnish or nginx separately from your web app that routes traffic for http://www.somedomain.com/api/* to the web app (you can even rewrite the request url to remove the “/api” prefix) and all other traffic to S3. Configuring either nginx or varnish to do this is pretty straightforward (hours, not days).

Then switch your www.somedomain.com DNS record to point to your ec2 instance instead of S3.

Sample non-linted VCL for varnish to do this :

After this, you can get as fancy if you like, for instance using route 53 to health check your ec2 instance and fail over DNS lookups to your s3 bucket when it’s down, and configuring custom caching rules, etc. But none of it’s necessary to achieve the behavior you desire.

Leave a Reply