Amazon CloudFront is a web service that speeds up distribution of your static and dynamic web content, such as .html, .css, .js, and image files, to your users. CloudFront delivers your content through a worldwide network of data centers called edge locations. When a user requests content that you're serving with CloudFront, the request is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.
If the content is already in the edge location with the lowest latency, CloudFront delivers it immediately.
If the content is not in that edge location, CloudFront retrieves it from an origin that you've defined—such as an Amazon S3 bucket, a MediaPackage channel, or an HTTP server (for example, a web server) that you have identified as the source for the definitive version of your content.
Some terms to be cleared
Origin:
The origin is the source of the content that CloudFront distributes. It can be an Amazon S3 bucket, an Amazon EC2 instance, an Elastic Load Balancer (ELB), or a custom HTTP server.
CloudFront retrieves content from the origin and caches it at edge locations for faster delivery to users.
Origin Error:
An origin error occurs when CloudFront cannot successfully fetch content from the configured origin server.
This can happen due to various reasons, such as connectivity issues with the origin server, misconfigured permissions, or invalid responses from the origin server.
Cache Behavior:
Cache behaviors define how CloudFront handles requests for different paths or types of content.
They specify criteria such as the path pattern, HTTP methods, query strings, and TTL (Time-to-Live) values for caching.
Cache behaviors allow you to customize caching rules for different types of content or URLs.
Cache Hit:
A cache hit occurs when CloudFront receives a request for content that is already cached at an edge location.
CloudFront can serve the requested content directly from the edge cache, reducing latency and offloading traffic from the origin server.
Cache Miss:
A cache miss occurs when CloudFront receives a request for content that is not cached at the edge location.
In this case, CloudFront forwards the request to the origin server to fetch the content and caches it at the edge location for future requests.
Edge Location:
An edge location is a data center in the CloudFront global network where content is cached and served to users.
CloudFront uses a network of edge locations located worldwide to deliver content with low latency to end users.
Distribution:
A distribution is a collection of edge locations that deliver content to users.
Each distribution has a unique domain name (e.g., d12345abcdef.cloudfront.net) that you can use to access cached content.
Invalidation:
Invalidation is the process of removing objects from CloudFront edge caches before they expire.
You can create invalidation requests to purge specific files or paths from CloudFront caches, ensuring that users receive the latest version of content from the origin server.
Viewer Request:
A viewer request is an HTTP request made by an end user to CloudFront.
Viewer requests are typically served from edge locations whenever possible, reducing latency and improving performance for users.
Viewer Response:
A viewer response is the HTTP response returned by CloudFront to the end user.
Viewer responses can be served from edge caches if the requested content is available, or fetched from the origin server in case of cache misses.
Let's move for practical steps
First we will discuss the cloudfront with ec2 instance with application load balancer inside vpc
step1: create vpc and subnets and internet gateway
step 2: Attach internet gateway to the vpc
step3: Create route table and add internet gateway to the route table's route and associate sub nets as well
step4: Create ec2 instance with these security group policy
Note:- Connecting to the ec2 by ssh, install the nginx
sudo apt update
sudo apt install apache2
sudo systemctl status apache2
sudo systemctl enable apache2
sudo systemctl status apache2
step5: Create target group with peering the ec2 instance and create application load balancer with target group that we created
step6: Then now run load balancer dns
step 7: Then create the distribution in the cloudfront with the cache policy
Then finally copy the cloudfront dns then we get sucess
Now, we will perform cloudfront with the s3 bucket
Here, we will create the private bucket and access the page inside it throught the cloudfront
steps
step1. Create the bucket and upload the index.html page containing some information
step 2 create the Distribution and copy the bucket policy and click the Go to S3 bucket permission to update
step 3 Then save the changes by pasting
Then finally copy the dns name of the cloud front
We can get from cloudfront.
Note: while creating and updating the cloudfront it takes some time aroud 1-2 minutes.
There are also some other topics like error for error if not found, cache invalidation to get new fresh object from the origin source and so on. I will update to this blog later with my time. so keep in touch
Thanks for reading..........................