CloudFront is a CDN (Content Delivery Network). The CloudFront retrieves data from the Amazon S3 bucket and distributes it to multiple datacenter locations. The data that delivers through a network of data centers is called edge locations.
The nearest edge location is routed when the user requests for data, so we will get some benefits like the lowest latency, low network traffic, fast access to data, etc.
A picture will help you to clarify how this actually works, so take a look at the image shown below. We have stored our content or data in an S3 bucket located in a region in N.Virginia(us-east-1), and we have users located around the world who access that content.
As the arrows show, whenever a user requests content, the request must go over the public internet to the source location–the S3 bucket in N.Virginia(us-east-1). Depending on the user’s location, content delivery can take a long time.
The delays in the response or in content delivery might even cause some user requests to bounce and return an error from the page.
Now imagine that we set up CloudFront in front of the S3 bucket. In the following image, you can see that there are no longer requests traversing the globe to get to our content.
Instead, requests are routed to the ones that are closest in terms of delivery speed. These are also called “least latent” Edge Location. As shown with the green arrows, CloudFront then quickly serves cached content directly to the requesting user nearby.
CloudFront retrieves the content from the S3 bucket origin if it is not cached with an edge server. Instead of the public internet, the content traverses the AWS private network and CloudFront optimizes the TCP handshake. Hence, the request and content return are still much faster than access across the public internet.
So by using CloudFront will fetch data from CloudFront data centers instead of S3. The following Image is showing us the same thing.
Let’s start the creation of CloudFront distribution.
1. Steps for creating a CloudFront distribution
Note: I already created S3 bucket in AWS so If you don’t have any website on S3 bucket then plz create it first
2. Restrict our S3 bucket policy to Cloud Front
IMPORTANT: Your bucket will have a different name.
{
"Version": "2012–10–17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::2020–05–21-mybucket/*"
},
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E2V8GJ8FKJPGFQ"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::2020–05–21-mybucket/*"
}
]
}
{
"Version": "2012–10–17",
"Statement": [
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E2V8GJ8FKJPGFQ"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::2020–05–21-mybucket/*"
}
]
}
3. Steps for testing that we successfully locked down S3 from public view
Browse to that URL and you should now see the index.html page. And your CloudFront is ready.
Happy Coding 😃