High Availability Architecture with AWS CLI
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.
So Let’s start!!
AWS Configure on CLI:
For completing this task, if you are using CLI for the first time or you haven’t configure your AWS in CLI then first of all you have to configure it.
For configuration you requires : Access Key ID and Secret Access Key, which is kind off Username and Password
Launch ec2 Instance:
Now to launch an Instance, first we will create a Security Group
aws ec2 create-security-group --description "SG for CLI-Task-6" --group-name task-6 --vpc-id vpc-e1a94b8a
You we can confirm from the webpage of AWS
Now we can create a Key pair which will be attached in the instance
aws ec2 create-key-pair --key-name <Key_Name>
But I have already created, so I will use that one
Now for launching the Instance -
aws ec2 run-instances --image-id ami-0a9d27a9f4f5c0efc --instance-type t2.micro --count 1 --subnet-id subnet-734d343f --security-group-ids sg-05460232b7068a79b --key-name myhadoopos
As we are going to use httpd web server so, it uses port 80 and for connecting the instance we use ssh protocol which uses port 22.
For this we will have to update the inbound rule of our security group
For port 80:
aws ec2 authorize-security-group-ingress --group-id sg-05460232b7068a79b --protocol "tcp" --port 80 --cidr 0.0.0.0/0
For port 22:
aws ec2 authorize-security-group-ingress --group-id sg-05460232b7068a79b --protocol "tcp" --port 22 --cidr 0.0.0.0/0
AWS Page -
Now we can good to go to configure the Webserver on the the ec2 instance which we launched now!!
I’m using Putty software to connect to the instance using the public ip
A. Webserver configured on EC2 Instance
To configure the webserver we need to do three steps:
- Install the httpd software:
yum install httpd -y
- configure the path:
- Start the services:
systemctl start httpd
You can confirm whether the httpd is running or not:
netstat -tnlp | grep httpd
Now, you can check the server is running.
B. Document Root (/var/www/html) made persistent by mounting on EBS Block Device
For this first we will create a EBS volume of size 1GiB
aws ec2 create-volume --availability-zone "ap-south-1b" --size 1
AWS Page Volume created:
Now we will attach the volume to the running instance
aws ec2 attach-volume --volume-id vol-0c86155bc06111295 --instance-id i-06f6ed074433763d9 --device /dev/sdf
AWS page Volume attached
We can also check in the instance whether it is attached or not
Now as we know to use any storage device we have to perform three steps to: 1. Partition, 2. Format and 3. Mount
- Partition of the attached EBS Volume:
We can confirm using command:
mount /dev/xvdf1 /var/www/html/
Now we can create a new webpage name test.html
Now, search for this page http://ip:80/test.html
Tip: If the page is showing Forbidden, then it could be because of Selinux, disable it using command setenforce 0
C. Static objects used in code such as pictures stored in S3
For this, first we will create a bucket in S3, Use the
s3 mb command to make a bucket. Bucket names must be globally unique (unique across all of Amazon S3) and should be DNS compliant.
aws s3 mb s3://my-task6 --region ap-south-1
AWS S3 service page:
Now, upload a picture to the bucket with the public access using
aws s3 cp C:\Users\PRATYUSH\Desktop\httpd.png s3://my-task6/ --acl public-read
AWS S3 bucket page:
This is the Image:
Now let’s update this url in the html code
And now it is been attached to the Web Page
D. Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
In the CloudFront delivery network, now we have to create distribution in which the origin domain name is the S3 bucket which we created now and the root object is the file (that httpd image).
aws cloudfront create-distribution --origin-domain-name my-task6.s3.amazonaws.com --default-root-object httpd.png
AWS CloufFront page:
It’s providing a domain name, if we search this domain name:
E. Finally place the Cloud Front URL on the webapp code for security and low latency
Now instead of the s3 bucket domain we will update the cloudfront domain name in the code which will provide us low latency.
The final test.html code:
<h1> Hello !! Task-6 </h1>
<p> Web Server ---- EBS Volume </p>
<img src="http://d25udx3ichcjnn.cloudfront.net/" weidht=500 height=200 >
<p> Thank You!! This is using the CloudFront URL </p>
The final webpage:
I hope you find it helpful.