Create High Availability Architecture with AWS CLI
Sahitya Puvvada
OPM support scientist|| Cognitive Neuroscience Research Assistant|| Cognitive Neuroscience and Robotics Graduate || Ex-PCB Designer
Hi enthusiasts,
In this article we are going to configure webserver on AWS instance and create simple web app with high availability and security.
We should do following steps to setup AWS architecture
1.Webserver configuration on EC2 Instance
2.DocumentRoot(/var/www/html) made persistent by mounting on EBS Block Device.
3.Static objects used in code such as pictures stored in S3
4.Setting up Content Delivery Network using Cloud Front and using the origin domain as S3 bucket.
5.Finally place the Cloud Front URL on the web app code for security and low latency.
Webserver configuration on EC2
First, we create EC2 instance using CLI. Then we remote login to console using putty and install apache web server.
Creation of new EC2 instance
#creating ec2 instance aws ec2 run-instances --image-id ami-098f16afa9ef40be --instance-type t2.micro --count 1 --security-group-ids sg-08b23e40a7d598ffe --key-name keyhadoop --subnet-id subnet-19ec4746
Installing Apache webserver on AWS EC2 instance. yum is the command that is used to install any software in Redhat Linux. httpd is the software used to configure system as webserver.
yum install httpd
We successful installed the httpd software.
To start webservices we need to start webserver using systemctl command.
systemctl start httpd
To check the status of webserver we use following command
systemctl status httpd
We are done with first part step. Let's go to step 2
Document Root(/var/www/html) made persistent by mounting on EBS Block Device.
There are 2 folders provided by httpd.
1./var/www/html
2. /var/www/cgi-bin
For doing front end work like creating static application designs we use html folder and we mostly use html, css, java script...languages at front end development.
Now we need to create new EBS volume. Attach the volume to instance and mount root (/var/www/html) folder to EBS volume. In general, by mounting folder to new volume we can secure data/code. By chance, If the hard disk corrupted we may loose complete data. So it is always recommended to have data in another drive/hard disk.
Creation of new EBS volume
#create EBS volume with 1 GB aws ec2 create-volume --availability-zone us-east-1a --size 1
We have successfully created EBS volume. We can check in AWS Web UI
Now we need to attach EBS volume to instance
#attach instance to EBS volume aws ec2 attach-volume --volume-id vol-06deab5b1a182ff32 --instance-id i-08e94773cc38ef313 --device /dev/xvdf
We successfully attached EBS volume to instance
Now we need to mount root folder to EBS volume. To mount any folder to the device we need to do 3 steps
- create partition of device
- Format
- mount the folder to the device
We can check the volume attached to instance
fdisk -l
1.Creation of partition
fdisk /dev/xvdf
2. Format
mkfs.ext4 /dev/xvdf1
3.mount the folder to device
# command to mount mount /dev/xvdf1 /var/www/html # command to check df -h
Now let's move to step 3...
3.Static objects used in code such as pictures stored in S3
Now we have to create s3 bucket, upload a image/object in a bucket, make the object public and get the URL of the object. Then we need to check weather the web app is work or not. For that we need write html code using URL to display image.
Creation of S3 bucket
Command to create s3 :- # aws s3api create-buckett --bucket mypic333 --region ap-south-1 --acl public-read --create-bucket-configuration LocationConstraint=ap-south-1
Upload an object to the S3 bucket. So for that we have to mention the source path and the destination path. For source path we have to mention the location where the object is stored and for the destination write the bucket name .
# upload image to s3 bucket aws s3 sync "C:\Users\puvvadasahitya\Desktop\allinone\wallpapers" s3:// --acl public-read
To make object public we should click on image name and then click on make public option so it can be viewed/access by everyone .
Now copy the URL and create a sample web app to check webserver is working or not
# go to the html folder cd /var/www/html/ #check list of files ls #create file (in my case it is k.html) vi k.html #enter following html code to display image <body bgcolot='aqua'> sahitya <img src='https://mypic333.s3.ap.south-1.amazonaws.com/1.jpg' height='200' width='200' </body>
Now enter the URL in chrome following way:
https://<public ip of instance>/<name of html file>
Wow it's working. let's proceed to next step.
4.Setting up Content Delivery Network using Cloud Front and using the origin domain as S3 bucket.
Before going to further task we need to know what it is necessity of cloud front ???
Terminology we need to be familiar with....
edge location= smallest data center
Low latency=less time delay
Amazon CloudFront is a web service that speeds up distribution of your static and dynamic web content, such as .html, .css, .js, and image files, to the users. CloudFront delivers the content through a worldwide network of smallest data centers called edge locations. When a user requests content that serving with CloudFront, the user is routed to the edge location that provides the lowest latency , so that content is delivered with the best possible performance.
If the user in a particular zone accessed the url for the 1 st time it takes time as the content should copy from server to the user nearest edge center . After that user in that zone can get content with low latency as the content is copied to the edge center. In general, the content in edge location is stored in the cache memory so by default content will only available for 24 hours in the edge location.
Now we need to create the cloud front distribution for S3 bucket objects.
aws cloudfront create-distribution — origin-domain-name mypic333.s3.amazonaws.com
5.Finally place the Cloud Front URL on the web app code for security and low latency.
We use the domain name as url in html code
As the server distributed the object in all locations. Now client can easily access the services from the server by using server public IP with very low latency & high speed.