Creating high availability architecture with AWS CLI

Deepanshu Yadav
6 min readOct 31, 2020

Today we gonna create a web app on aws and will see how to create high availability, low latency, etc. we will be configuring our services from console as well as using cli to have a taste of both.

Prerequisites:

  1. AWS CLI configured in your system

if you don’t know how to configure it then i have shown it in my previous blog. reach there from below link

2. Knowledge of services like EC2, EBS, s3 and CDN services like cloudfront

🔅The architecture we are going to create includes🔅
1. Web Server configured on EC2 Instance
2. Document Root(/var/www/html) made persistent by mounting on EBS Block Device.
3. Static objects used in code such as pictures stored in S3
4. Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
5. Finally place the Cloudfront URL on the webapp code for security and low latency.

🍤Let’ start now🍤

If you are new to the CLI world then i would recommend you to read my previous blog in cli as there i discussed from basics how to use it and the most important what’s the approach for using it. Here i would be little fast in creating the architecture. Below is the link of the story👇

🎇So, as mentioned, our first task is to create an ec2 instance and start a web server in it. As discussed in previous story and to make it fast i am assuming that we have already created an ec2 instance and an ebs volume you have attached to it.🎇

Now we are configuring a web server in our instance which is a very easy task. Needs just 2 commands to give in our instance.👊

yum install httpd -y
systemctl start httpd

Now for accessing our server, since we first need some web page also so i have created a small page on it, but you can have the code of your website here.

✔since we have configured Apache httpd server, in it we have to put our web pages and related data in the /var/www/html folder. so you can transfer your images, code, etc using winscp or any other program. And i have logged in instance using putty, you can use any ssh client or browser based connection services, etc.✔

To access webserver, type your ec2 instance ip and the page name as shown below👇

🔥Now our main task is started🔥

The problem is:(

👉What if our instance got corrupted ? The code we normally have on github and also have many backups but the data we have like images, videos, etc we will loose.

What we can do then ?🤔

🤞so lets put our code, data, etc in a separate EBS volume and attach it to our instance. so if our os got corrupted, we won’t loose anything and will get back our site in few minutes by launching an another server.🤞

since we already created an EBS volume and attached to our instance, let’s transfer our data to this volume. but before using an EBS storage we first have to make partition, format it and mount to our html folder as it’s a block storage. The steps to do it are below:

Making partition

👉fdisk yourdiskname

In my case the disk name is /dev/xvdf. you can see yours by the command fdisk -l.

Now press:

n, then enter, again enter, again enter and finally w. This will create partition in your volume.

Format and Mount

To format👉mkfs.ext4 /dev/xvdf

To mount👉mount /dev/xvdf /var/www/html

Note- I have shown very high level creating the partition as its a long topic. But its easy to learn. just search on google or youtube lots of good tutorials present.🤞

Now if you see by ls command, your html folder is empty as its now linked to different volume which is empty, you can now transfer here your data like images.

Now, there is again a problem🤔

EBS has low durability. it also has chances of failure or corrupt. if our data needs to be preserved for a long term we need some storage which like s3 which is highly durable, available, scalable, etc and s3 is the one. its very famous of its durability of 11 9s i.e. 99.99999999999.

so, lets shift our data from ebs to s3. we can have our code in the ebs only as code we can write again, and we normally have lots of backups, but the problem comes with the data like images, videos, etc which once lost can’t be recovered. so lets create and upload our data to s3✌

The commands to do it are shown below👇

Anything we upload in s3, we have a seprate url for it and anyone and any no. of people can access it at the same time, but we can set restrictions also accourding to our need.🔰

Now, since we want that our image should come from s3, not from ebs, so in our code, in the image url, we have to give the url of this image. so, the code would now look like this

if you are not able to access the image from s3, then make it public from the console as shown below. you can also do it from cli by changing the permission accordingly using the help command you can find easily how to do it.👇

so now our image is coming from a different location and code from different, but a client can’t see it and assumes it to be coming from one single place.

Now, one more problem we can have😒

The problem is of latency.💔 so, clients from different countries would see some delay in opening of our app which is not a good user experience and this can be solved using the cloud deleviery network (CDN) services of aws like cloudfront services.

so, we can setup cloudfront over s3 using the command below👇

since cloudfront gives a new url, we have to update our code as follows:

Now finally, our website will still look like same, but noine will face latency now, and we have finally created a highly available architecture for our website.👊

Thanks for reading😃 if you got any problem in the set-up or any related query, you can ping me on linkedin or mail.

--

--