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

Is it possible to do AWS content routing through the CloudFront CDN?


I'm fairly new to the Amazon ecosphere, but I wanted to see if my idea would be possible, because I haven't found any documentation to support or deny it's possibility.

I am porting pieces of an existing web application to the AWS ecosphere. I'm using versioning to support a zero downtime upgrade experience. As such my directory structure on the S3 looks like this:

/apps/v1/images

/apps/v1/images/subdir

/apps/v1/javascript

/apps/v1/javascript/subdir

...

/apps/v2/images

/apps/v2/images/subdir

/apps/v2/javascript

/apps/v2/javascript/subdir

...

I have a CloudFront CDN serving these to edge locations. References to these files in HTML/JSP all use the CDN urls.

My problem is that there is a bunch of javascript referencing images in the form of relative urls "/images/image.png" as this path is routed by the original webapp configuration. Server side, this is handled easily by modifying the path at run time before serving to the client by prepending the CDN domain and version to the url. JS, run at the client side, is not as simple.

My question is if it is possible to do anything in the AWS ecosphere to customize the route information so that I can map "/images" to "/apps/v1/images" and then modify it at the appropriate time to map "/images" to "/apps/v2/images". I'd like to keep root routes so that regardless of the directory depth that the javascript might be deployed to, the "/images/..." relative path would succeed. I'm not entirely sure that it is possible, but if anyone knows for certain, I would greatly appreciate the info.


Solution

  • Although cloudfront or S3 won't change your URL, you can try to use S3 redirection to solve this. You could redirect the unversioned URLs to the versioned ones and, when the browser follows the redirection, it would get the correct object from cloudfront. Your deployment script could set the correct redirection rules.

    See:

    http://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html

    Amazon S3 object redirect