High Availability Structure using AWS CLI

Published by Anubhav Singh on

We have discussed AWS Cloudfront and S3 storage service of AWS and now using both of that services we are going to create a high availability structure using AWS CLI.

In this structure, we are going to use our command prompt to complete the task and putty to configure the AWS instance. Before jumping the task first confirm that you have basic knowledge about AWS Cloudfront and Amazon S3 and if not check out the below post first.

AWS CloudFront

Amazon S3

In case you don’t know how to install AWS CLI click here to know more…

Task Description

All the below task is going to be performed on AWS CLI.

  • 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 a Content Delivery Network using CloudFront and using the origin domain as an S3 bucket.
  • Finally, place the Cloud Front URL on the web app code for security and low latency.

Prerequisite

  • AWS CLI
  • Putty

Note: To download the putty click here.

Setup

EC2 instance

In the last practical of my AWS CLI I talked deep about the commands of AWS, how to launch the instance, create key-pairs, security groups, and many more you can check out that by clicking here.

To start working on AWS. Use aws help. This will list all the commands available and now we want to launch the instance and launching instance is inside EC2 service. We will pic ec2 in that.

 

 

Now, use aws ec2 help.  Search for creating or run an instance and you will find run -instances as a subcommand. We have our command, the only thing we need is the details necessary to launch the instance. To find that we will use aws ec2 run-instances help.

Here we will find the actual arguments needed. Fill in the details and finally run the command to launch the program.

The final command would be something like this

aws ec2 run-instances --image-id <ami-id> --instance-type <instance-type> --key-name <your-key>
run instances command
aws CLI

Our instance is launched. Let’s complete the procedure of S3 and Cloudfront first.

Static Object in S3

Now, we need an s3 command that can upload our static data like image. We will further use aws help.

aws s3 command
aws help

To use further run aws s3 help.

aws s3 documentation
AWS s3 documentation

In the above picture, we can see sync is the subcommand to upload the data but first, we should have our bucket ready.

To create the bucket mb subcommand

aws s3 mb command

and hence using further help we get our actual command i. e.

aws s3 mb s3://<bucket-name>
aws s3 mb help
aws s3 mb help
aws s3 document
create bucket command

Now, we will use sync to upload our image

aws s3 sync "<local-path>" s3://<bucket-name> --include "*.<file-extension>"
sync data in s3 bucket
Sync data in s3 bucket

If data upload cause an error read the documentation first.

Image is uploaded to confirm that we will open web UI of AWS account and check the details

s3 bucket
Amazon S3 bucket description
bucket url
S3 bucket URL

Copy the URL and paste on the new tab to check the image

access denied s3 bucket
Access Denied

By default, our S3 doesn’t give access to view the file to the public but to make it public in web UI there is an option but we will use the command line. I searched and found at the time of upload we can make our object public using the below command

aws sync "<file-path>" s3://<bucket-name> --include "*.<file extension>" --acl public-read 

Let’s try to refresh our bucket URL

bucket url
bucket URL

Creating a CloudFront distribution

Let’s find a command for CloudFront using aws help

aws cloudfront
AWS CloudFront

Further use aws cloudfront help

aws cloudfront create distribution

Now we need required arguments for the creation of distribution. To know that I take help of web UI and to know the command I use help.

aws cloudfront create distribution arguments
Creating distribution argument

origin-domain-name is the important one, so I used that only and here is the final command

aws cloudfront create-distribution --origin-domain-name <bucket url>
aws cloudfront distribution

Copy the domain name and open that in your browser and let’s see the result

aws cloudfront distribution domain name view
cloudfront distribution

Great, We have successfully done this part. Now, we need to create one extra EBS volume and attach to that with our instance and then further configure

Create an extra EBS Volume

We know that EBS is the service inside the EC2 service and therefore we use aws ec2 help and there we will find a sub-command to create-volume.

Further taking help we gather the argument and here is our final command

aws ec2 create-volume --availability-zone <your instance zone> --size 1
ebs-create-volume
EBS volume creation

Attaching the volume with the instance

In EC2 we will further use help to find the attach command and final command will be like this

attach volume in EBS
EBS attach volume

Now, the volume is attached. Next, all the process will be followed on Putty.

We can confirm volume attached or not either from web UI or from CLI

ebs volume attached
EBS volume attached

Check this from CLI we have to go inside our instance. To go inside the instance we are going to use Putty and we need key pair in .ppk format. I already downloaded that earlier.

Once you logged into the Putty. You can list all the hard disk and related details from here

fdisk -l
hard disk description command

In order to use the attached volume we have to make partition and format and then mount that to /var/www/html folder. This is all about the linux command and in case you want to learn some basic command you can read this article.

Configure the EC2 instance

How to install Apache Webserver

Log in to the instance using putty and change the ec2-user with the root user using the command

sudo su -root

To install the Apache webserver use yum/dnf command

yum install httpd

Now, we can mount our extra volume to /var/www/html folder and paste our code there

Before that we have to make partition and format the disk for uses.

How to make partitions

fdisk <device-name>
format the hard disk using fdisk command

Partition command

Using this command we can go inside the hard disk and now we can format the hard disk

Once we are inside the hard disk to make a new partition we have n as the command. After that p for the primary. After that it will ask for the partition number and it is 1 in our case. Now, this asks for the starting and ending sectors we can simply click enter to go with the default value. Atlast w to save the partition.

hard disk format command in linux

Once partition is done our next task is to format the hard disk.

How to format the hard disk

To format use the below command

mkfs.ext4 <device name>
hard disk format command in linux
format command

Once the format is done now mount this hard disk with the /var/www/html folder

How to mount the hard disk to a particular directory

mount <device name> /var/www/html

Further we can check that using lsblk command.

mount the hard disk
Mount the hard disk

Put your website code in /var/www/html folder and start the web service to see the result.

Here is my code

html code for testing
website code

How to start the Apache webserver

Once the configuration of the apache webserver is done our final step is to start the service using the following command

systemctl start httpd 

Grab the instance IP and paste that in the new tab. And here is our final outcome.

apache webserver on ec2 instance
Our Website over EC2

Final words

Hurrah! we have successfully completed our set up part. Hope you guys find it useful.

In order to learn more about Cloud computing check out the cloud computing category and for more exciting practical stay tuned with BrighterBees.

You can connect with the author on Twitter and LinkedIn.

For more such content stay tunned with BrighterBees.

You may want to check our cloud category


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US