Using Amazon Cloudfront CDN with Rails 4.2.5.1 App

Objective

Learn how to setup CDN using Amazon Cloudfront for Rails 4.2.5.1 Apps.

Steps

Step 1

Go to Amazon Cloud Front and setup a CDN distribution. Click Cloud Front, provide the hostname
and accept the default values for other values. It will take about 15 to 30 minutes for the changes to take effect.

Amazon Cloudfront CDN

Step 2

In production.rb add :

 config.action_controller.asset_host = "amazon-provided-id.cloudfront.net"

You can also setup a CNAME like cdn.yourdomain.com to point to amazon-provided-id.cloudfront.net. I am using https for the entire site. Since I did not have wildcard SSL certificate, it did not work. So, I am using https://amazon-provided-id.cloudfront.net for now.

Step 3

After deploying your Rails app, view the page source. Verify the CDN. You should now see CDN for static assets. Something like this:

https://d1b5oz78c0udqh.cloudfront.net/assets/application-b5f55fdfb705b0fcdb2b9f6d9c28e97d.js
https://d1b5oz78c0udqh.cloudfront.net/assets/application-78676f382b46d6507c553c905c652a11.css
https://d1b5oz78c0udqh.cloudfront.net/assets/logo_300w-401a078fda85ae7672f3e18f1d8f9cfd.png

Summary

It took me half hour to setup CDN using Amazon Cloudfront for Ruby Plus. You can use WebPageTest to check the performance of your site before and after using CDN.

Web Page Test


Related Articles