Create S3 folder alias or redirect url path to S3 folder

Viewed 2520

Is it possible to have an alias for a folder in S3 bucket?

Here's what I'm trying to achieve: I want to host a website with documentation of various projects and upload documentation for new versions of these projects to folders in S3 bucket.

Bucket content would be something like this:

<projectA>
|---<0.1.0>
    |---index.html
|---<0.2.0>
    |---index.html    
<projectB>
|---<1.0.0>
    |---index.html
|---<1.1.0>
    |---index.html
index.html

Now I'd like the newest version of any project to be aliased as 'latest'. So when I upload version 1.2.0 of projectB I remove alias 'latest' from version 1.1.0 and add the alias to newly uploaded '1.2.0'.

And url to index page of projectB/1.2.0 would be xxx.cloudfront.net/projectB/latest/index.html

Is something like that even possible? Maybe with some redirects in cloudfront? (or route53)

2 Answers

Two possible ways with CloudFront.

The easiest is to set up a CloudFront pointing at your bucket, and use the "Origin Path" value. In your example, you set Origin Path to projectB/1.2.0 and call your distribution "latest.mydomain.com". When you update the files, go to origins and change the Origin path.

A more complex version is to use Lambda functions. Hook a lambda function to the "Origin Request" as that is specifically designed to rewrite URLs. https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

Worst case solution would be just to have another folder in each project called as latest and then whenever there is a new version of documentation release to any project, also upload that index.html to latest folder as well along with specific version folder.

Probably, have to invalidate in CloudFront for the origin path project /latest to make them effective immediately instead of waiting for TTL to invalidate the older cache.

Related