SSL and CloudFront

  • Getting an SSL certificate
  • Setting up CloudFront
  • Cache Invalidation
  • Domain names with Route 53

While Heroku automatically gives us an encrypted connection to our API via HTTPS (which is just a normal HTTP connection plus an SSL security layer--people will use the two terms interchangeably), S3 does not. That's because there's nothing sitting between the user and the files.

The only way to solve this problem is to 1) set up a Node server to handle requests and run this on EC2, or 2) run our requests through a proxy like CloudFront. Since we're going to need to communicate to our backend by a secure route, we're going to need to set up CloudFront and route our connections through it.

We will also need an SSL certificate. This used to be a long, tedious, and painful process that often cost hundreds of dollars a year (Godaddy still charges \$299/yr for it), but thanks to AWS, you get it for free and it's super easy to set up!

Getting an SSL certificate

As mentioned above, this used to be complicated and expensive, but now it's really easy. Log in to aws.amazon.com and open up the Certificate Manager. Click the Request a certificate button in the top left.

Then you want to request what is called a wildcard certificate and a naked domain certificate. That gives you the ability to protect all the single-level sub-domains of a particular site, so it would cover www.phoenixchat.io, api.phoenixchat.io, etc. Assuming the domain is phoenixchat.io, the requested domains would be *.phoenixchat.io and phoenixchat.io. Click Review and request and Confirm and request.

It often takes a few minutes to validate, but that's it. You're done.

Setting up CloudFront

CloudFront is something you can put in front of your app to make load times faster and enable HTTPS. It does aggressive caching in certain locations around the world so your app loads faster since your app won't have to request assets every time the page loads.

Within CloudFront, click on Create Distribution followed by Create under the Web section. From Origin Domain Name you can select the S3 bucket you just made from the dropdown. Make sure you add domain you would like to reference in the Alternate Domain Names section (e.g. phoenixchat.io). Change Viewer Protocol Policy to Redirect HTTP to HTTPS and add index.html as the Default Root Object. Then under SSL Certificate, choose Custom SSL Certificate and select the certificate you just created. Then create the distribution.

This will take a while to provision (often minutes to hours). While we're waiting, we can explain how cache invalidation works.

Cache Invalidation

Cache invalidation is a big subject and an especially complicated one. Fortunately for us, our use case is pretty simple. Because CloudFront caches files based on their names, if we make a change to bundle.js, CloudFront doesn't know that it needs to clear the cache and bring in the new version of the app. The act of clearing this cache is called an invalidation.

Since we're injecting our HTML file with hashed bundle and style files, all we have to do is tell CloudFront not to cache our index.html file, since every time we update the app, our bundle and style files will have a new hashed suffix.

The way to tell CloudFront to never cache the index.html file is by setting a new behavior. Select the distribution by clicking on the ID. Then go to the Behaviors and create a new behavior. Add index.html to the Path Pattern. Then change Object Caching to Customize, and set the Maximum and Default TTL (Time To Live--which is how long the object is cached) to 0.

Domain names with Route 53

If you've purchased your domain from another provider, your setup is going to be different. Assuming you used Amazon's Route 53, this is how you would point your domain to the CloudFront distribution you just created.

After purchasing a domain, go to Hosted zones in the left panel and select Create hosted zone for the domain you would like to change.

Now choose Create record set. Then in the panel on the right, toggle the Alias to Yes and select the CloudFront distribution you just created. Keep in mind that the distribution might not be provisioned yet, in which case you'll have to wait (if you go to CloudFront and it says "in progress", it isn't done). Then click Create and you're done. Now if you go to the domain you purchased (e.g. https://phoenixchat.io) it should look like you expect with HTTPS enabled.

Keep in mind, these changes can often take hours to propagate. If your changes don't immediately appear, don't automatically assume there is an error. Unfortunately there is no way around this and it makes your code really difficult to debug.

Previous: ESLint and Airbnb Style GuideNext: Refactoring Redux