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
-rw------- 1 root root 1675 Apr 11 20:34

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

$ cat

aws iam upload-server-certificate --server-certificate-name WILDCARD_MYDOMAIN_COM_FOR_PROD_CF --certificate-body file:// --private-key file:// --certificate-chain file://gd_bundle-g2-g1.crt --path /cloudfront/prod/

After uploading the SSL certificates, they will be available in drop-downs when configuring CloudFront for SSL.

Apache Cache-Control headers setup
  • Add these directives (modifying max-age accordingly) in all Apache vhosts, both for port 80 and for port 443
 <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Cache-Control "max-age=604800, public"

CloudFront setup
  • Origin: prod ELB (
  • Alternate domain name:\
  • Custom SSL client support: Only Clients that Support Server Name Indication (SNI)
  • Domain name:
  • 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
  • is a CNAME pointing to the CloudFront domain name above

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.

Base Skin URL:
Base Media URL:
Base Javascript URL:

More in-depth Magento-specific instructions for integrating with CloudFront are available here.

No comments: