Creation Of High Availability Architecture with AWS-CLI >-

Rahulbhatia1998
7 min readMar 21, 2021
Cloud Front

Task Description📄

🔰 Create High Availability Architecture with AWS CLI 🔰

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

Prerequisites:

  • Created an AWS account.
  • Install AWS CLIv2 in OS.
  • Configure AWS CLIv2 with IAM user.
  • Some Basics of How to configure WebServer

What is a 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.

The primary function of a web server is to store, process and deliver web pages to clients. The communication between client and server takes place using the Hypertext Transfer Protocol (HTTP). Pages delivered are most frequently HTML documents, which may include images, style sheets and scripts in addition to the text content.

Web Server

What is EBS?

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. A broad range of workloads, such as relational and non-relational databases, enterprise applications, containerized applications, big data analytics engines, file systems, and media workflows are widely deployed on Amazon EBS.

For eg : pendrive

What is S3?

Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics. Amazon S3 provides easy-to-use management features so you can organize your data and configure finely-tuned access controls to meet your specific business, organizational, and compliance requirements. Amazon S3 is designed for 99.999999999% (11 9’s) of durability, and stores data for millions of applications for companies all around the world.

What is CloudFront?

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment. CloudFront is integrated with AWS — both physical locations that are directly connected to the AWS global infrastructure, as well as other AWS services. CloudFront works seamlessly with services including AWS Shield for DDoS mitigation, Amazon S3, Elastic Load Balancing or Amazon EC2 as origins for your applications, and Lambda@Edge to run custom code closer to customers’ users and to customize the user experience. Lastly, if you use AWS origins such as Amazon S3, Amazon EC2 or Elastic Load Balancing, you don’t pay for any data transferred between these services and CloudFront

What Does The Architecure Include?

Step 1 : Webserver configured on EC2 Instance

aws ec2 run-instances — image-id <ami of the Image > — instance-type <_type_id> — count <no_of_instance> — security-group-ids <group_id> — key-name <key_name>

You can replace the value within <> with your own values

I have created an EC2- instance in Oregon(US-west-2)

Confirmation of instance

Now , in this newly launched instance, we will configure the web server.

for this Task, I am making use of the HTTPD Apache Web server

Now log into your instance and as a Super User install the webserver.

yum install httpd

Yum is the package manager tool used in RHEL/CENTOS

systemctl start httpd

systemctl enable httpd

This will start the httpd server and always keep it enabled, even when the EC2 instance restarts.

Now the basic steps to install any web server are as follows:

  1. Install the webserver : We have done this step above when we installed the apache httpd webserver with yum.
  2. Configure the WebServer: After you have installed the webserver, now you can configure it by going to this directory.
    /var/www/html
    Here you can add your respective HTML files that required to set up based on business requirement.
  3. Run the Webserver : Now you can run the webserver using

systemctl start httpd

systemctl enable httpd

This will start the httpd server and always keep it enabled, even when the EC2 instance restarts.

You can see your respective webpage that is hosted on your public IP.

Take for example if your webpage is index.html, you can view it using Public_IP/index.html

step 2: 1GB EBS Volume which serves as Document Root Device.

After creating EBS Volume let’s attach it to the EC2 Instance

To Create an EBS Volume using AWS CLI:

aws ec2 create-volume — avalability-zone <zone id> — size <number (it is in terms of GB)>

Note: we need to specify Avalability zone which create an EBS volume because EBS is zone-specific.

It is also a good practice to keep the Avalability Zone and EC2 instance within the same Region

Now after the EBS volume is created , we need to attach this volume to the Instance that we created before.

aws ec2 attach-volume — instance-id <instance id of the EC2> — volume-id <volume_id of the EBS instance> — device /dev/sdf

You can refer to the AWS Web UI now, to confirm if the EBS volume is created and installed.

3) To make Document root /var/www/html of web server we have to mount it on EBS Volume.

To use any external volume first we have to Create Partition then Format it and after that we can mount.

  1. Creating the partition :

fdisk /dev/xvdf

The new volume is attached to this device -> /dev/xvdf

2. Format the Volume :

To format, we use → mkfs.ext4 /dev/xvdc1 command.

Here I have used ext4 to create an I-node table, you can refer to other format types as well based on your business requirement.

3. Mount the Volume to /var/www/html: Document root

Create S3 bucket

Create the S3 bucket with public-read for now, region and bucket configuration.

S3 bucket is created successfully. Now, we’ll upload the static files to the bucket.

Upload the file

Upload the files using the following command with public read permission for now.

Update the S3 url in the /var/www/html/index.html file.

Webpage is working now and fetching the image from the S3 bucket. You can see the webpage using the public IP address as the URL.

Create Cloudfront distribution

Why do we need the Cloudfront distribution?

Cloudfront is the CDN(Content delivery network) as a service. It caches the static objects in the local data centres that improves the user experience and also isolates the S3 bucket from the public network and provides extra layer to the S3 bucket and improves security. We can also attach the S3 bucket directly to an EC2 instance with some other way like creating an IAM user profile and add permissions to it. But, here we’ll not gonna discuss this.

Create the cloudfront distribution using the following command by specifying the S3 origin.

Cloudfront distribution is created successfully. Now, just update the URL to the /var/www/html/index.html file.

Edit policy

Block the public access of the S3 bucket. We’ve created the Cloudfront, so the data will be fetched successfully without even having the public access to the S3 bucket.

Now, S3 bucket has only private access and it’s safe now.

We can check the user traffic from the Cloudfront dashboard.

Finally, we’ve created the high availability architecture using the Cloudfront with AWS CLI.

Thanks for reading, if you like it, do give a clap :)

--

--