Create High Availability Architecture with AWS CLI | CloudFront | S3 | EBS .

Yagyandatta Murmu
5 min readNov 14, 2020

Hello connections, In this blog I’m going to explain all the step process of how we can deploy an web server on the top of AWS with S3, EBS and CloudFront services through windows command prompt.

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 the 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.

Step1:- Configuring AWS CLI

Creating key-pair and converting the output in .pem format :

A key pair, consisting of a private key and a public key, is a set of security credentials that we use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and we store the private key. we use the private key, instead of a password, to securely access our instances.

To create a key-pair through AWs cli :-

aws ec2 create-key-pair --key-name <key_name> --query KeyMaterial > awskey.pem --output text > <key_name>.pem

Automate with python :-

import osprint("Creating Key-pair...")os.system("aws ec2 create-key-pair --key-name ec2_key --query KeyMaterial > awskey.pem --output text > ec2_key.pem")
command prompt
AWS

Creating a security group with Inbound rules :-

A security group acts as a virtual firewall for our EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to our instance, and outbound rules control the outgoing traffic from our instance.

To create a Security group through AWs cli :-

aws ec2 create-security-group --group-name <value> --description <value> 

Automate with python :-

import osprint("Creating security group...")os.system("aws ec2 create-security-group --group-name secure_ec2 --description this_group_has_all_trafic_rule  ")

Adding inbound rule :-

aws ec2 authorize-security-group-ingress  --group-id <value> --cidr <value> --protocol <value>

Automate with python :-

import osprint("Adding security rules...")os.system("aws ec2 authorize-security-group-ingress --group-id sg-08b10335671bd1705 --cidr 0.0.0.0/0 --protocol all ")
security group
Inbound Rule

Launch the ec2 instance :-

aws ec2 run-instances  --image-id  <value>  --instance-type  <value> --count <value>  --subnet-id <value> --security-group-ids <value>   --key-name  <value>

We can Automate with python script:-

we have to store the python code in a file . In my case I have stored this code inside rhel_os.py

import osprint("launching instance")os.system("aws ec2 run-instances --image-id  ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-b86168d0 --security-group-ids sg-08b10335671bd1705 --key-name ec2_key")
cmd
web

Creating a volume of 1GB in EBS :-

My requirement is to create a volume of 1GB in EBS in ap-south-1a(Mumbai) availability zone.

aws ec2 create-volume --availability-zone <value> --size <value> 

Automate with python :-

import osprint("Creating EBS Storage...")os.system("aws ec2 create-volume --availability-zone ap-south-1a --size 1")

Attach Volume with the respective instance :

aws ec2 attach-volume  --volume-id <value> --instance-id <value>    --device <value>

with python automation :-

import os
print("Attaching EBS to EC2...")
os.system("aws ec2 attach-volume --volume-id vol-05fc978f27b679e7d --instance-id i-083a731e9dbbcb899 --device /dev/sdf ")

📎Configuring Apache Webserver:-

#Installing Apache server :>> yum install httpd

Starting the service of HTTPD !!

#To start or enable httpd service
>> systemctl start/enable httpd
# To check the running status
>> systemctl status httpd

Mounting EBS volume to ec2 instance :-

To make the data persistent we have to mount the EBS and format it,

#Creating a new partition 
>> fdisk /dev/xvdf
>> n (for new partation)
>> p (for primary partaion)
>> 1 (partation number)
>> w (write and save)
#formatting EBSstorage to use
>> mkfs.ext4 /dev/xvdf1
creating a new partion
formatting

Mount to our destination folder :

>> mount  /dev/xvdf1   /var/www/html

Now we need to create a S3 bucket :

aws s3api  create-bucket --bucket <value> --create-bucket-configuration LocationConstraint= <Specifies the Region where the bucket will be created>

With Python Script :

import osprint("launching s3 bucket...")os.system("aws s3api create-bucket --bucket  yagyandatta --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1")

Now we have to put data inside our s3 bucket:

aws s3 cp <localPath> s3://<>s3blockName/<fileName>

Now, we can proceed with CloudFront creation :

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency and high transfer speeds.

aws cloudfront create-distribution   --origin-domain-name <value>  --default-root-object <value>

Automate with python :

import osprint("launching cloudFront bucket...")os.system("aws cloudfront create-distribution   --origin-domain-name yagyandatta.s3.amazonaws.com  --default-root-object sonu2.jpg")

Now we have to configure web server on the top of cloud.

#you must be inside root 
>> cd /var/www/html
#create a html file
>> vim index.html
#write your data and save it ..

Final Output :

on web

Thanks for visiting me :)

--

--

Yagyandatta Murmu

Devops || MlOps || Flutter || Web Development || PYTHON || Data Science || AWS cloud || GCP || Azure