CREATING HIGH AVAILABILITY ARCHITECTURE WITH AWS CLI
The architecture includes –
A. Webserver configured on EC2 instance
B. Document Root(/var/www/html) made persistent by mounting on EBS Block Device
C. Static objects used in code such as pictures stored in S3
D. Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
E. Finally place the Cloud Front URL on the webapp code for security and low latency.
Webserver configuring on EC2 instance
Prerequisite : We need to launch ec2 instance then we are going to configure httpd webserver.
What is webserver?
A web server is a computer that runs websites. It’s a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users. This intercommunication is done using Hypertext Transfer Protocol (HTTP).
How to install webserver?
Below commands help to install httpd webserver top on AWS instance
```#yum install httpd```
Prerequisite: If we have to install any packages then yum must be configured
Only installation of web server won’t help us. We have to start services of webserver and then check its status
To start webserver => #systemctl start httpdTo verify server running or not => #service httpd status
Above image indicate we have successfully start and run webserver.But after each reboot to avoid redundancy and to make the services permanently enabled we use following command.
#systemctl enable httpd
Let’s create webpage and check our webserver working or not.
ð Go to file >> cd /var/www/htmlð Create html file using vi text editor >>vi _fileName_.htmlð To see the content of file use >> cat _FileName_.html
Note: Server always store webpages inside /var/www/html directory.
To check content from webUI in search bar use webserver IP with html file name. This will automatically redirect to the webpage.
Creating Persistent Storage By Mounting On EBS Block Device
To store webserver data we require external storage device.
We have to attach EBS volume to our running instance.
What is EBS?
AWS Elastic Block Store (EBS) is Amazon’s block-level storage solution used with the EC2 cloud service to store persistent data. This means that the data is kept on the AWS EBS servers even when the EC2 instances are shut down.
Why we are using external EBS device?
1) There possibility of data loose
2) All data of OS store in ‘/’ drives.
3) If ‘/’ drive corrupted then all data will be lost.
4) To avoid this we will mount EBS device to launched instance and there we can store website content
5) If we want to use storage of EBS volume then volume and EC2 instance must be in same region.
6) Cause order to provide high speed service AWS set constraint.
Note: EBS and EC2 are both independent service. They connected via n/w from ISCSI protocol they share their services.
Before mount we have to check available or attached disk.
#fdisk –l => This command show all devices that connected with instance.
Attaching EBS volume to webserver instance.
Firstly we are going to create EBS volume and below command will help us to create EBS volume.
#aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a
We have created EBS volume successfully. :)
Let’s move to attaching part where we are going to attach EBS volume with our running instance. Below command will help us to attach.
#aws ec2 attach-volume --volume-id vol-077bf0d96dc662cc0 –instance-id i-0a0ed978f5c1b4308 --device /dev/sdf
Prerequisite: Instance Id, volume id and device name
We can see from our webUI the EBS volume is attached successfully. :)
Only attaching volume can’t help us to store the data. We are going to follow some steps to store data in EBS volume.
TO Store Data on EBS volume
Oder to store data on HDD we must follow given steps:
1. Creating partition
Why we create a partition in the storage device?
Partition a disk can make it easier to organize files, such as video and photos, especially if you have a large hard drive. Creating a separate partition for your system files can also help protect system data from corruption since each partition has its own file system.
Checking attached disk #fdisk -l
/dev/xvdf this EBS volume with size 1GiB is attached. Now we are going to create partition for this we have to go inside the volume.
Do you know what is difference between GiB and GB?
When purchasing disk drives, 1 GB is often defined as 1,000,000,000 bytes. … GiB (Gibibytes) is a standard unit used in the field of data processing and transmission and is defined as base 1024 rather than base 1000. For example, 1 GB is defined as 100⁰³ bytes, whereas 1 GiB is defined as 102⁴³ bytes.
We created partition and below image represent steps that I used to create partition.
1. n : To create new partition
2. p: To specify primary partition
3. +500: We are created 500mib primary partition from 1gib
4. w: To save partition.
Partition is created with size 500MiB. Now we can use our volume to store webserver data but before store we have to mount where webserver data going to store.
To store data we must format the volume
#mkfs.ext4 /dev/xvdf1 : This command help us to format
Webserver store data to /var/www/html this location. Now we are mounting our volume to this location
#mount /dev/xvdf1 /var/www/html
# df –hT : we can see the allocated space to web server.
To mount 1stly we have to need directory where we can mount.
In our case we are using this EBS for storing web content.
So we are going to mount in /var/www/html where server store all content of website.
Now there no fear of data loss cause instance volume and web volume both are independent volume.
Creating S3 Bucket To Store Object Data
Note: we use S3 bucket to store object data because Amazon S3 is designed to provide 99.999999999% durability of objects over a given year. This durability level corresponds to an average annual expected loss of 0.000000001% of objects.
Let’s create S3 bucket using cli. To create s3 bucket command is given below
#aws s3api create-bucket --bucket _bucket-name_ --region-name_ _region_ --create-bucket-configuration LocationConstraint=_region-name_
Prerequisite : This command help us to create bucket. In this location constraint is must be added without location constraint command won’t run. This will show u below error.
We specify location constraint that’s why we created bucket successfully
To check the bucket is create or not. Let’s check from webUI. As you can see below s3 bucket successfully added.
In s3 bucket we can add images and video etc. now we are going to add one image inside s3 bucket using cli. Below command help us to add image.
# aws s3 cp _local-file-path_ s3://_bucket-name_
We uploaded the Image. Our image must be public. let’s attach s3 bucket to our webpage.
In html code we given our s3 bucket link with image tag.
To access webpage we have search webserver IP with html file name.
Using Cloud Front From CLI
Get started with Amazon CloudFront. Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment.
Now we are creating cloud front for this we are going to use below command
#aws cloudfront create-distribution --origin-domain-name _s3bucket_name_
Let’s attach link with our html page
Link is added successfully let’s check out form webUI
To access webpage we have search webserver IP with html file name.