This Blog has moved from Medium to blogs.tensult.com. All the latest content will be available there. Subscribe to our newsletter to stay updated.

CloudFront does the caching of data in the Edge Locations to speed up the access of the website across the world. Once the content is cached to the CloudFront, it stays there till Time To Live expires which makes static pages ideal for the situation. Most of the cases a website is the collection of static and dynamic pages. So we have to build a strategy to accommodate both. We will do an experiment with the help of multiple Cache Behaviors. A Cache Behavior is the set of pattern-based rules acting on the filename, file extension or URL name. Each cache behavior can contain path patterns, origin server name, TTL related parameters, etc.

About the Experiment

Created two EC2 based web servers, one server with dynamic contents and other with static contents. It is not mandatory that you have to create multiple servers, but I have done so. The index.php file I have created is like below, which displays the current time, it means that it is not ideal for CloudFront caching. Any time I click I have to get the current time. The other server is configured with static pages and is meant for caching in the CloudFront. I have copied .jpg files in that server. Instead of EC2 based web server, you can consider S3 bucket web hosting for holding static pages. This experiment is only for testing multiple cache behavior and in a production environment it may be more complex. The index.php is,

<?php

echo "The time is " . date("h:i:sa");

?>

Procedure

Select CloudFront service from AWS web console and click “Create Distribution”.

2) We configure with minimum required parameter. Enter Origin Domain Name as the public DNS name of the Server. I have chosen the server which has a dynamic file index.php file.

3) You can’t change the Path Pattern (Default) since this is the only server at this point of time.

4) Set the TTL. This is very important to set the TTL to 0, which makes sure that it will not cache the content and contact Origin Server for each time you refresh the pages. It makes sense for the above .php file which returns the current time.

5) Default Root Object I selected as index.php. You can configure your own web server's document root and many other parameters listed here. Click Create Distribution.

6) Once Cloud Front is deployed you can see the status like below.

7) Now you have to create second Cache Behaviour, ie. for static data. Double click the ID (above screenshot), then select “Origins” menus and click “Create Origin”.

8) Enter minimum parameter ie, Origin Domain Name as second servers DNS name. Leave other parameters unchanged.

9) In Behaviors menu click Create Behavior with all required parameters like below. Enter the Path Patten of the static file you want to cache. Set Default TTL in seconds according to how much time you want to cache the files.

Testing

In the CloudFront service console, General Menu of the distribution finds out the value Domain Name filed. Now type the URL in the web browser and refresh multiple times and you can observe that each refresh the current time is displayed. But the .jpg in cached for a period of Default TTL. ( You can also test static caching by copying the index.php to the other server with a different name and entering the full filename in Path Pattern field of Cache Behavior. Now if you type CloudFront URL/the_newfilename.php and refresh multiple times, it will not return different value for the current time since it caches for many hours)

Conclusion

CloudFront caches the data in the edge locations to speed up the access of the website from the client. We wanted to access the static and dynamic contents using a single CloudFront endpoint. While static pages are cached while dynamic pages are spared from caching. We have achieved with the help of multiple Cache Behaviors.

Related Blog: