Sarthak Agarwal
5 min readNov 14, 2020

--

🔰 To Create High Availability Architecture with AWS CLI 🔰

🔅The architecture includes-
→Webserver configured on EC2 Instance.
→Document Root(/var/www/html) made persistent by mounting on EBS Block Device.
→Static objects used in code such as pictures stored in S3
→Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
→Finally place the Cloud Front URL on the webapp code for security and low latency.

To configure web server on EC2 instance

Firstly,We have to connect to the EC2 instance and then on the black screen we have to run the command :-

sudo -su root

To give all the access to the root user.

Now we have to configure the webserver and for that we have to run the httpd services but we should also have the httpd services installed in our operating system then only we can configure it.So let us come to the installation part of httpd.

As yum is already configured in Amazon Linux we just need to give the command :--

yum install httpd

This command is also shown in the screenshot below : —

Now the httpd services are installed in our operating system.

Making document Root (/var/www/html) persistent by mounting on EBS block device

We have to start the web services by the command : --

systemctl start httpd

Now we need to go to the directory of the file that is to be saved for uploading the text or the image.Directory for the web server is /var/www/html. So we use the command to go inside this directory :-

cd /var/www/html/

Now we can create our file to upload the text in it by the command :--

touch filename.html

To go inside this file we use the command :--

vim filename.html

To insert the text we press I and then we can enter the text we want in our web page. To save and exit we can use :wq

Where w stands for saving the file and q stands for exit.

Now you can search your website by using the link format :--

http://(public IP)/(file name)

To create and attach the EBS volume to the ec2 instance

we can run the following command on our command prompt as discussed in the earlier task :--

To make the data persistent we have to mount the EBS volume and then we have to format it.

To do this or for knowing about the partition in our OS,we use the terminal of our operating system and we use the command :-

fdisk -l

This command is shown below :--

Now for creating a new partition we use the command :--

fdisk /dev/xvda

Rest of the commands for selecting a new and saving a new partition created are shown below in the screenshot :--

To fomat the partition we used the command :--

mkfs.ext4 /dev/xvda1

To mount the partition onto the particular directory we use the command

mount /dev/xvda1 (directory name)

A screenshot for this is shown below :--

Static objects used in code such as pictures stored in S3

To create a new bucket with the help of AWS CLI we use the command :--

aws s3 mb s3://(bucket name)

NOTE: The bucket name that you will provide must be unique.

To copy the image to the bucket with the help of AWS CLI we use the command :--

aws s3 cp (image name or image path) s3://(your bucket name)/

Screenshot for the same is shown below :--

To make the object as public so that anyone can access the image by the URL,we use the command:--

aws s3api put-object-acl-bucket (your bucket name) --key (object name that you copied) --acl public-read

Also,to list the bucket objects we use

aws s3 ls s3://(your bucket name)

Output for both of the commands is shown :--

To get the URL to access the image we have to open the console and then go to s3 and then select the bucket object to see the URL. We have to copy this URL to use in the HTML file we created earlier for web server.

HTML code that we have to provide for proper outlook is given as :--

Now we can go to any search engine and can see the image in our website as : --

Setting up content delivery network using CloudFront and using the origin domain as s3 bucket.

Placing the CloudFront URL on the webapp code for security and low latency.

We have to create the CloudFront distribution by the help of AWS CLI. Finally,we have to copy the domain name provided which will access the image with high security and low latency. Code required :--

aws cloudfront create-distribution --origin-domain-name=“(bucket name).s3.amazon.com”

Usage of this cmnd is shown :--

Now we have to put the URL in the HTML file we have for our webapp. Remove the older URL and paste the new URL which is for the CloudFront.

Again try to access the webapp by using any search engine.

Hope this BLOG will help you guys.

Thank you 🙏

--

--

Sarthak Agarwal

Cloud & DevOps Enthusiast ★ARTH Learner ★ AWS ★ GCP ★ Jenkins ★ K8S ★ Ansible ★ MLOps ★ Terraform ★ Networking ★ Python