Search code examples
amazon-web-servicesamazon-cloudfrontamazon-elastic-beanstalk

Statics deployment with AWS Elastic Beanstalk and AWS Cloudfront


I'm using AWS Elastic Beanstalk for deploying my PHP application, which is great because I can deploy the application with a single git push action. In this application I have static files such as JS and images.

Is there any automatic way to deploy these static files to Amazon Cloudfront? Of course I can write a deployment script but I want to know if someone is using any AWS provided solution.


Solution

  • You can specify your application URL as the origin for your Cloudfront distribution (rather than using a S3 bucket). This will make Cloudfront transparently start serving your static assets. A couple of caveats:

    1. You'll need to adjust the routes to your assets to use absolute URLs. For example <img src="/images/hello.png" /> will need to become <img src="//xxxxxx.cloudfront.net/images/hello.png" />. (Note the odd-looking scheme. That allows the same URL to be used via both HTTP and HTTPS connections).

    2. You'll need to implement some type of resource versioning method so users are always getting the latest revisions. So <img src="//a42532.cloudfront.net/images/hello.png" /> actually becomes <img src="//a42532.cloudfront.net/images/hello-3456464234.png" /> (or something similar). Otherwise you will have to manually invalidate the distribution each time you update your static files, which takes forever and can get very costly since Amazon bills for invalidations. With Git, you could write a commit hook that adds the UNIX epoch to your assests' filenames before the code is pushed to Beanstalk.