Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 354
kungfoo Avatar asked Jun 10 '13 00:06

kungfoo


People also ask

Can we attach S3 to EC2 instance?

To connect to your S3 buckets from your EC2 instances, you must do the following: 1. Create an AWS Identity and Access Management (IAM) profile role that grants access to Amazon S3.

What is an S3 proxy?

S3 proxy middleware for returning S3 objects Express apps. Useful for streaming media files and data files from S3 without having to configure web hosting on the entire origin bucket. You can explicitly override the cache headers of the underlying S3 objects. Added an option to remove bucket name from url.

Can API gateway connect to S3?

API Gateway sets the s3-host-name and passes the client specified bucket and key from the client to Amazon S3. (Optional) In Path override type /. Copy the previously created IAM role's ARN (from the IAM console) and paste it into Execution role. Leave any other settings as default.


1 Answers

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 :

backend s3 {
    .host = "s3.amazonaws.com";
    .port = "80";
}

backend app {
    .host = "localhost";
    .port = "8080";
}

sub vcl_recv {
    if (req.url ~ "^/api/.*") {
        set req.backend = app;
        set req.url = regsub(req.url, "^/api", "");
        set req.http.Host = "api.somedomain.com"; /* If your web app cares about host */
    } 
    else {
        set req.backend = s3;
    }
}

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.

like image 156
Johnny C Avatar answered Sep 27 '22 23:09

Johnny C