High Availability Architecture with AWS CLI

Pranita Gughane
7 min readMay 11, 2021

🔰 Link to the previous article showing how to get started with AWS CLI — https://www.linkedin.com/pulse/building-cloud-infrastructure-using-aws-cli-pranita-gughane/?trackingId=05FVfhd5oECVa6C6HI1Q2A%3D%3D

✅TASK DESCRIPTION:

🔅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.

👉 Everything is done by AWS CLI👈

AWS : Amazon Web Services is a Public Cloud Service by Amazon Company. AWS provides Infrastructure As A Service, Platform As A Service, and Software As A Service. In This Task I am going to use AWS CLI,EC2 ,EBS, S3,Cloudfront. AWS Provides High Availability, Isolation and Security of services used by us. AWS Provide each service with minimal cost. AWS works on a pay-as-we-go model.

🔅REMEMBER TO ALWAYS MAKE USE OF ‘HELP’ OPTION IN CLI

👉AWS Configure :

Before starting anything we need to configure. Configuration is done by us because it will connect our Command Line to AWS IAM User Account so that we can launch our services there.

“ aws configure ” is the command for configuration.

👉Create KeyPair :

This is the Command for generating key and storing on our given location for local P.C.

aws ec2 create-key-pair --key-name Task2Key --query "KeyMaterial" --output text > Task2Key.pem

👉Create Security-Group and allow Ingress to ports 22 and 80 :

aws ec2 create-security-group --group-name awsgrp --description "security group for task2" --vpc-id vpc-433fd328
aws ec2 authorize-security-group-ingress --group-name awsgrp --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name awsgrp --protocol tcp --port 80 --cidr 0.0.0.0/0

👉EC2 Instance :

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --count 1 --instance-type t2.micro --key-name Task2Key --security-group-ids sg-0234e1b7b8d9b3138 --subnet-id subnet-4c031624

Now to give a tag to my instance i have used this command

aws ec2 create-tags --resources i-06eca10be961bde27 --tags Key=Name,Value=AwsTask

👉EBS Volume :

The command to create EBS Volume is

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

Volume is Created but not attached

aws ec2 attach-volume --volume-id vol-0f9748e7297561b17 --instance-id i-06eca10be961bde27 --device /dev/sdf

Instance is successfully launched and also volume is attached.

👉Partition :

We have successfully attached 1 Gib EBS Volume to EC2 Instance So we have to follow 3 steps now so that we will mount 1 Gib Volume to /var/www/html directory.

First check how many volumes are attached to this instance by the command “ fdisk -l ”

The command to do partitioning is “ fdisk /dev/xvdf”

By entering “m” will open more options in front of you.

Press “n” to create a new partition.

Press “p” to create the primary partition.

Press “w” to save the partition made.

See the partition is created.

👉 Format

“ mkfs.ext4 /dev/xvdf1 ” is the command to format the partition.

Before mounting, install httpd which is Apache Tool to make an instance as a web server.

The command to install httpd is “ yum install httpd -y”

👉 Mount

/var/www/html is by default a folder made by httpd as this is the main folder which is accessed by httpd while launching the website.

The command to mount partition is “mount /dev/xvdf1 /var/www/html”

By “df -h” command you can see that /var/www/html is mounted to /dev/xvdf1.

👉S3 Bucket :

S3 here is used to store static files which are used in websites . AWS gives high Availability and Durability Guarantee on S3.

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

“ aws s3 ls” command is used to see how many buckets are present in s3.

aws s3 cp "D:\aws.jpg" s3://pranitabucket/

👉Create a file :

Now create a HTML file so that it will be publicly accessible but the image URL used is of S3.

NOTE — create your program file in /var/www/html directory as httpd by default access that folder files.

cd /var/www/html
vi index.html
<body>
<h1>THIS IS AWS TASK2!!!...</h1> <img src="https://pranitabucket.s3.ap-south-1.amazonaws.com/aws.jpg" width="100" height="200"

</body>

Now start httpd as this is very important otherwise you will not be able to see your page.

systemctl start httpd

Not to worry we forgot to make S3 Object Public Readable.

👉 S3 OBJECT PUBLIC READ-

Make S3 Object Publicly readable.

Now to make the object publicly readable use this command

aws s3api put-object-acl --bucket pranitabucket --key aws.jpg --acl public-read

Now you can see it publicly Visible.

👉Cloudfront

CloudFront plays very important role for low latency.When origin is far from client then edge location is used to store cache so that it will be fastly accessible.As in cloudfront we can set Time To Live [TTL] so that only for that time cache will be stored in edge location.Caches are temporary in nature.

aws cloudfront create-distribution --origin-domain-name pranitabucket.s3.amazonaws.com --default-root-object aws.jpg

Distribution is Created.

I have accessed bit by this Url -

d19nlnnpqkczjh.cloudfront.net - url

And you can see how the URL changed to the origin URL.

👉CHANGE THE CODE URL TO CLOUDFRONT URL :

And now it’s visible.

Thank You for Reading This Article!!😊

--

--

Pranita Gughane
Pranita Gughane

Written by Pranita Gughane

"DevOps engineer by day, pipeline enthusiast by night. Ensuring your code deploys smoothly while I’m up at 2 AM wondering if I’ll ever get a full night’s sleep"

No responses yet