Deploying Web Server on AWS using S3 and CloudFront through AWS CLI

Deploying Web Server on AWS using S3 and CloudFront through AWS CLI

In this article,we will see,how to deploy web server on AWS using S3 and CloudFront through AWS CLI.

Services of AWS that will be used

In this,we are going to use following services of AWS:-

1-EC2(Elastic Compute Cloud):-Amazon Elastic Compute Cloud is a part of Amazon.com's cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers on which to run their own computer applications.

2-EBS(Elastic Block Store):-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. 

3-S3(Simple Storage Service):-Amazon S3 or Amazon Simple Storage Service is a service offered by Amazon Web Services that provides object storage through a web service interface. Amazon S3 uses the same scalable storage infrastructure that Amazon.com uses to run its global e-commerce network.

4-CloudFront:-Amazon CloudFront 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.

Requirements

Now,let's list our requirement point to point.

Our requirement is to create a High Availability Architecture through AWS CLI.This architecture includes:-

1-Web Server configured on EC2 instance

2-Document Root(/var/www/html) made persistent by mounting on EBS Block Device

3-Static objects used in code such as pictures stored in S3

4-Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket. 

5-Finally place the Cloud Front URL on the webapp code for security and low latency

How to perform this task??

For this,we need to perform following steps:-

1-First,we have to launch an AWS instance.

Command for this is:-aws ec2 run-instances --image-id "Image ID" --count "Number of instances" --instances-type "Instance type" --key-name "Name of key" --security-group-ids "Security-Group ID"

No alt text provided for this image

We can see this reflecting in Web UI also.

No alt text provided for this image

2-Now,since we have to create a webserver,we also have to attach an EBS Volume to the AWS instance for making our data persistent.

So.let's create an EBS Volume.

Command for this is:- aws ec2 create-volume --availability-zone "Availability Zone in which you want to launch" --size "Size of Volume"

No alt text provided for this image

We can see this reflecting in Web UI also.

No alt text provided for this image

3-Now,after creation of an EBS volume,we want to attach it to our AWS instance.

Command for this is:- aws ec2 attach-volume --device "Device you want to attach" --instance-id "ID of the Instance,volume to be attached" --volume-id "ID of volume"

No alt text provided for this image

We can see this reflecting in Web UI also.

No alt text provided for this image
No alt text provided for this image

4-Now,we are done with provisioning of our AWS instance.

So,for installing Apache Web Server,we have to login into AWS instance through AWS CLI.

For login into the system,run following commands:-

chmod 400 "Name of key"

ssh -i "Name of the key" ec2-user@"Public IP"

No alt text provided for this image

5-Now,we can install Apache Web Server.

Command for this is :- yum install httpd -y

No alt text provided for this image

Now,we can start and enable the services of httpd.

Commands for this are:-

systemctl start httpd

systemctl enable httpd

No alt text provided for this image

6-Now,let's see how many devices are connected to my instance.

Command for this is:-fdisk -l

No alt text provided for this image

7-Now,to use this attached volume,we need to create partition it it.

Command for this is:- fdisk "Name of device"

No alt text provided for this image
No alt text provided for this image

8-Now,we need to format the created partition.

Command for this is:- mkfs.ext4 "Name of device"

No alt text provided for this image

9-Now,mount this partition to /var/www/html where we will be having out HTML Web pages.

Command for this is:- mount "Name of device" /var/www/html

Using command df -h ,we can see that now it is mounted to /var/www/html

No alt text provided for this image

10-But,when we shut down the system,this partition will be automatically unmounted.

So,we need to mount it whenever my instance boots.For this,we use the file /etc/rc.d/rc.local

No alt text provided for this image
No alt text provided for this image

This file runs whenever a system boots,so it will automatically mounts the partition.

11-But,for running this file,we need to make it executable.

Command for this:- chmod +x "Name of file"

We can also check this using command:- ls -l "Name of file"

No alt text provided for this image

12-Now,we can store out HTML Web Page in the directory /var/www/html.

But,it's better to use S3 for storing the static content like images,videos etc. of our webpage.

For that,we need to create S3 bucket for storing static content of my webpage.

Kindly note that, name of S3 Bucket should be unique in a region.

Command for this is:- aws s3 mb s3://"Name of bucket"

No alt text provided for this image

13-Now,it's time to upload the static data to S3 bucket.I am taking this "dp" folder and uploading all the images of the folder and making bucket as public readable.

No alt text provided for this image

Command for this is:- aws s3 cp . s3://"Name of bucket" --recursive --acl public-read

No alt text provided for this image

We can also see that this is reflected in Web UI.

No alt text provided for this image
No alt text provided for this image

14-Now,let's assume,we launched this web server in Mumbai,but some client want to see this from California or Singapore or Virginia.

Definitely,it will take time to reach to Mumbai,and getting back to client.So,we need to create a Distribution which will distribute my static data to all the Edge Locations.

An Edge location is where end users access services located at AWS, the cloud computing division of US-headquartered Amazon. They are located in most of the major cities around the world and are specifically used by CloudFront (CDN) to distribute content to end users to reduce latency.

No alt text provided for this image

For this,we will use AWS CloudFront which will deploy my static data to all the Edge Locations.Also,we don't want to provide the client with the URL of S3 bucket,so ,CloudFront will create a different Domain Name which we can use instead of URL.

Command for this is:- aws cloudfront create-distribution --origin-domain-name "Name of bucket".s3.amazonaws.com

No alt text provided for this image

This will give a domain name which can be used as URL of static data.

15-Now.let's create a simple website.

No alt text provided for this image

16-We are done with our requirement.

Let's take a look at the website using browser.

Type this in the browser:- http://"Public IP"/"Name of webpage"

No alt text provided for this image

So,this is a High Availability Architecture which is created entirely using AWS CLI.


Thank You!!

Stay connected for more articles like that!!!!



To view or add a comment, sign in

More articles by Nilesh Mathur

Insights from the community

Others also viewed

Explore topics