Search code examples
ruby-on-railsruby-on-rails-3amazon-web-servicesasset-pipelineamazon-cloudfront

Rails How to upload precompiled assets to Cloudfront


  • I'm using a Rails 3.2 app
  • I created a Cloudfront distribution with a S3 origin
  • I already change config.action_controller.asset_host to my cloudfront asset
  • I'm using capistrano

I want to know, how to upload my precompiled assets to Cloudfront every time I deploy


Solution

  • There is no need for the s3 bucket as Amazon Cloudfront now supports "custom origins". It used to be that you had to specify a s3 bucket as the origin, but now you can point your cloudfront distribution at your rails app.

    http://aws.typepad.com/aws/2010/11/amazon-cloudfront-support-for-custom-origins.html

    So, after you create a distribution that is pointed at your publicly accessible app, you set your asset host in the appropriate config file e.g. production.rb and you are off to the races.

    config.action_controller.asset_host = "url of your cloudfront distribution"
    

    Here is a half decent article on how to configure things:

    http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails

    Don't be confused with the bit about setting up CNAME recprds for an origin subdomain. This is only relevant if you want to use your own subdomain instead of amazons.