Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting multiple S3 origins under one CloudFront distribution

Is it possible to host multiple S3 origins under the same CloudFront distribution? Let's say I have bucket A and bucket B which both host static websites--could I add them both as origins to a distribution, and specify a path e.g. /alternate for the origin from bucket B so that visiting http://<distribution>/ took you to the website hosted by bucket A and visiting http://<distribution>/alternate brought you to the one hosted by bucket B? From what I've read so far it seems possible to have multiple origins, not necessarily multiple S3 origins.

like image 311
danielle Avatar asked Nov 23 '17 01:11

danielle


2 Answers

You can add multiple S3 origins to a CloudFront distribution

However, behaviors are locked to a specific origin. Behavior controls the path. So, if /static is used by Origin A/S3 A, you can't have /static go to /Origin B/S3 B

Someone suggested using multiple CloudFronts with the same CNAME. That does not work either. CloudFront will not allow the same CNAME to be used in multiple distributions.

Academically speaking, this may be possible through LambdaEdge or some other external process that is checking S3 A, and if it is unavailable (say - under maintenance while new code being pushed), it can go and update CF Behavior for /static to Origin B/S3 B.

I wish AWS came up with a more elegant solution to this use case. Like allowing same CNAME in multiple distributions, or providing some kind of R53 integration with S3 buckets (never tried putting two different S3 buckets behind R53 .. probably will not work because the host header would change)

like image 173
Yes Kay Avatar answered Nov 06 '22 21:11

Yes Kay


You can attach a Lambda function as a trigger to intercept CloudFront's routing. The implementation is quite involved; but would permit your specific use case.

Example Implementation: https://aws.amazon.com/blogs/networking-and-content-delivery/dynamically-route-viewer-requests-to-any-origin-using-lambdaedge/

Lambda@Edge: https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

like image 25
Jesse Bethke Avatar answered Nov 06 '22 21:11

Jesse Bethke