Search code examples
amazon-web-servicesamazon-s3amazon-cloudfront

AWS CloudFront: How to map a Behavior Pattern to the Base of a Bucket


I'm using content to access multiple S3 buckets. The S3 Buckets are already being used for a number of different websites, which limits my ability to move the content around.

Let's say I have MY_BUCKET1, MYBUCKET2, MYBUCKET3, and all three have files in the base of the bucket.

I want to be able to set it so these files can be referenced at:

CF_CRAZY_URL.cloudfront.net/BUCKET1/file1.mp4
CF_CRAZY_URL.cloudfront.net/BUCKET2/file2.mp4
CF_CRAZY_URL.cloudfront.net/BUCKET3/file3.mp4

But when I choose the bucket as the origin in CloudFront, it looks like the way the behaviors work for folder mapping it looks for the files for the first behavior rule at:

CF_CRAZY_URL.cloudfront.net/file1.mp4

Which I'm pretty sure then makes it so it can't find: file2.mp4, since its not in that first bucket where its looking first.

Is there a way to do this?

Basically, I want: CF_CRAZY_URL.cloudfront.net/BUCKET1 to map to https://s3.amazonaws.com/BUCKET1/

Rather than: CF_CRAZY_URL.cloudfront.net/ mapping to https://s3.amazonaws.com/BUCKET1/


Solution

  • CloudFront uses the first matching path pattern to determine which origin server the request will be sent to.

    It then sends the path exactly as it was received from the browser, unless an origin path is set on the origin, in which case the origin path is prepended before sending the request to the bucket.

    Neither of these standard behaviors will do what you want -- with CloudFront, you can't remove anything from the path requested by the browser before sending it to the origin.

    The only way to modify the path (to remove /BUCKETx from the beginning) before the bucket receives the request will be with a Lambda@Edge Origin Request trigger. Lambda@Edge, however, is still in preview, so it's not yet generally available.