Create High Availability Architecture with AWS CLI using EC2, S3 and CloudFront.

Saurabh Rohankar
6 min readMar 20, 2021

--

Today we will see how you can create a High Availability architecture on Amazon Web Services(AWS) using Command Line Interface(CLI).

Our architecture will have integration of various AWS services like EC2, S3, and CloudFront and ELB.

High Availability Architecture on AWS

We will be creating the following architecture on AWS.

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

Step 1) Launching EC2 instance and installing apache server

To launch an ec2 instance we need following things:-

  1. Image Id:- ami-081bb417559035fe8
  2. Instance type:- t2.micro
  3. Security group id:- sg-06604abc96fa0bb65
  4. Subnet Id :- subnet-1e6e6776
  5. Region:- ap-south-1
  6. key name :- your key name
  7. Count:- 1

Now we will launch an ec2 instance with CLI using the above information.

aws ec2 run-instances --image-id ami-081bb417559035fe8 --count 1 --instance-type t2.micro --key-name new-key --security-group-ids sg-06604abc96fa0bb65 --subnet-id subnet-1e6e6776 --region ap-south-1 --tags Key=Name,Value=Webserver

Now your instance will be launched and we need to install apache in it. For that, we will log into our instance using SSH and the Public IP of the instance.

You can get the Public IP of running instances by the following command:-

aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicIP:PublicIpAddress,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --filters Name=instance-state-name,Values=running --output table

Now ssh in our instance using the following command:-

ssh -i "your_key_path" ec2-user@public_ip

Installing apache webserver using yum:-

yum install httpd -y

Starting and enabling httpd service

$ systemctl start httpd 
$ systemctl enable httpd

Step 2) Creating EBS volume

For creating EBS volume we need to specify volume type, size, and availability zone. Since EBS is a zonal service we will create it in the same zone as our instance which is ap-south-1a.

aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a --tag-specifications ResourceType=volume,Tags=[{Key=Name,Value=webserver_root}]

Now attaching this volume to an instance requires instance-id so use the following command

aws ec2 describe-instance-status

Finally attaching the volume to the instance

aws ec2 attach-volume --instance-id i-0ffd8d5afa3c455e8 --volume-id vol-07c6f41e3e4ac07b6 --device /dev/sdf

Now SSH in our instance because we need to format and make a partition to use EBS volume and then attach it to our web server's document root viz. /var/www/html.

  1. SSH like we did earlier
  2. Check the available volumes using:- fdisk -l

3. Now creating a primary partition of size 1 GB in our EBS volume.

Use following commands:-
a)Selecting volume for partition
$ fdisk /dev/xvdf
b)Creating new partition
$ n
c)Creating primary partition
$ p
And then enter enter..

4. Formatting the partition using ext4 format. (You can use any other format type if you need.)

$ mkfs.ext4 /dev/xvdf

5. Now mounting the formatted partition to document root of apache webserver viz. /var/www/html

$ mount /dev/xvdf /var/www/html

Step 3) Creating an S3 bucket for object storage

Now we will create an S3 bucket in which we will store our static files like images, videos, etc which we will use later for CloudFront.

  1. To create an S3 bucket use the following command:-
aws s3api create-bucket --bucket bucket_name --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1

You need to setup LocationConstraint for any region other than us-east-1

2. Uploading files to our s3 bucket

aws s3 cp "file_path" s3://bucket_name

3. Now we need to make our bucket public so that website could access it because we will be putting this link in our webpage

Now anyone can access our objects using object URL.

Step 4) Creating CloudFront Distribution

Now we will Set up Content Delivery Network with CloudFront and using the S3 bucket as the origin domain. In simple words, we will be using CloudFront to deliver our files stored in an S3 bucket to clients for low latency and more security.

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment.

To create distribution in CloudFront use:-

aws cloudfront create-distribution --origin-domain-name bucket_name.s3.amazonaws.com

From the above output, take a note of Domain Name because we will use that in the URL for our image which we will use in our code.

Step 5) Creating a sample webpage

Now finally we will create a sample webpage that will use our above-created CloudFront distribution to share an image with clients.

<html>   
<head>
<title>HA Arch</title>
</head>
<body bgcolor="cyan">
<h1>This is High Availability Architecture by Saurabh</h1>
<img src="https://d25gwetsi8p8y2.cloudfront.net/pexels-ehsan-ahmadnejad-3408057.jpg" , height=720, width=480>
</body>
</html>

In the image source, we have replaced the S3 object URL with the CloudFront Domain Name which provides us Content Delivery Network Setup.

Now add this file to /var/www/html folder.

And Congrats, our HA architecture is finally done! 👏🏻

Now you can access the High Availability webpage by Public IP of instance.

http://13.235.115.183/

In this architecture, You can also use an Elastic Load Balancer (ELB) to manage the load coming on webservers. Since this was a demo and I had used only a single instance so I did not use ELB but you can easily do it.

So I hope this article was helpful to you. It took me a few trials and errors and a lot of reading through docs for creating this architecture. This is a real Industry use case that many organizations use to reduce latency as well as downtime and to provide a seamless content surfing experience to their customers. So that's it for this one and I will see you in the next interesting article.

LIKE and SHARE!

--

--

Saurabh Rohankar
Saurabh Rohankar

Written by Saurabh Rohankar

Its never too late, my friend! 😉

No responses yet