AWS CloudFront is a CDN service. It is a really fast and secure way for low latency content distribution. Cloudfront uses edge locations to enable the user to access content from a website, without traveling all the way to the origin, which results in lower latency.
Why S3 though? For starters, it’s easy to set up and use. It is low cost. A major advantage of S3 buckets is that they are automatically scaled so you don’t need to worry about the space your data might take beforehand.
Setting up AWS CloudFront
Step 1: Creating an S3 bucket
We’ll be using S3 buckets to store the data. If you already have one you can simply skip to the next step. Else, sign in to your AWS management console and click on S3 underservices to create an S3 bucket. Upload a test image file in that bucket. Then go to permissions and make it accessible to the public.
Step 2: Creating CloudFront
Under AWS Services find CloudFront and click on Create Distribution.
Step 3: Creating distribution
Go through the following instructions and enter the required details:
- Select your S3 bucket in Origin Domain Name (your bucket name).
- Then provide the path of the folder containing your file next to Origin Path.
- Then select Create a new identity under Origin Access Identity and Yes, update bucket policy under Grant Read Permissions on Bucket.
Proceed to Default Cache Behavior Settings.
Just make sure to select Redirect HTTP to HTTPS under Viewer Protocol Policy to make sure users don’t get an error when they try accessing your site using HTTP. You can leave the rest of the settings unchanged.
Follow these steps if you want to use a custom domain:
- Click on Add item and enter the domain name.
- To assign a custom SSL certificate you’ll have to choose the custom SSL certificate you wish to assign from the dropdown list and then create a distribution.
Click on Create Distribution to create your CloudFront distribution. You’ll see the following screen.
You can see the distribution you just created by clicking on Distributions in the left panel. It might not have been deployed yet though. It is completely normal for your distribution to take 10-20 mins to get deployed.
You can change the geo-restrictions, invalidations, etc by going to the distribution settings. Just select your distribution and click on Distribution Settings.
Now go to your distribution’s General tab and try accessing the same file using the CloudFront URL. You can just paste the Domain Name followed by the file’s name in your browser. The result you see now is coming through the CloudFront distribution. It might take a little time to load the file for the first time because the edge location is retrieving the file from the origin for the first time. The second time however it’ll take literally no time.
Please remember to disable and delete the distribution we just created or you’ll end up being charged for it. Also, make sure to read the AWS documentation on CloudFront to get a better understanding of the service and its pricing.
CloudFront can improve the performance of your website in locations far from your origin without increasing the cost exponentially. It’s more economical than sharing your content directly from an S3 bucket. We hope to have helped you set up an AWS Cloudfront distribution by this article.
- Get the best cloud hosting and secure your valuable data.