Reputation: 4223
I have two S3 buckets behind a CloudFront distribution.
One bucket is the default behavior origin; one is an additional behavior under a specific path pattern.
The problem is that CloudFront uses the whole path to request the bucket object, which requires the exact path to exist as part of the object name in the bucket.
Path pattern auth/*
requires an auth/index.html
object if I request .../auth/index.html.
If I change the path, I would have to change all the object names in the bucket.
Is there a way around this?
Upvotes: 0
Views: 534
Reputation: 4223
Thanks to a comment, I found a solution with Lambda@Edge.
If you used the path pattern auth/*
in a CloudFront behavior so that all URLs starting with auth/
are routed to a specific S3 bucket, and you want to save all your files in that bucket without an auth/
prefix. Then you need to add this edge Lambda to your CloudFront behavior:
const prefix = "auth/"
exports.handler = async (e) => {
const { request } = e.Records.pop().cf;
if (request.uri.contains(prefix))
request.uri = request.uri.replace(prefix, "");
return request;
});
The event type is origin request.
On a request to auth/index.html
CloudFront will fetch index.html
from your associated bucket and not auth/index.html
Since origin requests are only sent to the S3 bucket when a cache miss happens, this should be a quite cheap solution.
Upvotes: 1