Are you trying to set up AWS CloudFront?
This guide is for you.
Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment.
CloudFront retrieves data from the Amazon S3 bucket and distributes it to multiple datacenter locations. The data delivers through edge locations.
When we request data, the nearest edge location routes, resulting in the lowest latency, low network traffic, fast access to data, and so on.
In this context, we shall look into how to setup AWS CloudFront.
How AWS CloudFront Delivers the Content ?
Moving ahead, let us see how AWS does content delivery.
1. The user accesses a website and requests an object to download. For example, an image.
2. DNS routes this request to the nearest CloudFront edge location to serve the user request.
3. There, CloudFront checks its cache for the requested files.
If found, returns it to the user otherwise does the following:
i. Initially, it compares the request with the specifications and forwards it to the applicable origin server for the corresponding file type.
ii. The origin servers send the files back to the CloudFront edge location.
iii. As soon as the first byte arrives from the origin, CloudFront forwards it to the user and adds the files to the cache in the edge location for the next time.
4. The object is now in an edge cache for 24 hours or so in file headers.
i. CloudFront forwards the next request for the object to the user’s origin to check the edge location version is updated or not.
ii. If the edge location version is updated, then CloudFront delivers it to the user.
iii. If the edge location version is not updated, then origin sends the latest version to CloudFront.
CloudFront delivers the object to the user and stores the latest version in the cache at that edge location.
Features of CloudFront Includes:
1. Fast: The broad network results in lowering latency, high data transfer rates, and low network traffic.
2. Simple: It is easy to use.
3. Can use with other AWS Services: We can easily integrate it with other AWS services, like Amazon S3, Amazon EC2.
4. Cost-effective: We pay only for the content that we deliver through the network.
5. Elastic: We need not worry about maintenance
6. Reliable: Its edge locations will automatically re-route the end-users to the next nearest location if required.
7. Global: It uses a global network of edge locations located in most of the regions.
How to Setup AWS CloudFront ?
Follow the steps below to setup AWS CloudFront:
1. Sign in to the AWS management console using https://console.aws.amazon.com/.
2. Upload Amazon S3 and choose every permission public.
3. Create a CloudFront Web Distribution using the following steps:
i. Open CloudFront console via https://console.aws.amazon.com/cloudfront/
ii. Click the Get Started button in the web section of 'Select a delivery method for our content page'.
iii. Create Distribution page opens. Choose the Amazon S3 bucket in the Origin Domain Name and leave the rest as default.
iv. Then we can see a default Cache Behavior Settings page. Keep the values as default and move to the next page.
v. A Distribution settings page opens. Fill in the requirements and click the Create Distribution button.
vi. The Status column changes from In Progress to Deployed. Enable the distribution.
Wait for around 15 minutes for the domain name to be available in the Distributions list.
How to Test the Links ?
Once done, CloudFront knows the location of the Amazon S3 server and the user knows the domain name associated with the distribution.
However, we can create a link to Amazon S3 bucket content with that domain name and have CloudFront serve it. This saves a lot of time.
Following are the steps to link an object:
Step1: Copy the following HTML code to a new file and write the domain-name that CloudFront assigned to the distribution in the place of the domain name.
Write a file name of the Amazon S3 bucket in the place of object-name:
<head>CloudFront Testing link</head>
<p><img src = “http://domain-name/object-name” alt = “test image”/>
Step2: Save the text in a file with .html extension.
Step3: Then open the web page in a browser to see if it is working correctly.
If not, then crosscheck the settings.
How to resolve HTTP 504 (Gateway Timeout) errors from CloudFront ?
Recently we had a customer who came across the error:
Amazon CloudFront distribution is returning an HTTP 504 (Gateway Timeout) error.
In order to troubleshoot HTTP 504 errors, we check the configurations on our firewall, security groups, and the origin server.
If we use a custom origin and can connect directly to the origin, but are getting 504 errors from CloudFront, then we increase the distribution’s origin response timeout.
By default, CloudFront allows the origin connection to open for 30 seconds.
However, if we take more than 30 seconds to process and return a response, CloudFront returns an HTTP 504 error.
To optimize performance, the connection time between CloudFront and the origin should be kept as short as possible.
Our Support Experts recommend keeping the origin response timeout value as low as possible.
In order to do that, follow the given steps:
1. Open the CloudFront console and then select the distribution.
2. Choose the Origins and Origin Groups tab and then select the origin.
3. Then select, Edit.
4. Update the Origin Response Timeout value.
5. Choose Yes and then Edit.
If we use persistent connections between CloudFront and the origin, we make sure that the origin keep-alive timeout value is equal to or greater than the origin response timeout.