Setup Amazon CloudFront (Content Delivery Network)

cloudfront

 

 

Amazon CloudFront is a content delivery web service. It integrates with other Amazon Web Services products to give developers and businesses an easy way to distribute content to end users with low latency, high data transfer speeds, and no minimum usage commitments.

Let’s take a real example: This website is hosted on a web server that’s located in some part of America. Now if we have a visitor from Singapore, the page loading time for him will be relatively high because of the geographic distance between Singapore and America. Had there been a mirror server in either India or Australia, the page would load much faster for that visitor from Singapore.

Now a content delivery network has servers across the world and they automatically determine the fastest (or the shortest) route between the server hosting the site and the end-user. So your page will be served from the server in Australia to a visitor in Singapore and from America for a visitor in UK. Of course there are other advantages but this example should give you a good idea of why we need a Content Delivery Network.

This is a simple step-by-step tutorial on how to serve your images and other media faster to end-users using the Amazon S3 + CloudFront Content Delivery Network:

Step 1

Create a S3 Bucket and upload your data on it.

Step 2

Get the S3 Fox add-on for Firefox and log-in to your Amazon S3 account by giving your access Key and ID. Now right-click your S3 bucket and select “Manage Distributions.”

 

1

 

Step 3

Next we’ll associate a CNAME (some easy to remember sub-domain name) with our S3 bucket so it become easy to link to the files from our web pages. In this example, I use the web address “cache.labnol.org” and then click Create Distribution.

 

2

 

Step 4

The status will change from “In Progress” to “Deployed”. Now copy the resource URL to the clipboard which is of the format xyz.cloudfront.net

 

3

 

Step 5

Log in to your account on your web hosting service’s website, and go to the DNS management page. Create a new CNAME record as shown in the screenshot. The CNAME name is the same sub-domain you created above while the CNAME value is your cloudfront.net URL generated by Amazon.

 

4

 

It may take a while (5 minutes?) for the domain change to propagate across different DNS servers. You may use online nslookup to confirm that the changes have been correctly propagated.

And you are done. Simply replace the image URLs in your web templates with the new sub-domain (in our case, cache.labnol.org) and your done.

###############################################################################################################################################
If you want to do it using AWS Console

Step 1

I am assuming that you have an AWS account. Sign In and go to CloudFront.

Step 2

Select Create Distribution

 

a1

 

Step 3. You will get a screen similar to this screenshot

 

a2

 

 

a3

 

 

a4
Let me explain one by one what these fiels are:

1. Origin Domain Name
Click in the field and specify the domain name for your origin—the Amazon S3 bucket or web server from which you want CloudFront to get your web content. The dropdown list enumerates the AWS resources associated with the current AWS account. To use a resource from a different AWS account, type the domain name of the resource. For example, for an Amazon S3 bucket, type the name in the format bucketname.s3.amazonaws.com. The files in your origin must be publicly readable.

2. Origin Path
Optional. If you want CloudFront to request your content from a directory in your Amazon S3 bucket or your custom origin, enter the directory name here, beginning with a /. CloudFront appends the directory name to the value of Origin Domain Name when forwarding the request to your origin, for example, myawsbucket/production. Do not include a / at the end of the directory name.

3. Origin ID
Enter a description for the origin. This value lets you distinguish multiple origins in the same distribution from one another. The description for each origin must be unique within the distribution.

4. Path Patern
The default cache behavior only allows a path pattern of * (forward all requests to the origin specified by Origin). To change the behavior or the routing for other requests (for example, *.jpg), add more cache behaviors after you create the distribution.

5. View Protocol policy
If you want CloudFront to allow viewers to access your web content using either HTTP or HTTPS, specify HTTP and HTTPS. If you want CloudFront to redirect all HTTP requests to HTTPS, specify Redirect HTTP to HTTPS. If you want CloudFront to require HTTPS, specify HTTPS Only.

6. Allowed HTTP Methods
Select the list of HTTP methods you want to allow for this cache behavior

7. Cached HTTP Methods
Select the list of HTTP methods you want to cache for this cache behavior

8. Forward headers
Select whether you want CloudFront to forward the headers sent in viewer requests and to cache your objects based on header values.

9. Object Caching
Select Use Origin Cache Headers if your origin server is adding a Cache-Control header to control how long your objects stay in the CloudFront cache. Select Customize to specify a minimum time that objects stay in the CloudFront cache regardless of Cache-Control headers.

10. Minimum TTL
The minimum amount of time (in seconds) that an object is in a CloudFront cache before CloudFront forwards another request to your origin to determine whether an updated version is available. The default time is 24 hours. To change the time that an object is in the cache, configure your origin to add a Cache-Control max-age directive

11. Forward Cookies
Select whether you want CloudFront to include all user cookies in the request URLs that it forwards to your origin (All), only selected cookies (Whitelist), or no cookies (None). If you select Whitelist, add the names of the cookies to the Whitelist Cookies field. Note that Amazon S3 and some HTTP servers don’t process cookies.

12. Forward Query Strings
Select whether you want CloudFront to include query strings in the request URLs that it forwards to your origin. If you return different versions of an object based on the query string, select Yes. Otherwise, select No.

13. Smooth Streaming
If you want to use Microsoft Smooth Streaming for on-demand streaming, click Yes

14. Restrict Viewer Access
Choose whether you want CloudFront to require users to access your content using a signed URL or a signed cookie. Additional configuration is required. In the Help, see “Serving Private Content through CloudFront.”

15. Price Class
Select the price class associated with the maximum price that you want to pay for CloudFront service. If you select a price class other than All, some of your users may experience higher latency.

16. Alternate Domain Name
Optional. If you want to use your own domain name (for example, www.example.com) instead of the CloudFront domain name (for example, d1234.cloudfront.net) for the URLs for your files, specify up to 100 CNAMEs. Separate CNAMEs with commas or put each on a new line. You also need to create a CNAME record with your DNS service to route queries for www.example.com to d1234.cloudfront.net.

17. SSL Certificate

18. Default Root object
Optional. The object that you want CloudFront to return (for example, index.html) when a viewer request points to your root URL (http://www.example.com) instead of to a specific object in your distribution (http://www.example.com/index.html).

19. Logging
Select whether you want CloudFront to log all viewer requests for files in your distribution. You are charged for access logs.

20. Bucket For Logs
Click in the field and specify the Amazon S3 bucket in which you want CloudFront to save web access logs. The dropdown list enumerates the buckets associated with the current AWS account. To use a bucket from a different AWS account, type the bucket name in the format bucketname.s3.amazonaws.com.

21. Log Prefix
Optional prefix for the names of log files (for example, cf-logs/). The slash is optional but recommended to simplify browsing your log files.

22. Cookie Logging
Select whether you want CloudFront to include cookies in access logs

23. Comment
Optional. Any comment about the distribution.

24. Distribution State
When the distribution is enabled, CloudFront processes viewer requests for the content associated with this distribution. When the distribution is disabled, CloudFront does not accept any requests for the content associated with this distribution.

146 Comments