Distributions can be configured with a fairly wide array of options. Our recipe is going to be quite simple so that you can get up and running as quickly as possible. But we will talk through some of the more common configuration options:
- Origins: A distribution needs to have at least one origin. An origin, as the name indicates, is where your website content originates from your public-facing website. The properties you'll most likely be concerned with are:
- Origin Domain Name: This is the hostname of your public-facing website. The CloudFormation template we supply accepts this hostname as a parameter.
- Origin Path: It's possible to configure the distribution to fetch content from a directory or subfolder at the origin, for example, /content/images if you were using CloudFront to cache images only. In our case, we are caching our entire website, so we don't specify an Origin Path at all.
-
- Origin ID: This is particularly important when you are using nondefault cache behavior settings and therefore have configured multiple origins. You need to assign a unique ID to the origins so that the cache behaviors know which origin to target. There'll be more discussion on cache behaviors later.
- HTTP Port, HTTPS Port: If your origin is listening on nonstandard ports for HTTP or HTTPS, you would use these parameters to define those ports.
- Origin Protocol Policy: You are able to configure the distribution to talk to your origin via:
- HTTP Only
- HTTPS Only
- Match Viewer
The Match Viewer option forwards requests to the origin based on which protocol the user requested with in their browser. Again, we are keeping things quite simple in this recipe, so we'll be opting for HTTP Only.
- Logging: Keep in mind that because less traffic will be hitting your origin, fewer access logs will also be captured. It makes sense to have CloudFront keep these logs for us in an S3 bucket. This is included in the CloudFormation provided with this recipe:
- Cache behaviors: In this recipe, we'll configure a single (default) cache behavior, which will forward all requests to our origin.
- CloudFront: It allows you to get quite fine grained with the behaviors you configure. You might, for example, want to apply a rule to all the .js and .css files on your origin. Perhaps you want to forward query strings to the origin for these file types. Similarly, you might want to ignore the TTL the origin is trying to set for image files, instead telling CloudFront to cache for a minimum of 24 hours.
- Aliases: These are additional hostnames you want the distribution to serve traffic for. For example, if your Origin Domain Name is configured to loadbalancer.example.org, then you probably want aliases that look something like this:
- example.org
- www.example.org
The CloudFormation template provided with this recipe expects one or more
aliases to be provided in the form of a comma-delimited list of strings.
- Allowed HTTP Methods: By default, CloudFront will only forward GET and HEAD requests to your origin. This recipe doesn't change those defaults, so we don't declare this parameter in the provided template. If your origin is serving dynamically generated content, then you will likely want to declare this parameter and set its values to GET, HEAD, OPTIONS, PUT, POST, PATCH, and DELETE.
- TTLs (minimum/maximum/default): You can optionally define how long you'd like objects to stay in CloudFront's caches before expiring and being refetched from the origin. Again, we've opted to stick to CloudFront's default values to keep this recipe simple, so we've omitted this parameter from our template. The defaults are as follows:
- Minimum TTL: 0 seconds
- Default TTL: 1 day
- Maximum TTL: 1 year
- Price Class: By default, CloudFront will serve your content from all of its edge locations, giving you the maximum performance possible. We're going to deploy our distribution using the lowest possible price class, Price Class 100. This corresponds to edge locations in the United States, Canada, and Europe. Users from Australia would not benefit too much from this Price Class, but you're also paying less for it. Price Class 200 adds Asian regions, and Price Class All adds South America and Australia.
A comprehensive list and detailed explanation on which values can be specified when creating a CloudFront distribution can be found here at http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html.