Search code examples
amazon-web-servicesamazon-cloudfront

Dynamically choose an S3 object to be served by CloudFront


Is it possible to have a custom origin server tell CloudFront to directly serve a file from an S3 bucket, similar to the way X-Sendfile works in Nginx? I'd like to avoid having to read the file from S3 and pipe it to CloudFront.


Solution

  • No, this isn't possible.

    Once the request is sent from CloudFront to the origin server, the only thing CloudFront will do (unless an error occurs, of course) is return the origin server's response to the requester.

    The only way that comes to mind in which this could really be possible would be if CloudFront followed HTTP redirects, which it does not do.

    If you want to return content from elsewhere once the request has arrived at the origin, you'll have to fetch it and stream it back... which will probably perform better than you expect, if the bucket is in the same region as the origin server and your code is tight. The latency to S3 within a region is very low and the available bandwidth is high. I have an application that does exactly this, many thousands of times each day on a little t2 instance, so it's certainly viable.

    Of course, with a single CloudFront distrubution, you can have multiple origins -- such as your server and S3. CloudFront can choose which origin will handle each request based on path pattern matching... but that's a static mapping, so I don't know whether it applies to what you're trying to do.