Configuring Webserver with High Availability Architecture using AWS S3 & CloudFront

 


Let's think of a situation where we want to access the data from the server running in the other country, then there will be a delay in accessing the content which is also termed as Latency Issue.

In this article, we would be creating a web page using AWS CloudFront that provides Content Delivery Network (CDN) service, which creates caches at Edge Locations.

→Firstly we will configure the web server on EC2 to host our website.

→Then we will store the website code into External storage(EBS) to secure it from an OS failure.

→We will create a webpage with the images for the webpage stored in an AWS S3 bucket.

→Then we will integrate AWS CloudFront with our website to enable CDN service


A web server is a program running in the OS that helps in running and hosting the website, as well as CGI files

A CDN (Content Delivery Network) is a topology that contains a highly distributed platform of servers that helps in reducing the delay caused in loading web page content reducing the distance between the hosted server and user creating local caches near the user location so that the user can access the content without any latency very easily.

Prerequisite:

Configure AWS CLI and Launch EC2 instance

As we are creating Infrastructure using CLI. CLI should be configured in your system if not you can refer to the above article. In this article, I already explained how to Configure CLI, create key-pair, launch ec2 instance and security-group.

For hosting a webpage we need a web server and we are using an Apache web server here

  • First, we have to download and install a web server for this we will use the yum command
yum install httpd -y

  • Now we have to start the webserver using the system command
systemctl start httpd

If we use root storage(where our OS is installed) then if by chance our OS crashes then all the data (webpages ) created will get destroyed so we add a different hard disk to store our webpages.

In the previous article we already attached an EBS storage of 1 GB to check if our storage is attached with our instance we use the command

 fdisk -l

Here we can see our storage but to store it in this storage we need to create a partition, format and mount it.
  • Step 1: Creating a partition

- fdisk /dev/xvdf(device name)
- n (it create a new partition)
- p (for primary partition)
- now we have to select the first and end sector, in my case I have made a single partition of 1 GB size
- w (to save the created partition)

We can see our partition is successfully created of approx 1 GB size and name of our partition is /dev/xvdf1

  • Step 2: Formatting created partition

When we format partition, it creates the Index table or Inode table that keeps a record of all files with their stored location for easy access.

We will format our partition in ext4 format

mkfs.ext4 /dev/xvdf1(partition name)

  • Step 3: Mounting partition with a folder

We will mount our partition with /var/www/html folder as the apache webserver will only execute the webpage which is present in this folder and we also need to secure our data from any system failure.

mount /dev/xdf1 /var/www/html

Now we will check if our partition is mounted or not using the command

df -h

We can see our partition is mounted with /var/www/html folder

We are using S3 instead of EBS storage as user data is static and it is very important for us to keep user data secure and easily available, availability and durability of S3 is more than EBS and this is the reason behind using S3 storage over EBS.

  • First, we have to create an S3 bucket using the command
aws s3api create-bucket --bucket give-bucket-webserver --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1

This created an S3 bucket with the name of “give-bucket-webserver”

  • Uploading an Image in S3
aws s3api put-object -- bucket give-bucket-webserver -- key task1.jpg --body task.jpg --content-type image/jpg

We have to make sure to give the correct content-type, here if you don’t give content-type image/jpg. URL instead of view downloads image this won’t work directory using URL we have to some more thing on the webpage to show an image.

We can see our image is uploaded in the S3 bucket

The image must be made public so that anyone can see it.

The object URL specified in the bucket for the current image uploaded would be used in the HTML file which I would be showing now. So that the server loads the image in the HTML file from the S3 bucket.

aws cloudfront create-distribution --origin-domain-name give-bucket-webserver.s3.amazonaws.com

Here origin-domain-name is our S3 bucket

The CloudFront gives us a new domain address that enables the CDN in the origin domain bucket.

We got our CDN enabled URL: d1kjon6ojmwqr7.cloudfront.net

  • Creating Html file yash.html

We have used the image source domain which we received from CloudFront for our bucket

Since the apache web server is active right now we can view our webpage using the public IP of the instance and webpage name

Now the content cache is there in every Edge location of the user who has accessed this webpage. The first time it would give latency as initially, it copies cache in nearby edge location of the user but next time it will automatically fetch content from nearby Edge location cache to reduce latency and faster content delivery.

Thanks for reading this article! Leave a comment below if you have any questions.



Comments

Popular posts from this blog

How to configure Hadoop cluster using Ansible playbook

Automation Using Python Menu-Driven Program

Configuring HAProxy using Ansible playbook