Thursday, May 26, 2016

Setting up AWS CloudFront for Magento

Here are some steps I jotted down for setting up AWS CloudFront as a CDN for the 3 asset directories that are used by Magento installations. I am assuming your Magento application servers are behind an ELB.

SSL certificate upload to AWS

Install aws command line utilities.

$ pip install awscli

Configure AWS credentials

Create IAM user and associate it with the IAMFullAccess policy. Run ‘aws configure’ and specify the user’s keys and the region.

Bring SSL key, certificate and intermediate certificate in current directory:

-rw-r--r-- 1 root root 4795 Apr 11 20:34 gd_bundle-g2-g1.crt

-rw-r--r-- 1 root root 1830 Apr 11 20:34 wildcard.mydomain.com.crt

-rw------- 1 root root 1675 Apr 11 20:34 wildcard.mydomain.com.key

Run following script for installing wildcard SSL certificate to be used in staging CloudFront setup:

Custom SSL client support: Only Clients that Support Server Name Indication (SNI)

Domain name: eg7ac9k0fa3qwc.cloudfront.net

Behaviors

/media/* /skin/* /js/*

Viewer protocol policy: HTTP and HTTPS

Allowed HTTP methods: GET, HEAD

Forward headers: None

Object caching: Use origin cache headers

Forward cookies: None

Forward query strings: Yes

Smooth streaming: No

Restrict viewer access: No

Compress objects automatically: No

DNS setup

cdn.mydomain.com is a CNAME pointing to the CloudFront domain name above eg7ac9k0fa3qwc.cloudfront.net

Magento setup

This depends on the version of Magento you are running (1.x or 2.x), but you want to look for settings for the Base Skin URL, Base Media URL and Base Javascript URL, which are usually under System->Configuration->General-Web. You need to set them to point to the domain name you set up as a CNAME to CloudFront.