How to create High Availability Architecture using S3 and CloudFront?
Tags: aws, cloud, tutorial
Published: 2022-12-05 06:02:14

## What is High Availability?

A highly available architecture involves multiple components working together to ensure uninterrupted service during a specific period. This also includes the response time to users’ requests. Namely, available systems have to be not only online, but also responsive.

Implementing a cloud computing architecture that enables this is key to ensuring the continuous operation of critical applications and services. They stay online and responsive even when various component failures occur or when a system is under high stress.

Highly available systems include the capability to recover from unexpected events in the shortest time possible. By moving the processes to backup components, these systems minimize downtime or eliminate it. This usually requires constant maintenance, monitoring, and initial in-depth tests to confirm that there are no weak points.

> Example: 

If you want to deploy a website and want your website to be fast, secure and available all the time even in the time of some failure in the network then High Availabilty Architecture comes in play.

It is important to create High Availability Architecture to work efficiently without any delay due to failure.

> In this Blog we will perform following activities:

- We will Launch an instance using AWS CLI
- We will configure Webserver
- We will Create an EBS Volume
- We will Attach EBS Volume
- We will Create Partition
- We will create S3 Bucket
- We will Create Cloudfront Distribution

### Prerequisites
- You should have AWS account, you can create AWS free tier account.
- Install AWS CLI in Windows,Mac or in Linux.
- Configure AWS CLI with IAM user.
- It is preferred that you are aware HTML basics and Linux

### Step 1: 
> Launch an Instance using AWS CLI

- First configure the AWS CLI in your terminal or window cmd, here I'm using window cmd. 
- To configure provide Access Key, Secret Key and region name.


Now to launch an Instance, we require:

- Image-id
- Instance-type
- Count i.e how many instance we want to launch
- subnet-id
- Security-Group-id
- Key-name

> Syntax:

`aws ec2 run-instances --image-id ami_ --instance-type _type_id --count no_of_instance --subnet-id -a57e77cd --security-group-ids group_id --key-name key_name`


![aws cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kn0rqanj6clrf03of577.png)


> output:


![aws cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wf1rjzu2upbw9db63d2k.png)

### Step 2: 
> Configure Webserver

- To Configure Webserver we need to install **httpd** i.e Apache Webserver in instance that you have launched in Step 1

- To install httpd use command: `yum install httpd`

- After httpd installation, we need to check whether service is started or not.

- To check the httpd service use command: `systemctl status httpd`


![httpd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4rhaou0hrk7spgiaxb0.png)


- And to start the service use the command `systemctl start httpd`


![httpd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dib1sbbxlvoi7sszz03p.png)


### Step 3:
> Create an EBS Volume

**To Create an EBS Volume we require:**

- Availability Zone name
- Size of Volume that we want to create.

> Syntax:

`aws ec2 create-volume --availability-zone ap-south-1a --size 1`

Here **ap-south-1a** is availability zone, you have to give availability zone name where you have launched instance and 1 is the size of volume i.e 1Gb, you can give desired size to your volume.


![ebs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/escinu2bc59b84wudk23.png)

### Step 4: 
> Attach EBS Volume to Instance

**To attach the EBS Volume to an Instance we require:**

- Volume-id (The volume that we have created in step 3)
- instance-id (The instance that we have launched in step 2)
- device (Example: /dev/sdf)

> Syntax:

`aws ec2 attach-volume --volume-id (volume_id) --instance-id (instance_id) --device(ex: /dev/sdf)`


![attach_ebs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlylquo2dqsvj9uhd6zn.png)


### Step 5: 

> Create and Format a Partition

- Now open the instance that you have launched in step 1

- run the command `fdisk /dev/xvdf` to create partition.

- After running this command **press n** to create new partition and then press p and then enter.

- To save the partition press `w`.


![partition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txzvt1jo9b2hdoiw2rlh.png)

-Now we need to format the partition.

- To format the partition use the command `mkfs.ext4 device_name`


![format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq543oyzni9q3u7r21l3.png)

### Step 6:

- Mount `/var/www/html` directory to partition
- We need to mount /var/www/html directory to partition because we will create our html page in this directory and it will be safe in this partion even in the time of instance failure.
- To mount the directory use the command mount `/dev/xvdf /var/www/html`
- And to check it is mounted or not use command `df -h`


![mount](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bp3de0klbbmctcshfxi.png)

- Now create HTML page, go inside directorye🌟 /var/www/html in your linux instance.

- Create a HTML file.

**Syntax:**

```
<!DOCTYPE html>
<html>
<head>
<style>
img {
  width: 100%;
}
</style>
</head>
<body>
<img src="html5.png"  width="128" height="128">
<img src="html5.png" width="128" height="128">
</body>
</html>
```
 
- Now add your own content to the syntax above and create your own html webPage.


### Step 7:
> Create S3 Bucket

**To create S3 Bucket we require:**

- bucket name(you can give any name)
- region name(region name should be same like your instance region)

> Syntax:

`aws s3api create-bucket --bucket --region --create-bucket-configuration LocationConstraint= region`


![location](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjpocr4ydcce2bpplac0.png)

### Step8:
> Upload images in S3 Bucket

The images that you will use while creating website or webpage, store that images in S3 bucket so that images in your website will open with less latency or delay.

- Go to the AWS service in AWS WebUi
- Select S3 service
- Now select your bucket that you have created in step 7
upload images
- Make the images that are uploaded public so that all can see your images.

![bucket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b49lvk5763edyxm5bnh9.png)

- Now copy the link of the images in the S3 Bucket and paste that link in your webpage.


![Image link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fphmlf0t71wse0tqkcqm.png)

### Step 9: 
> Create Cloudfront Distribution

We create **Cloudfront** Distribution so that everyone in the world can access to our website with same speed like we access locally.

We will make Cloudfront Distribution of S3 bucket so that images will open without any delay when someone will access the website from other countries.

> To create Cloudfront Distribution we require:

- origin-domain-name(S3 Bucket domain)

> Syntax:

`aws cloudfront create-distribution — origin-domain-name`


![CF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzdxjvyr2ybogenxpymx.png)

**Output:**


![cf out](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynla6om6j1p4fjk4bgkg.png) 

- Now open the webpage using the Ip address of instance and file name in your browser .


![WebPage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdovfxw9bfy1kqt0zbc8.png)


**By performing all steps you can deploy your website with High availability,secure your website and make your website manageable.**

Hope you find this blog insightful, Do give it a like 🌟

**Thank you!**