Moving on, Route53 is yet another awesome service provided by AWS that is specially designed for distributing and delivering content across the globe and it is called Amazon CloudFront. For some reason, CloudFront is not one of the most commonly used services in the AWS service family; nevertheless, it is still a good alternative to S3 when it comes to distributing content geographically. How does it all work? Well, it's quite simple actually! To begin with, the first thing that you need to do is configure an Origin Server. An Origin Server is nothing more than a place from where CloudFront retrieves the files or content for distribution. Origin Servers can be anything from an S3 bucket to even an EC2 instance running in a VPC. Once an Origin is defined, the next step involves the upload of objects to your Origin Server. Objects can be anything from images, media files, to even web pages! Yes, you heard it right! Web pages as well! Anything and everything that can be served over the HTTP protocol or a supported version of the Real Time Messaging protocol (RTMP). Refer to the following diagram as an example of Origin Server:
The next step is perhaps the most important one and involves the creation of a CloudFront distribution. The distribution describes which Origin Server to use when a user initiates a request to an image or a media object from your application or website. This step will provide your objects with a new CloudFront URL, which you need to substitute in your application. For example, a standard image URL may look something like this once it is referenced by CloudFront: http://112233.cloudfront.net/myimage.jpg
. CloudFront then sends this distribution configuration to all of its specialized edge locations that are spread out across the world.
These edge locations are nothing more than small data centers where CloudFront caches copies of your objects and keeps them ready for distribution. When a user accesses or requests the object from your application or website, the DNS will route it to the nearest edge location. CloudFront will then check its cache for the requested file in the edge location. If the file is present in the cache, then it is returned to the user. If not, then CloudFront will request the file from the Origin Server and cache it in its edge location. This delivery method comes in really handy when you have a lot of data that gets requested or accessed by users on high frequency basis. It also improves the overall performance of your application and website as well as increasing reliability and availability.
Now that the basic concepts are out of the way, let's look at some simple steps using which you can get your own CloudFront distribution up and running in a matter of minutes! First up, access the CloudFront option from the AWS Management Console, as shown here:
This will bring up the CloudFront Management dashboard where you will be able to create your distributions as well as perform a wide variety of monitoring and analysis tasks, such as view Cache Statistics, Popular Objects based on frequency of requests, Usage, alarms, and so on.
To create your first distribution, select the Create Distribution button. This will bring up a page where you need to select the distribution's delivery method. Currently, CloudFront supports two modes of delivering content; they are as follows:
For this little demonstration, I'll be using one of the S3 buckets that we created in the previous chapter (Chapter 9, Working with Simple Storage Service). The bucket contains a simple video that I wish to distribute using CloudFront's edge locations, so the obvious choice for the distribution selection in this case will be RTMP. In case you do not have a bucket by now, follow the simple steps outlined in Chapter 9, Working with Simple Storage Service, and create a bucket, upload a video to it, and make sure that the bucket has public permissions provided to it.
Next, fill out the following details to create your very own RTMP distribution:
Once your configurations are completed, select the Create Distribution option. The distribution will take a couple of minutes to change from pending state to enabled. Once it's ready, you can use the CloudFront Domain Name to retrieve your objects from the Origin Server:
You can edit your distribution's settings anytime by simply selecting the distribution entry and clicking on Distribution Settings as shown. You can also Disable or Delete the state of your distribution using this dashboard. Follow the same steps and you can also configure a web distribution for your application, so give that a try as well.
Here are some key takeaways and best practices to keep in mind when working with CloudFront: