Getting Started with AWS-CLI — Part-2 (HA Architecture)

In this article, I will show you how to create a High Availablity Architecture using AWS CLI. Here, I will configure WebServer on AWS EC2 instance and will make the document root(/var/www/html) persistent by mounting it on EBS Block Device. After that, I will store all of the static files like images, videos, etc in the S3 bucket and will set up a Content Delivery Network using AWS CloudFront and using the origin domain as an S3 bucket. Finally, I will place the CloudFront URL in the WebApp code to achieve low latency and faster content delivery.

So, let's begin…

Pre-requisites:

  • AWS-CLI should be installed and configured. (For AWS-CLI installation and configuration refer to this article.)
  • If you are new to AWS-CLI then to get familiar with AWS-CLI, read this article.

Step 1: Launch an EC2 instance and Configure WebServer

For launching the EC2 instance and configuring the WebServer run the following command in CLI.

$ aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-2f0b3147 --security-group-ids sg-0c63dbd9b997a729a --key-name cankush625 --user-data file://aws/ud.txt

The above command will launch the EC2 instance with Amazon Linux 2 and instance type t2.micro. To configure the WebServer we will use HTTPD software which is the WebServer provided by Apache. I will use the user data concept to configure the HTTPD WebServer automatically at the time of launching of the EC2 instance.
The ud.txt file contains the commands to install and configure the HTTPD WebServer.

The content of the ud.txt file will be as follows:

This will launch the EC2 instance and configure the HTTPD WebServer on that EC2 instance.

Step 2: Create and Attach the EBS Block Device to the document root

To make the document root persistent, I will mount it on the EBS Block Device.

To create the EBS block device run the following command

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

This command will create an EBS volume with the tag AutoVol.

Now, we have to attach this volume to the EC2 instance we had launched earlier. For attaching EBS volume run the following command.

$ aws ec2 attach-volume --volume-id vol-04f2cc536132f78de --instance-id i-0fdd806d073e711e4 --device /dev/xvdc

This command will attach the EBS volume to the EC2 instance at /dev/xvdc

As we have to make the document root(/var/www/html) persistent, I will mount the /dev/xvdc at /var/www/html and format it with the file system type ext4.

Connect to the EC2 instance we had launched and follow the following steps.

First of all, I will create a partition of size 500MiB in /dev/xvdc.

fdisk /dev/xvdc << FDISK_CMDS
g
n
1
+500MiB
n
2

t
1
83
t
2
83
w
FDISK_CMDS

The above command will create a partition named /dev/xvdc1 of size 500MiB.

After that, I will create a file system of type ext4 in the /dev/xvdc1 partition.

$ mkfs -t ext4 /dev/xvdc1

And finally, mount the /dev/xvdc1 partition at document root(/var/www/html).

$ mount /dev/xvdc1 /var/www/html

Now, I’m all set to store my WebApp code inside the document root. I will go to the /var/www/html directory and download the WebApp code there using git.

$ cd /var/www/html
$ git clone https://github.com/cankush625/Web.git

Until this point, I have the WebApp up and running. But this application has some latency because of the static files of heavy size. So, to overcome this problem, I will store all of the static files inside an S3 bucket and will create a Content Delivery Network using AWS CloudFront to achieve low latency.

Step 3: Create an S3 bucket and store all of the static files in it

To create the S3 bucket run the following command in the terminal.

$ aws s3api create-bucket --bucket terminal-ui --region ap-south-1

The above command will create an S3 bucket named terminal-ui in the ap-south-1 region.

now, I will upload the static files in the S3 bucket we had created.

Run the following command to upload static files like images, videos, documents, etc in the S3 bucket.

$ aws s3 cp white_rose.jpg s3://terminal-ui/white_rose.jpg --acl public-read

The command above will upload the image named white_rose.jpg in the S3 bucket named terminal-ui with the public-read access.

Now, I have uploaded static files in the AWS S3 bucket.

Step 4: Set up Content Delivery Network using AWS CloudFront

Amazon CloudFront is a content delivery network offered by Amazon Web Services. Content delivery networks provide a globally-distributed network of proxy servers that cache content, such as web videos or other bulky media, more locally to consumers, thus improving access speed for downloading the content.

For creating CloudFront distribution, run the following command in the terminal.

$ aws cloudfront create-distribution --origin-domain-name terminal-ui.s3.amazonaws.com --default-root-object white_rose.jpg

The above command will take the origin domain name that is the S3 bucket URL and the default root object that I will provide as the white_rose.jpg image.

After running this command, the AWS CloudFront distribution will be created.

The Domain Name provided by the CloudFront distribution is the URL through which we can access the static files we had stored in the S3 bucket. As I have only one image for now and I had provided that image as a default root object, I can access that image directly by using the Domain Name provided by the CloudFront.

Finally, I had set up the CloudFront distribution for the S3 bucket.

Step 5: Using CloudFront Domain Name inside WebApp code to access static files

Now, I can use the Domain Name provided by the CloudFront distribution inside my WebApp code to achieve low latency and faster content delivery.

Thanks for reading!

For any help or suggestions connect with me on Twitter at @cankush625 or find me on LinkedIn.

Tech blogger, researcher and integrator