Setting up Fast & Secure Globally scaled Content Delivery Network with high availability using AWS Cloudfront & S3
Tags: aws, s3, cloudfront, cloud
Published: 2021-06-16 02:16:25

In this article, we are going to see about what is the Content Delivery Network, why we need them, what are its use cases and then finally we are going to set up our own Content Delivery Network with Fast, Secure and high availability using one of the most powerful services provided by AWS namely Cloudfront.

##Content Delivery Network
A Content Delivery Network (CDN) is a globally distributed network of web servers whose purpose is to provide faster content delivery. The content is replicated and stored throughout the CDN so the user can access the data that is stored at a location that is geographically closest to the user.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptkuvtwyujlug7c4laso.png)

This is different and more efficient than the traditional method of storing content on just one, central server. A client accesses a copy of the data near to the client, as opposed to all clients accessing the same central server, in order to avoid bottlenecks near that server.



High content loading speed ==positive User Experience

##CDN Architecture model
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo1jdtu78lutj7zvsis0.png)

The above figure clearly illustrates the typical CDN model. When a user requests the content, for the first time it will send to Content Provider, then Content Provider will send their copy of the document known as Source to **CDN** and that copy is stored as digital information which is created, licensed and ready for distribution to an End User. 

If the User requests the content again, he will receive the content from CDN only which is located nearer to the geographical location of the user, **not** from Content Provider. We can reduce **latency** and ensure **high availability.**

##Benefits of CDN over Traditional method
- CDN enables global reach
- 100% percent availability
- Your reliability and response times get a huge boost
- Decrease server load
- Analytics

##Use-Cases of CDN
- Optimized file delivery for emerging startups
- Fast and secure E-Commerce
- Netflix-grade video streaming
- Software Distribution, Game Delivery and IoT 

##CloudFront

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svxm66yxeyyx5pdxec0a.png)
                     **credits:** whizlabs

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment. CloudFront uses Origins from S3 for setting up its Content Delivery Network.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dih89tvfmufp9eg8n24o.png)

Cloudfront uses **Edge locations** to store the data cache. Currently, AWS now spans **77** Availability Zones within **24** geographic regions around the world and has announced plans for **18** more Availability Zones and **6** more AWS Regions in Australia, India, Indonesia, Japan, Spain, and Switzerland.
AWS uses **DNS** to find the nearest data centers for storing the **caches**. Data comes from **Origin** to **Edge location** and **Edge location** to our **PC**.

#Practicals

Now I am going to show you how to setup your Own custom Content Delivery Network for your content which includes images, videos, etc. 

##Pre-requisites
- AWS account
- AWS instance
- EBS volume

For saving time, I already launched one Amazon instance and one EBS volume sized 10GB and attached to its instance.

##Steps
- Installing HTTPD server
- Making its document root persistent
- Storing the content in S3
- Deployed it to CloudFront

##Installing HTTPD server
Since the package manager “YUM” is already installed in Amazon Linux 2, so run the following commands for configuration of HTTPD server in that instance.

```yum install httpd -y```

Then we have to start the service.

```yum start httpd```

We have to enable the service. So that we need not start the service again and again after every reboot.

```yum enable httpd```

##Making its document root persistent

Since the OS in Amazon 2 Linux is RHEL 8, so the document root of HTTPD server is **/var/www/httpd.** The document root is the location where the HTTPD server **reads** and deploys the webpage in web server. 

We have to make that document root persistent in order to secure the data from being lost due to OS crash etc.
For that, you have to ready with the previously created one EBS volume and done with the partition. Then run the following command.

```mount /var/www/html /partition```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xqrjtqml7gctil5413u.png)

##Simple Storage Service(S3)

Since the Origin for CloudFront is S3, we have to setup S3 so that we can get the Origin Domain name for CloudFront. In S3, the folders are said to be **buckets** and files are said to be **Objects.**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6c1m8i5bxwr6aigzdjm.png)

First step is to create a bucket by using the following command syntax.

```aws s3 mb s3://bucketname```

The second step is to move/copy the objects to the buckets by using following command syntax.

```aws s3 mv object-location s3://bucketname```

By default, Public access for S3 buckets is blocked. We have to release the access by running the following command syntax in your Windows Command Prompt.

```aws s3api put-public-access-block --bucket bucketname --public-access-block-configuration “BlockPublicAcls=false, IgnorePublicAcls=false, BlockPublicPolicy=false, RestrictPublicBuckets=false”``` 

```aws s3api put-object-acl --acl "public-read" --bucket "bucketname"  --key objectname```

We had completed the S3 setup, the HTML code of the webpage is shown below.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zep9diuhubkv5p5vvfxg.png)

##CloudFront

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gexgv5kscejlnmki0v3v.png)

Since Origin S3 setup is over, now we have to setup the CloudFront by creating one distribution using Origin Domain Name from S3 and Object name as Default Root Object.

We have to run the following command to create a distribution

```aws cloudfront create-distribution --origin-domain-name vishnu1234.s3.amazonaws.com --default-root-object Scam-1992.jpg```

After creating the distribution, CloudFront will give one domain address, we have to copy that domain address to that HTML code and it will replace the S3 domain address.

Finally our **Output** will be**…**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d590ng3o3cpgo0ceflsd.png)
>Thank you all for your patience to read this article.Stay tuned for my upcoming interesting articles.Have a good day.