Day-19-Aws
Understanding AWS CloudFront: A Comprehensive Guide to Content Delivery Networks
TLDR: This blog post explores AWS CloudFront, a content delivery network (CDN) service, detailing its purpose, functionality, and integration with Amazon S3 for hosting static websites. It includes a demonstration by cloud expert Piyush on how to configure CloudFront with S3, highlighting the benefits of reduced latency, enhanced security, and cost efficiency.
In this blog post, we will delve into AWS CloudFront, a powerful content delivery network (CDN) service offered by Amazon Web Services (AWS). This guide will cover the fundamental concepts of CloudFront, its integration with Amazon S3 for hosting static websites, and a step-by-step demonstration by cloud expert Piyush.
cloudFront → CDN → Content Delivery Network |
CDN |
user upload a reel from Australia |
Instagram maintained all reel to central storage (USA) |
if user from India try to access that reel → USA → it will req multiple hops (multiple Router) to access the reel → high latency |
so CDN comes to picture → CDN create local copy of central storage |
Indian user access CDN → to reduce latency |
in general there will be multiple CDN for specific region → [multiple CDN in india ,multiple CDN in usa, multiple CDN in china , multiple CDN in North Koeria] |
S3 (store static file → audio,video,img) + Cloud Front (CDN) → to host static website |
reduce cost → no direct read and write to S3 bucket |
low latency → copy is cached to edges (CDN) , which is near to user |
high security → no one can access s3 directly |
create s3 bucket |
search → s3 →bucket (card) → create bucket |
General Config (card) → [name: trainwithshubam.com (name should be same as custom domain) → region : → ] |
Bucket ownership (card) → ACL disabled |
Block public access setting (card) → block all public access (checked) [only CDN will be able to access S3] |
Bucket versioning (card)→ enable (to recover deleted s3) |
Advance Setting (card) → obj lock : disable |
—> create bucket |
enable Static website hosting , upload file |
click on created bucket → properties (tab) → Static website hosting → edit → [Static website hosting: enable → type: host static website→ index document: index.html→ error document: error.html ] → save changes |
click on created bucket → object (tab)→ object (card) → upload —> upload (page) → files and folder (card) → add files → select file from laptop → upload |
click on created bucket → properties (tab) → Static website hosting → right click on url , open on new tab |
403 forbidden error |
create distribution for cloud front → get content from s3 bucket |
search → cloud front → create distribution → |
origin(card) → domain : trainwithshubam.com → name (auto populated) : → access : legacy access identity → [create a new OAI → name (auto populated) → create] → bucket policy : yes (automatically change above created bucket policy→ cloud front can get content from s3 bucket ) |
WAF (card ) → disable |
— set default option — |
—> create distribution |
access website created by distribution |
click on created distribution → general (tab) → details(card) → domain name: —- →copy it and open it in new tab |
now able to view content of website |
What is CloudFront?
AWS CloudFront is a managed service that provides a solution for content delivery networks (CDNs). A CDN is a system of distributed servers that deliver web content to users based on their geographic location. The primary goal of a CDN is to improve the speed and efficiency of delivering content to users.
The Problem CloudFront Solves
When users access content from a central server, they may experience latency due to the distance between the server and the user. For instance, if a user in India tries to access an image stored on a server located in North Virginia, the request must travel through multiple routers, increasing the loading time. This can lead to a poor user experience, especially for platforms like Instagram or Amazon, where quick access to content is crucial.
CloudFront addresses this issue by caching content at edge locations around the world, allowing users to access content from a server that is geographically closer to them. This significantly reduces latency and improves the overall user experience.
How Does CloudFront Work?
When a user uploads content to a platform like Instagram, that content is stored in a central location. However, with CloudFront, copies of that content are cached at various edge locations. For example, if a user in India wants to access an image uploaded by a friend in Australia, CloudFront will serve the image from the nearest edge location in India rather than the central server in the US. This process minimizes the number of hops the request must take, thereby reducing latency.
Demonstration: Integrating CloudFront with S3
To illustrate how to configure CloudFront, Piyush will guide us through the process of integrating it with Amazon S3, which is an object-based storage service used to host static files.
Step 1: Create an S3 Bucket
Log into your AWS console and navigate to S3.
Click on "Create Bucket" and name it according to your domain (e.g., tutorialswithpiyush.com).
Ensure that the option to block all public access is checked to enhance security.
Enable bucket versioning to recover deleted objects if necessary.
Click on "Create Bucket" to finalize.
Step 2: Enable Static Website Hosting
Go to the properties of the newly created bucket.
Scroll down to the "Static website hosting" section and click "Edit."
Select the option to host a static website and specify the index and error pages (e.g., index.html and error.html).
Save the changes.
Step 3: Upload Content to the Bucket
Click on the bucket and select "Upload."
Add your static files (e.g., index.html, CSS files) and hit upload.
Step 4: Create a CloudFront Distribution
Search for CloudFront in the AWS console and click on "Create Distribution."
Choose the S3 bucket you just created as the origin domain.
Create an Origin Access Identity (OAI) to restrict public access to the bucket.
Update the bucket policy to allow access from the OAI.
Configure caching settings and enable SSL if necessary.
Click "Create Distribution" and wait for the deployment to complete.
Step 5: Accessing the Content
Once the distribution is deployed, you will receive a CloudFront URL that points to your S3 bucket. Users can access the content through this URL, benefiting from reduced latency and enhanced security.
Conclusion
AWS CloudFront is an essential tool for improving the delivery of content across the globe. By integrating CloudFront with S3, users can host static websites efficiently while ensuring quick access to content. The demonstration by Piyush provides a clear understanding of how to set up and utilize CloudFront effectively.
For those interested in cloud computing and content delivery networks, experimenting with CloudFront and S3 is highly recommended. It not only enhances your understanding of AWS services but also equips you with practical skills for real-world applications.