|| EC2+EBS+S3+CloudFront || High Availability Architecture Of Web-Server via AWS CLI

Radhika Sharma
5 min readNov 6, 2020
how cloud front works

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

Let’s Start-

Step -1

  • Create a key-pair :
aws ec2 create-key-pair --key-name mykey --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath mykey.pem
I here used windows-powershell to create key-pair because in my case out-file command was not set in environment variable
  • Show/describe key-pairs:
aws ec2 describe-key-pairs --key-names mykey
In aws CLI I can see my key pair is created
In AWS GUI : created key pair

Step-2

  • Create security-group :
aws ec2 create-security-group  --group-name "myaws-sg" --description "Security group for instances"
  • Set Inbound rules in security-group(Only allow port 22 and 80 that is for ssh and http protocol respectively) :
# aws ec2 authorize-security-group-ingress --group-name "myaws-sg"  --protocol tcp  --port 80 --cidr 0.0.0.0/0# aws ec2 authorize-security-group-ingress --group-name "myaws-sg"  --protocol tcp  --port 22 --cidr 0.0.0.0/0
  • See created security group
aws ec2 describe-security-groups  --group-name "myaws-sg"
Create security-group : In AWS CLI
Security-group with Inbound rules : AWS GUI

Step-3

  • Launch-instance via AWS CLI -
aws ec2 run-instances  --image-id  ami-052c08d70def0ac62  --instance-type  t2.micro  --key-name mykey --count 1 --security-group-ids  sg-057f7e2d1574938bf
launch instance with crated security-group and key-pair
  • Describe or see created instance -
aws ec2 describe-instances --instance-ids i-03a2207160a58cc68
AWS CLI : instance is created
AWS GUI : Created instance

STEP-4

  • Create a EBS Volume of 1GB -
aws ec2 create-volume --size 1  --availability-zone  ap-south-1b
  • Attach This EBS Volume to earlier created instance -
aws ec2 attach-volume  --instance-id i-03a2207160a58cc68  --volume-id vol-07fc492d082d397db  --device /dev/sda2
Volume is created and attached with EC2 instance : AWS GUI

STEP-5

  • ssh into the instance via create key and user-name i.e ec2-user with public ip of instance -
ssh -i mykey.pem ec2-user@13.232.253.155
ssh into the instance
EBS is attached to instance
  • Install apache httpd and start the service via yum command because the instance was having rhel-8 OS & preconfigured yum -
yum install httpd -y  #install httpdsystemctl start httpd  #start the service
  • Disable SElinux — (otherwise we will not be able to access webpages)
disable selinux in instance
  • Create Partition of attach EBS Volume -
create partition in EBS Storage
  • format & mounting in document root folder of httpd server (/var/www/html)-
mkfs.ext4 /dev/xvdb1 #format the partitionudevadm settle #load the drivermount /dev/xvdb1  /var/www/html/  #mounting
mounting the EBS volume in /var/www/html folder

STEP-6

  • Create S3 Bucket -
aws s3 mb s3://mygallery299
make bucket : AWS CLI
Bucket is created : AWS GUI
  • See create bucket in aws-
aws s3 ls
  • Upload Object in precreated Bucket -
aws s3 cp .  s3://mygallery299/ --recursive --include "*.jpg" --exclude "*.css" --exclude "*.html" --acl public-read-write
Upload Objects (Images that I will be used in my web page) in S3 Bucket : AWS CLI
object is uploaded in s3 bucket : AWS GUI

STEP-7

  • Create Cloudfront Distribution —
aws cloudfront create-distribution --origin-domain-name mygallery299.s3.amazonaws.com
Cloudfront Distribution Create : AWS CLI
Distribution is created and link also generated , Now give this link in our web page code : AWS GUI

STEP-8

  • Provide distribution Link in web pages in URL of images -
put this code in webpage program files created in /var/www/html
In the webpage code I gave the cloudfront distribution link of images in image source link of images

STEP-9

  • Now access the web page -
  • Here my gallery page came and all photos was loading from my nearest edge location of AWS and that makes this more powerful and fast .
Access via browser of public ip of instance and web page name
  • This Page now can access from whole world and these all photos will come from origin(s3) if first time request and after on any request pages come from the local cache of nearest edge location of client till TTL(time to live) of local cache set in cloudfront distribution…..A Great Concept of : Content Delivery Network….

DONE !!

PS : Here , I would like to thank vimal daga sir to give this task and to teach us all these concepts.. #ARTH task-6

HAPPY CLOUD LEARNING..

THANKS FOR SCROLLING…

--

--