HIGH AVAILABILITY ARCHITECTURE WITH AWS CLI

So lets begin this task !!

🔅 The architecture includes 🔅

  • Configure Profile for AWS CLI
  • Create a Key Pair.
  • Create a Security Group.
  • Launch a EC2 Instance using the above Key Pair and Security Group.
  • Create a EBS Volume of 1GB.
  • Attach the above create volume to the Instance created.
  • Configure a Webserver on EC2 Instance.
  • Mount /var/www/html to the attached EBS volume (Block Device) to make the location persistent.
  • Create S3 Bucket and place the static object in the bucket and make it publicly accessible.
  • Create and setup Content Delivery Network using CloudFront using the origin domain of S3 bucket .
  • Use the Cloud Front Domain in the webapp instead of S3 domain name for security and low latency during access.

Pre-requisites:

1. Install and Configure AWS CLI version 2.

2. Create AWS IAM User.

Steps to Create AWS IAM User -

Step 1

Configure the AWS Command Line Interface (AWS CLI) and specify the settings for interacting with AWS.

  • We will setup the AWS Secret Access Key.
  • We will setup the Default Region Name.
  • We will setup the Default Output Format.
# run only once and enter the details
aws configure

Step 2

Here we will create a new Key Pair using a AWS CLI, and save the private key(.pem) in a file which will be needed at the later point of time.

aws ec2 create-key-pair --key-name awscsakey --query "keyMaterial" > awscsakey.pem

Step 3

This step we will be creating a Security Group which we named as SecurityGroupForCLI.

aws ec2 create-security-group --description Security_group_using_AWS_CLI --group-name SecurityGroupForCLI

Step 4

Once the security group is created, we need to add the rules for Inbound and Outbound, in our case we added the rules for inbound where we allowed all traffic from anywhere (entire world) using the AWS CLI.

aws ec2 authorize-security-group-ingress --group-id sg-0d489a16b56da793e --protocol  all --cidr 0.0.0.0/0

Step 5

Now once the Key pair and Security Group is created and also the inbound and outbound rules are added, its a right time to create a AWS EC2 instance.

  • Instance type
  • Security Group Id
  • Subnet Id
  • Key Name
aws ec2 run-instances --image-id ami-052c08d70def0ac62 --security-group-ids sg-0d489a16b56da793e --instance-type t2.micro --subnet-id subnet-6b1b7027 --key-name awscsakey

Step 6

Now we will create the EBS volume of 1 GB to make our code persistent or safe the code from any loss. Create the EBS volume in the region where EC2 instance is created.

aws ec2 create-volume --availability-zone ap-south-1b --size 1 --volume-type gp2

Step 6

Once we are ready with EC2 instance and EBS Volume, now we need to connect them with each other. For that we need the following information.

  • Volume Id
aws ec2 attach-volume  --instance-id i-0e898efb2e8844ce4  --volume-id vol-0cb41c3d139b8a9a8  --device /dev/xvdh

Great! 👏 you have created EC2 instance, EBS Volume and successfully attach them together.

Now we are going to setup Cloud Front for Content Delivery Network.

Step 7

Login to the EC2 instance using SSH Protocol in case of Linux. In case of Windows use Putty to do SSH to the EC2 instance.

# Change the permission of private key file.
chmod 400 awscsakey.pem
# SSH to EC2 instance using Private Key.
ssh -i "awscsakey.pem" ec2-user@ec2-13-232-153-64.ap-south-1.compute.amazonaws.com

Step 8

Now we need to format, partition and mount the connected EBS disk to the folder in a instance.

# command to check the partitions available in the instances
fdisk -l
mkfs.ext4 /dev/xvdh
# command to start partition process
fdisk /dev/xvdh# write the following options
# select p to the the details of the partitions available

p# select n to create a new partition
n# select p for the primary or e for extended partition
p# Select the number of partitions 1-4
1# Starting of the first sector, starting from 2048 as a default.
Press Enter without any changes# Ending of the sector, ending at 2097151 as default(entire size)
Press Enter without any changes# Press p to see the details of the partition
p
# command to check the partitions available in the instances
fdisk -l
mkfs.ext4 /dev/xvdh1

Step 9

Now it’s a time to setup the httpd server, for that first we need to install the httpd server using the following command.

dnf install httpd
systemctl status httpd

Step 10

Once the httpd server is setup, we need to mount the formatted partitions created in the step 8 to the /var/www/html to make the location persistent.

mount /dev/xvdh1 /var/www/html

Step 11

For store any object like images, videos, files etc we need to use S3 bucket. S3 is a global service we need not to worry about the region where to create. To create the same we need to run the following command.

aws s3api  create-bucket  --bucket arth-task-6-1523  --region ap-south-1   --create-bucket-configuration LocationConstraint=ap-south-1

Step 12

For showing our website we need to create a code base for our website and then copy the files to /var/www/html, as httpd server reads the file from the same folder.

# change to root user
sudo su
cd /var/www/htmlvi
index.html
# copy the code
# press esc

:wq# press enter
# restart the server
systemctl restart httpd
# html code
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.fa {
padding: 20px;
font-size: 30px;
width: 30px;
text-align: center;
text-decoration: none;
margin: 5px 2px;
border-radius: 50%;
}.fa:hover {
opacity: 0.4;
}
.fa-github {
background: #24292E;
color: white;
}
.fa-linkedin {
background: #007bb5;
color: white;
}
.fa-instagram {
background: #125688;
color: white;
}
.fa-medium {
background: #117A65;
color: white;
}
.fa-rss {
background: #808080;
color: black;
}img {
border-radius: 50%;
}
.a {
opacity: 0.9
background: #808080;
}
</style>
</head>
&nbsp
<p>
<body bgcolor="white" class=a>
<center><img src="http://d20poq3ti3l95r.cloudfront.net/TechBoutique.jpg" alt="Sami" style="width:200px">
<h2><font face = "Verdana" size = "6">TechBoutique Automation Hub</font></h2>
<h4><font face = "Verdana" size = "4">Blogger || Arth Learner || Developer </font></h4>
</center>

</body>
</body>
</html>

Step 13

Finally after the completion of the previous steps its a time to move to the last step to create CloudFront and use the Cloud Front Domain to our code. For creating the cloudfront we need s3 domain.

aws cloudfront create-distribution  --origin-domain-name  arth-task-6-1523.s3.amazonaws.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store