Power Sitecore with CloudFront

There are already a few good blog posts which describe how to use CloudFront in combination with Sitecore. All the posts I saw used CloudFront as a solution to offload images and other static resources on a different domain, and then change Sitecore to load static resources from this different domain. Sitecore itself will not be using CloudFront. This post will describe a different approach where Sitecore sits behind CloudFront as well. It will also discuss the advantages of using this approach.

Before diving into using CloudFront for Sitecore lets quickly look at the approach of just offloading static resources to CloudFront on a separate domain. Following flowchart shows this in more detail:

This is an good solutions and it solves the issue of making the site faster by delivering large resources quicker from edge locations globally, at scale and in a more cost effective manner.

Advantages of using CloudFront with Sitecore

In the previous setup Sitecore pages are not using CloudFront, and therefore cannot take advantage of some useful CloudFront features like:

  • Security at the Edge: CloudFront provides protection against most common DDoS attacks. It can also be used to block traffic from certain countries or only allow traffic from a specific set of countries
  • SSL Offloading/compression: SSL can be terminated at CloudFront and CloudFront could gzip compress the responses. Even when not offloading SSL, SSL will be more efficient with CloudFront as it will reuse connections between itself and the origin
  • Restricting access to premium content: if you have premium content which users need to pay for then you can use signed URLs or signed cookies to limit access. This is much easier to standup then a fully fledged IDP but still provides enterprise grade security.
  • Error Pages: Sitecore can handle most common errors better than CloudFront can. However some catastrophic errors are better handled outside of Sitecore, for example a scenario where the app pool is crashing or hanging or a scenario where Sitecore is unable to handle the incoming load. CloudFront supports customized error pages depending on what gets returned from the origin. More info can be found here.

Host Sitecore behind CloudFront overview

This article will will describe a different solution where everything is fronted by CloudFront, and there is only one URL. This way all CloudFront features can be leveraged for both the Sitecore pages as well as all other resources. The origin in this case will be the Sitecore ELB. Below diagram explains this in more detail:

Now that Sitecore pages are also fronted by CloudFront they could be setup to be cacheable, see number 3. Recommendation is to never do this when using Sitecore as all analytics and personalization will not apply and register the page view when CloudFront returns it form cache.

Setting up Sitecore behind CloudFront

This section walks through some of the important configuration when setting up Sitecore’s ELB as the origin of the load balancer.

Set Sitecore ELB as origin

When creating the CloudFront distribution the ELB which hosts Sitecore needs to be set as the origin, see below screenshot:

Allowed HTTP Methods

Since all traffic goes through CloudFront it is important to allow all HTTP verbs which your site uses, e.g. POST, PUT, DELETE etc. See below screenshot for this option in the UI.

Configure caching

There are 2 ways to control how long content stays in the CloudFront cache, more details can be found here

  • Using headers to control cache duration for individual objects: CloudFront will honor the Cache-Control and Expires response headers to control how long objects stay in the cache. This is recommended by CloudFront and personally I also prefer this approach as everything can be controlled from the Sitecore site in IIS.
  • Specifying the amount of time that CloudFront caches objects: In this case the values for time to live are specified in CloudFront. To use this select “Use legacy cache settings” and then “Customize” under object caching, see screenshot below

Understanding other CloudFront settings

There are many other settings in CloudFront and using them correctly can be critical for your site to work properly and to gain maximum benefit from CloudFront. This page provides more detail about all the different settings and how they work.