Create a High Availability Architecture Using AWS CLI

We all know how Cloud Computing is ruling the market today, with business-critical functions and infrastructure being migrated from the legacy infrastructure to the Cloud. This is because of the immense functionality and ease of use that Cloud Providers like AWS, Azure, Google have built for us.

This article briefly talks about how can one create a high availability architecture to reduce the latency involved in serving the requests.

A Quick Overview:

The architecture includes-

1. Webserver 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 the S3 bucket.

5. Finally placing the Cloud Front URL on the web app code for security and low latency.

We will be using the AWS CLI terminal to perform these tasks. If you are new to AWS CLI, I recommend you follow a basic demo discussed in a previous blog.

Step 1: Create an EBS volume

aws ec2 create-volume --availability-zone <zone> --size <size-in-GB>  
--tag-specifications ResourceType=volume,Tags[{Key="key",Value="value"}]

Step 2: Launch an EC2 instance

Note: I am using the existing key, Security Group. If you don’t have one, create one by referring to this blog.

aws ec2 run-instances --image-id <ami-id> --instance-type <type>   
--key-name <name> --subnet-id <id> --security-group-ids <sg-id> --count 1

Step 3: Attach the EBS volume to the instance

aws attach-volume --volume-id <volume-id> --instance-id <instance-id>

Step 4: Log in to the instance, format the disk attached, and persistently mount the document root on it.

Step 5: Create an S3 bucket

aws s3api create-bucket --acl <rule> --bucket <name> 
--create-bucket-configuration LocationContraint=<zone>

Step 6: Upload an image to the bucket

aws s3 cp <image/file> s3://<name-of-bucket>/<name-of-file> 

Step 7: Create a Cloud Front Distribution

What is Cloud Front?

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.

aws cloudfront create-distribution --origin-domain-name <s3-bucket-url> --defaualt-root-object <name-of-file>

Since our server cannot be close to every requesting host in the world, a significant factor of concern is latency. What we get from AWS CloudFront Service solves our problem here. It sets up CDN for our data (here an S3 object). The data travels through a high-speed fast private AWS network and gets stored in the edge locations nearest to the requesting client, thus, reducing latency to great extent.

We copy the URL specified by the DomainName in the output and use that URL to display the image in our web app.

For doing so, go to /var/www/html/ directory. Create an index.html file and use the basic HTML syntax for displaying an image.

That’s it. You have successfully created a simple yet high availability architecture.



Software Engineer | DevOps Enthusiast | Writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store