High Availability Architecture with AWS CLI

Upmanyu Sharma
4 min readJan 7, 2021

The architecture includes-

1) Webserver 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 Cloud Front URL on the web app code for security and low latency.

So, Let’s Start with the practical.

Firstly, we have to Launch the Instance on the AWS

For that we can use Command:

aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — subnet-id subnet-0b606963 — count 1 — security-group-ids sg-09007a4edb9fed422 — key-name Mykey

Here Above we can see the instance launched in AWS GUI.

1) Configure Web Server on EC2 instance

Connect to the instance via AWS GUI

Login via root account by using command : sudo su — root

Firstly we need to install httpd by using command: yum install httpd

Now we have to start the server using command : systemctl start httpd

You can confirm that by using command : systemctl status httpd

Add alt text

For creating a website, we have to go inside the folder

var/www/html by using command : cd /var/www/html

Here we can create an html file for the website.

2) Document Root(/var/www/html) made persistent by mounting on EBS Block Device.

Html file created above is in the root drive, if the Operating System fails or gets corrupted due to some reason then whole data would be lost in the root drive.

So, to make the html file persistent, we have to mount it on EBS Block.

We have to create volume to be attached, let say of size 1 Gib.

Command: aws ec2 create-volume — availability-zone ap-south-1a — size 10

Now we have to attach this volume of 1 Gib to the EC2 instance created.

Command: aws ec2 attach-volume — instance-id i-0ca90e697a3b77b0c — device /dev/sdh — volume-id vol-04e6ec6587ddf6e1b

Now we have to create a partition in EBS Volume

We have to do 3 steps :

1) Creating Partition

For creating partition we need to use command: fdisk <device name>

n — for the new partition

p-for primary

w- to save

Now the partition is created.

2.) Format the Partition

After creating the partition we have to format the partition

Command: mkfs.ext4 <partition name>

3.) Mount the partition

Now we have to mount the /var/www/html to the partition created in EBS Volume

Command: mount <partition name> /var/www/html

3)Static objects used in code such as pictures stored in S3.

We can store static data here. So we are going to create an S3 bucket.

To create a bucket we can use the command:

aws s3api create-bucket — bucket new1testbucket — create-bucket-configuration LocationConstraint=ap-south-1 — region ap-south-1

We can see on AWS that our S3 bucket is created

Now, We have to upload the image:

Command:

aws s3 cp c:\Users\Dell\Desktop\new.jpg s3://new2testbucket/

We can see that object(image) name new.jpg is now uploaded.

Now We have to Grant Public Access to Bucket and Image:

For Bucket :

Command:

aws s3api put-bucket-acl — bucket new2testbucket — grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers

For Object :

Command:

aws s3api put-object-acl — bucket new2testbucket — key new.jpg — grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers

4) Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.

Now we are going to create Cloudfront distribution for Web

Command:

aws cloudfront create-distribution — origin-domain-name new2testbucket.s3.ap-south-1.amazonaws.com

Now we have to copy the Cloud Front URL on the web app code.

Now we can open the website by using the Public IP of the EC2 instance created followed by HTML file name.

Thank You All for reading my Article.

--

--