High Availability Structure using AWS CLI
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.
In case you don’t know how to install AWS CLI click here to know more…
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.
- AWS CLI
Note: To download the putty click here.
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>
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.
To use further run aws s3 help.
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
and hence using further help we get our actual command i. e.
aws s3 mb s3://<bucket-name>
Now, we will use sync to upload our image
aws s3 sync "<local-path>" s3://<bucket-name> --include "*.<file-extension>"
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
Copy the URL and paste on the new tab to check the image
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
Creating a CloudFront distribution
Let’s find a command for CloudFront using aws help
Further use aws cloudfront help
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.
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>
Copy the domain name and open that in your browser and let’s see the result
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
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
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
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
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
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.
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>
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.
Put your website code in /var/www/html folder and start the web service to see the result.
Here is my 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.
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.
For more such content stay tunned with BrighterBees.
You may want to check our cloud category