DEV Community

Cover image for How to use CloudFront to distribute content using Amazon S3 as Origin
Opeyemi Jokanola
Opeyemi Jokanola

Posted on

How to use CloudFront to distribute content using Amazon S3 as Origin

CloudFront is a content delivery network that helps improve the read performance of your content by caching it at different edge locations around the world. This improves the user experience by reducing latency and providing faster access to content. CloudFront has a global network of more than 600 Points of Presence and 13 regional edge caches in over 100 cities spanning 50 countries. With global distribution, CloudFront provides DDoS protection for content, as AWS Shield and AWS Web Application Firewall (WAF) are integrated to work together seamlessly. The availability and reliability of your files are also enhanced since copies of your content are cached in multiple edge locations worldwide.

Image description

How CloudFront Works
When using CloudFront, you need to specify the origin servers, which can either be an Amazon S3 bucket or your own HTTP server, containing the files you want to distribute. These files will be transported from the origin servers to CloudFront edge locations scattered across the world.
The origin server holds the original and definitive version of your objects. The next thing is to upload your files to your origin servers. These files can be anything that can be served over HTTP, including web pages, images, and media files.
We are using Amazon S3 bucket as our origin server, which has the option of making the objects in the bucket publicly readable. Anyone with the CloudFront URLs for your objects can access them. Alternatively, these objects can be kept private and regulate who has access to them.
Once the origin servers have been specified, a CloudFront distribution can be created. This distribution will inform CloudFront which origin servers to retrieve your files from when users request the files through your website or application.
After creating a new distribution, CloudFront will assign a domain name to it, which can be seen in the CloudFront console.
CloudFront sends your distribution's configuration to all of its edge locations, or points of presence (POPs). These are collections of servers in geographically dispersed data centres where CloudFront caches your content.

How to set up CloudFront
We need to create S3 bucket for starters.

Image description

Image description

We are going to upload our files into the bucket created.

Image description

Image description

Then we open up CloudFront and create origin access control
Image description

Image description
We also need to update our S3 bucket policy, and then disable WAF protection because we do not need them for this hands-on.

Image description
The default root object should be "index.html", then click Create distribution
Image description

We have successfully created a new distribution. The S3 bucket policy needs to be updated and we can copy it from the CloudFront console.
Image description .
We go back to the S3 bucket created, click permissions, scroll down to Bucket policy, and then Edit. Paste the copied S3 bucket policy from CloudFront.
Image description

Then we go back to the CloudFront console, copy the distribution domain name then paste it into a browser.
Image description

Well done for coming thus far. We have been able to successfully distribute the files using CloudFront.
Image description
Image description

That's it, folks.
Till I break down more AWS services, catch you soon.

Top comments (0)