So lets say that you are trying to distribute content around the globe, and you want to have as little latency as possible for every consumer who will be accessing your files. What do you choose as a service? You could use an EC2 instance, but this means everyone will be grabbing the content from one specific location at any given time. Same thing if you go with S3, if you only provide a direct link to a file from S3, everyone will hit the ‘one entrance’ bottleneck when accessing your content.
Well, CloudFront would be a great choice for you. With CloudFront, you can take a file in your S3 bucket (ex. mybucket/images/file.jpg) and instead of directly serving the file only from the bucket, you can choose to create either a streaming or download distribution, which utilizes a global edge network where your content is cached onto local edge servers near each user, which allows for a much better user experience while accessing your content.
Setting up a CloudFront Distribution is pretty simple. Navigate over to the CloudFront Management console, then click Create Distribution. You will need to choose either Download or Streaming. After choosing, it will prompt you for an Origin Domain name and also an origin ID. The easiest way to use CloudFront is to create a bucket in S3 that will distribute your content. If you click in the field for Origin Domain Name, it will auto populate each of your S3 buckets. It is also possible to create a custom origin domain name, but for simplicity sake we will just roll with an S3 bucket. Once choosing the bucket, an Origin ID will auto generate for you.
From here on out, you will just need to modify the Cache Behavior Settings and also the Distribution Settings. If you are just streaming normal HTTP content, you can pretty much leave everything at its defaults. However, if you want more information about when your files are being downloaded, I would advise to enable logging of your resources.
From this point, it will take CloudFront about 15 minutes to create your distribution. Here is the perfect opportunity to walk your dog, call your mom or dad, or argue with your significant other. After that time has passed, you should have a distribution that looks like dc8gtmXXXXXX.cloudfront.net
If your s3 bucket is set up to have an images/ and videos/ folders, and content within them, you can utilize your distribution by grabbing the domain name (dc8gtmXXXXXX.cloudfront.net) and then adding the file path to the files (dc8gtmXXXXXX.cloudfront.net/images/surfing.jpb). If your back end code or the link that your users are clicking on is pulling directly from this distribution, then CloudFront will be doing its job by serving its content via the glorious edge network.
That wraps up how to get up and running with CloudFront, until next time, signing out.