Configuring Web Server and Designing High Availability CloudFront Distribution Using AWS CLI.

Sourav Dinda
6 min readApr 5, 2021

--

What is AWS CLI?

AWS CLI is a tool that provides easy control of multiple AWS services with a single tool, pulling all AWS services together into one central console. The summary is because of the Amazon Web Services command line interface because, as its name suggests, users manage it from the command line. It allows you to manually control services or automate them with powerful scripts.

Task Description:

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

All the above steps must be performed using AWS CLI

🔅 First of all we have to launch one EC2 Instance in which we configure the Apache Webserver.

First you need to type the aws Help command. This command shows the names of all the services. You should know that the example running sub-service comes under EC2 service so Types Aws ec2 help. here you find many options. Now you look for the run-instance option in it. Now type aws ec2 run-instance help.

Here you can see the multiple options so Finally you find this — image-id, — instance-type, — count, — security-group-ids, — subnet-id and — key-name options after giving the information about these options your command completed.

command :

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

Let’s see on AWS CONSOLE our instance is launched or not ?

hurrah !!!! it’s sucessfully launched .

🔅 Now we create an EBS volume of 1 GB :

First of all you have to type aws help command this command shows all the service names. You have to know that volume creation comes under ec2 service so type aws ec2 help here you find many options.Now you find the option create-volume in it. Now type aws ec2 create-volume help. Finally you find this availability-zone, size and volume-type option giving the information about these options your command completed.

command :

#aws ec2 create-volume — volume-type <value> — size <value> — availability-zone <value>

Let’s check on AWS CONSOLE our EBS volume is created or not ?

🔅Now we attach the above created 1 GB of EBS volume to the AWS EC2 Instance :

command : #aws ec2 attach-volume — instance-id <value> — volume-id <value> — device <value>

Let’s check on AWS CONSOLE our EBS volume is attached or not ?

🔅 Firstly, we will install Apache httpd webserver on our Instance launched on AWS.

command : yum install httpd

🔅 To make Document root /var/www/html of apache web server

✔ First we have to create partition

command : fdisk /dev/xvdh

✔ Second we have to Formatting the partition

command : mkfs.ext4 /dev/xvdh1

✔ Last we have to Mount this partition with the the folder /var/www/html/

command : mount /dev/xvdh1 /var/www/html

✔ start httpd server

command : systemctl start httpd

🔅Now for more availability we need to keep the static content in the S3 bucket like the image used in the web code. For this we need to create an S3 bucket.

✔ To create S3 Bucket AWS CLI has Command as -

Let’s check s3 bucket is created or not ?

🔅 Now we can upload our objects / contents inside the S3 bucket. We have this command to upload or copy local static content or files:

aws s3 cp {file_path} s3://{bucket_name}

Let’s check object is successfully uploaded or not ?

✔Using the URL provided to the object by S3 we can now access the Image .

https://sourav83.s3.ap-south-1.amazonaws.com/dp.jpg

🔅Since our image was uploaded to the bucket, we now create a cloudfront distribution for the S3 bucket.

command : aws cloudfront create-distribution — origin-domain-name

Let’s check it’s distributed or not ?

We see that Cloudfront provides a domain name or unique URL to access content with faster speed and less delay. Now we need to give this cloud front url in the code of the image instead of the s3 url.

✔Our Cloudfront Distribution for S3 bucket is created, We will recieve all the information about distribution.

🔅Create a html file under /var/www/html/ folder

🔅now I am going to use web server using any browser

Link : ip_address/file_name

here my link is : 13.235.241.30/sourav.html

✔source code of this page :

Finally we can see that the part of the web page where the image is coming from AWS Cloud Front CDN as a part of the web page our web page is now becoming faster in terms of speed. The second point is that it is not possible for anyone to know the exact path of the object stored in S3.

🔹 First Know your requirement then do your implement 🔹

🔹 KEEP SHARING IF YOU THINK THAT IT’S HELPFUL . 🔹

🔹 THANK YOU 🔹

Thanks for reading this!!

--

--