Create High Availability Architecture with AWS CLI

Task Description📄
-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.
PREREQUISITE:-
You required only two things for performing this task 1. You should have account on AWS. 2. You have installed AWS Cli in your pc.
STEP1:-
Credential For Login :-
For login through AWS cli into your AWS Console you have to Access Key and Secret key for this with the help of these credential you can easily login in your console.
STEP2:-
CREATE KEY PAIR AND SECURITY GROUPS:-
For launching the instance on Aws you have to required some things like you have to created first Key pair and Security group for this.
What Is Key Pair:- A key pair, consisting of a private key and a public key is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public Key, and you store the private key. You use the private key, instead of a password, to securely access your instances.
for creating this key pair via cli you have to type one command on your cli.
aws ec2 create-key-pair — key-name awslog1
I give here awslog1 here as a name of my key pair you can choose your own.

What Is Security Groups-
A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. When you launch an instance, you can specify one or more security groups.
for creating this Security Group via cli you have to type one command on your cli.
aws ec2 create-security-group — group-name MySecurityGroup5 — -description “My security aws”
here is MySecurityGroup5 the name of my security group you can give whatever you like.and description is also you can give in my case description have “My security group”

STEP3:-
LAUNCH EC2 INSTANCE:-
What Is Ec2 Instance:-
An instance is a virtual server in the AWS cloud. With Amazon EC2, you can set up and configure the operating system and applications that run on your instance. To get started with a Windows instance.
After creating Key Pair and Security Group on Aws now we can launch Aws instance. before launching the instance you have gather some your instance information like :
1. Name of instance(centos,windows,redhat,linux,etc), here we launch our instance from cli so we have to use Image id of our instance which you want to launch. in my case name of my instance is Red hat so for this instance, i use Image Id ami-04403849925d20b6a.
2. How many instance you want to launch so for this we have to give math matical number throw the count.
3.Aws provide multiple type of instance so you have to choose type of your instance i used t2.micro.
4. Now we utilize our Key Pair and Security Groups which we created above in step2 from Aws cli command.
for launching the instance you have to type one command on aws cli.
aws ec2 run-instances — image-id ami-04403849925d20b6a — count 1 — instance-type t2.micro — key-name awslog1 — security-groups MySecurityGroup5

so finally now we launch our instance on aws successfully now we move on to our next step.
STEP 4:-
CREATE EBS VOLUME:-
Now time’s come to create our Ebs volume . so firstly we should know that what is ebs volume.
EBS VOLUME:- Amazon Elastic Block Store (EBS) is an easy to use, high performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale.
ATTENTION:- you should consider one thing at the time of creating your ebs volume throw aws cli your volume should be in the same Availability Zone ,in which your Aws instance launch. means your EBS and EC2 INSTANCE Should be in the same region.
for creating the ebs volume you have to type this command
aws ec2 create-volume — volume-type gp2 — size 5— availability-zone ap-south-1a

size of ebs you can give according to your need.
STEP 5:-
ATTACH EBS VOLUME INTO EC2 INSTANCE:-
For attach Ebs volume to Ec2 instance we have to require some id’s for this 1. Ebs Volume Id 2. Instance Id
device:- we give the device name unique because with the help of device name your ebs volume is connect to the instance: i give my device name /dev/sda2.
after collecting the data which is mention above now you can type command for this.
aws ec2 attach-volume — volume-id vol-078f8ba93efb13055 — instance-id i-04403849925d20b6a — device /dev/sdf

Output:-
here above you can see that our ebs volume has been successfully attached to our Ec2 instance.
STEP 6 :-
CREATE S3 BUCKET:-
S3 BUCKET:-An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services’ (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata.
COMMAND FOR CREATE S3 BUCKET USING AWS CLI:-
aws s3api create-bucket — bucket mybucckettrr — region ap-south-1 — create-bucket-configuration LocationConstraint=ap-south-1

STEP 7 :-
UPLOAD DATA IN S3 BUCKET :-
To upload the files or projects, execute the below command in CMD.
aws s3 sync “{YOUR-LOCAL-PATH}” s3://{YOUR-BUCKET-NAME}
aws s3 sync “C:\Users\HP\Pictures\Saved Pictures\whatsaap pic\New folder” s3://mybucckett — acl public-read

STEP 8 :-
CREATE CLOUD FRONT FOR S3 BUCKET:-
WHAT IS CLOUD FRONT:-
Amazon Cloud Front is a content delivery network offered by Amazon Web Services. Content delivery networks provide a globally-distributed network of proxy servers which cache content, such as web videos or other bulky media, more locally to consumers, thus improving access speed for downloading the content
CMD FOR CREATE CLOUDFRONT FOR S3 BUCKET
aws cloudfront create-distribution — origin-domain-name mybucckett.s3.ap-south-1.amazonaws.com

At the time of creating cloud front you have to give only your origin-domain-name of your bucket (mybucckett.s3.ap-south-1.amazonaws.com).
STEP 9:-
LAUNCH AND START WEB SERVER IN EC2 INSTANCE:-
What is web server:-
A web server is server software, or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web. A web server can, in general, contain one or more websites. A web server processes incoming network requests over HTTP and several other related protocols.
Command for launch web server in ec2 instance :-
yum install httpd

Command for start web server in ec2 instance :-
systemctl start httpd

MOUNT EBS VOLUME INTO /VAR/WWW/HTML FOLDER:-
Why we mount ebs into html folder of web server:-
we mount this ebs volume in webserver folder if in future due to any reason our main drive will be crash then our webserver data will also lost so for this we mount html folder to the new ebs volume.
for mount this volume we have to follow 3 steps which is given below:-
- Partition
- formation
- mount
PARTITION
CMD FOR THIS:-
syntax:Fdisk ebs drive name
Fdisk /dev/xvdf

FORMAT
CMD FOR THIS:-
Syntax: mkfs.ext4 drive name
mkfs.ext4 /dev/xvdf

MOUNT
CMD FOR THIS:-
Syntax: mount drive name folder name
mount /dev/xvdf1 /var/www/html
now your ebs has been successfully mount to the webserver html folder.
STEP 10:-
CREATE HTML FILE INSIDE /VAR/WWW/HTML FOLDER OF WEB SERVER:-
You can see here inside the folder of html i create one pm.html file


and inside the source code at the place of img src i give one cloud front url this url i got when i create cloud front for s3 bucket by this url you can easily access your data(img or file) in across the world.
OUTPUT:-

Thanks for reading……………………………………………………