Open In App

AWS CloudFront Create-Invalidation

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS CloudFront is a CDN service that accelerates content delivery globally. Invalidation is a key feature of AWS CloudFront which allows you to promptly remove outdated content from the edge locations. In this article, you will explore what is AWS CloudFront, how CloudFront works, and the significance of invalidation. I will also provide the steps to perform invalidation in AWS CloudFront manage your content effectively and ensure seamless user experiences.

What is AWS CloudFront?

Suppose you have a website with awesome images, videos, and other content. Many people access the website. However, there is a certain problem many users of the website may face which is high latency. Due to high latency users will have a bad experience when accessing the website. To solve such a situation content delivery network (CDN) comes to the clutch. CDN is a network of interconnected servers(servers present in different edge locations ) that speeds up webpage loading. AWS CloudFront is an Amazon CDN service that delivers content, images, and videos with low latency and high transfer speed. AWS CloudFront can integrate with other AWS services to deliver content with high transfer speed and low latency.

You may now wonder how AWS CloudFront works. For example, let’s say you have a website that is running on an EC2 instance and it also uses AWS CloudFront for better content delivery. When a user tries to access your website the request first goes to AWS CloudFront. Then CloudFront checks whether the website is present in the edge location or not. If the website is present in an edge location then users can get the website content at a much lower latency or else CloudFront fetches the website and stores it in an edge location for future requests and then sends the website content to the users.

What is Invalidation in AWS CloudFront?

Let’s say you have a website running on an EC2 instance and it is accessed through the CloudFront distribution URL. When any request comes first to CloudFront, basically it will store the website in the cache or edge location. After storing the website in the cache any further requests made in the future to access the website, then CloudFront will deliver the content from the cache.

Now let’s say you have updated the contents of your website. You can now see the changes in the website when accessed through the Public IP of the EC2 instance but whenever you have tried to access the website using CloudFront URL then you will see no update in the website. This happens because CloudFront gives the website content from the cache. The cache does not automatically update on any change in the website. You have to perform invalidation manually to update the cache. Invalidation means removing the outdated content present in the cache and ensuring users receive the most up-to-date version of your website content.

Steps To Perform Invalidation in AWS CloudFront

Step 1: Create an EC2 instance. Use the below script to install Apache on your ec2 instance. This script will also create an index.html in /var/www/html/ path.

#!/bin/bash
sudo yum update -y
sudo yum install httpd -y
echo "In this article I will perform invalidation in AWS CloudFront" >/var/www/html/index.html
sudo systemctl start httpd
sudo systemctl enable httpd

After successful creation of EC2 instance you can then access the Public IP of EC2 instance to see the website content .

Step 2 : Copy the DNS of EC2 instance .

copy-dns-of-ec2Step 3 : Go to CloudFront and create a CloudFront distribution using EC2 instance as origin . First paste the copied DNS in the origin block . Then create a cache policy . Attach the cache policy to CloudFront distribution . At the end give the root object that is index.html .

Step 4 : Wait until the CloudFront distribution is deployed . After successful deployment access the website using CloudFront distribution URL .

accessed-through-cloudfront-url

Step 5 : Now make some changes in the website content . Connect EC2 instance and go inside the path /var/www/html/ . Edit the index.html .

sudo su
cd /var/www/html/
vi index.html

update-website-content

Step 6 : Now access the updated website through Public IP of EC2 instance .

access-through-public-ip-updated-content

Step 7 : Now try to access the website through CloudFront URL but you will see there are no updates in the website content .

can-not-access-the-updated-website

Step 8 : Go to CloudFront distribution and select invalidations .

select-invalidation

Step 9 : Create invalidation . Add /* to delete all the cache contents .

create-invalidation

Step 10 : Wait for sometime until the CloudFront distribution is deployed successfully . Then access the CloudFront URL , now you can see the updated website .

updated-content-finally-accessed

Conclusion

First we learned about the what is AWS CloudFront and how AWS CloudFront works . Then we have discussed about one of the important feature AWS CloudFront that is invalidation . We have also followed the steps to perform invalidation in AWS CloudFront . This step ensured that all the cache content will be deleted . Then finally accessed the updated website content.

AWS CloudFront Create-Invalidation – FAQs

Is It Possible Perform Partial Invalidation ?

Yes it is possible to perform partial invalidation in AWS CloudFront . You can mention specific paths to be invalidated which allows you to selectively remove and update content in the cache .

What Is Edge Locations ?

Edge locations are data centers which stores the copies of content like images , videos and any other website content . These are strategically located areas across the world to reduce content delivery latency and high transfer speed .

What Happens In The First Request To CloudFront ?

CloudFront checks whether the requested content is available in the cache or not ,if not then it stores the content in the cache first and then it delivers the content to user .

In What Cases Processing Invalidation Requests Takes Time ?

Processing invalidation requests can significantly increase if there are more number of edge locations used .

Is It Possible To Invalidate Content In Specific Edge Location Only ?

The answer is no. CloudFront does not provide such ability . Invalidation apply globally to all edge locations .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads