AWS Cloudfront is a CDN (Content Delivery Network) managed service that is provided by AWS. Cloudfront receives the data from S3 bucket/load balancer/mediapackage and mediastore and delivers it to multiple locations for fast retrieval using a kind of network of data centers called as edge locations.
What is Edge Location?
Cloudfront edge location is where end users access services located at AWS. These are located in the major cities around the world and are specifically used by CloudFront (CDN) to distribute content to end users to reduce latency. It is like a front-end for the service we access which is located in AWS cloud.
In this demo we are taking the example of s3 as a source of data/content.
Procedure for delivering the content using Cloudfront
The user hits the particular website for accessing or downloading the content, that particular DNS will route the user’s request to the geographically nearest edge location. The Cloudfront will look for the cache of that particular content, if it finds that particular content over there then it will return the content back to the user from the edge location itself.
If it doesn’t find that particular content over there i.e. edge location then it will take the request to the origin server and it will bring the data/content from there to the edge location and will deliver the content to that user as well.
If another user next time will hit the website/request for downloading that particular content then the cloudfront will deliver the data from edge location because it has been already brought to the edge location to reduce the latency issues.
The particular object will stay in a cached location for 24 hours.
Features of Cloudfront
- Cost effective
Web and RTMP Distributions
We can use web distributions to serve the following content over HTTP and HTTPS
- Static and Dynamic Download Content
- Multimedia Content. You can’t serve Adobe Flash multimedia content over HTTP and HTTPS
- A live event such as meeting, concert or conference in a real time
For web distribution your origin can be s3 bucket or HTTP server, for example a web server
RTMP Distribution stream media files using Adobe Real Time Messaging Protocol (RTMP) and adobe media server
An RTMP Distribution must use an Amazon S3 bucket as origin.
How to setup AWS Cloudfront?
Step 1: Go to your aws console and click on service and go to cloudfront service
Step 2: Click on Create distribution
Step 3: Under Web distribution click on Get Started
Step 4: In Origin Domain Name, provide your s3 bucket name which you want to work as source.
In default cache behavior uses REDIRECT HTTP TO HTTPS for security.
Step 5: Scroll down and you will see a price class option, In price class you can choose the price class that corresponds to the maximum service that you want to pay for the CloudFront service i.e. for your edge locations.
Step 6: Scroll down and click on Create Distribution, and wait for some time as it will take 10-15 min. for creating the distribution.
After creation it will look like
Once it is completed click in the distribution link and provide the name of any of the content of your s3 bucket that you have set as an origin after /
Now copy the domain name of your cloudfront distribution and provide the object name after your domain name with /
As you can see in the screenshot below we have entered our domain name in the browser and after that we have entered our object name from s3 bucket.
Example URL: AssignedCloudfronturl/S3Object as shown below.
Invalidate Cloudfront Cache
Suppose somehow you are not able to see your latest changes in your website that was serve through cloudfront, the main reason would be that cloudfront receiving the data from cache, in order to resolve this we need to invalidate cache setting.
How to invalidate cloudfront cache
Step 1: Go to your cloudfront
Step 2: Select your distribution and move to Invalidation
Step 3: Now click on create validation
Step 4: Provide the path at which you want to invalidate your cache.
Example:- If you want to do invalidation on your full website just type /* in the path and It will do invalidation on your full website on all of the contents.