High Availability Architecture with AWS CLI

Gursimar Singh
4 min readMar 26, 2021

--

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.

What is AWS CLI?

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

Webserver configured on EC2 Instance

Creating Key-Pair value and security group for EC2 instance.

Creating your own security group:

$ aws ec2 create-security-group — group-name <group_name> — description “<description>”

Now let’s create inbound rules:

$ aws ec2 authorize-security-group-ingress — group-name <group_name> — protocol all — cidr 0.0.0.0/0

The above command will create inbound rules for the security group which will allow all traffic.

Next we have to create private key-pair:

$ aws ec2 create-key-pair — key-name haa1 — query ‘KeyMaterial’ — output text | out-file -encoding ascii -filepath practicekey.pem

We can confirm it by checking in the Graphical Console.

Launching an EC2 Instance.

Let’s launch the instance in EC2 using the created key-pair and security group:

$ aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — availability-zone ap-south-1a — count 1 — tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=AWSCLIPRACTICE}] — security-groups MySecurityGroup2 — key-name haa1

Creating an EBS Volume and attaching it to the EC2 instance.

The next step is to create an EBS volume and attach it to the EC2 instance. To create a volume in EBS:

(The region ap-south-1a i.e, Mumbai, India and the size is 1Gib.)

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

To attach the EBS volume to EC2 instance:

aws ec2 attach-volume — volume-id [volume-id] — instance-id [instance-id] — device /dev/xvdf

Creating a S3 Bucket for storing static objects.

So EC2 is launched and EBS volume is successfully connected to EC2 instance. To create S3 bucket, run the following command:

(Note: Bucket Name should always be unique)

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

Transferring static files like images to S3 Bucket we created in previous step.

Now we have to upload the static data like images on the S3 bucket:

(I will be using the file test.jpg which is located in the root directory. Next I will upload it to S3 bucket and -acl public-read will give read access to the public.)

aws s3 cp /root/test.jpg s3://clib — acl public-read

Setting up Content Delivery Network(CDN) using Cloudfront and using the origin domain as S3 bucket.

Cloudfront is a service which comes under networking and content delivery. So let’s create a Cloudfront with clib S3 bucket as the origin domain:

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

(Note the domain name provided by the cloud front after running the above command.)

Webserver configuration on EC2 Instance.

Next step is to configure web-server on top of our EC2 Instance. Connect to the instance using ssh protocol or we can directly access the instance if it is the amazon AMI. First we need to login as root:

$ sudo su — root

And then install the httpd (apache webserver) and start the service:

$ yum install httpd -y$ systemctl start httpd

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

Let’s mount the document root /var/www/html to EBS Volume. Before mounting, we need to create and format the partition in EBS volume.

$ fdisk /dev/xvdf

press ’n’ to create a new partition
press enter 4 times
press ‘w’ to save the partition
The partition table has been altered.

To format the partition /dev/xvdf1, run the following command:

$ mkfs.ext4 /dev/xvdf1

To mount this partition over document root, use:

$ mount /dev/xvdf1 /var/www/html

To confirm that the partition has been successfully created and mounted, use “fdisk -l” or “lsblk” command.

$ fdisk -l (or) lsblk

Finally placing the Cloud Front URL on the webapp code for security and low latency.

$ cd /var/www/html$ cat > test.html

Create the web-page and include text and the image(which is located in S3 — instead of S3 URL, provide the URL of Cloudfront for security and low latency), and access it in the browser. Now you can observe that the images and static objects are provided by S3, whereas the data of the web-page is coming from EBS storage which is persistent.

$ aws cloudfront create-distribution — origin-domain-name mycloudfronttask6.s3.amazonaws.com

The final step is to access our webpage.

We can access our webpage by typing the following in the url box of the web browser

http://instanceIP/webpagename

This is how we can create High Availability Architecture with AWS CLI.

--

--

Gursimar Singh
Gursimar Singh

Written by Gursimar Singh

Google Developers Educator | Speaker | Consultant | Author @ freeCodeCamp | DevOps | Cloud Computing | Data Science and more

No responses yet