Using AWS-CLI, Create a High Availability Architecture on AWS

Rohan Parab
5 min readMar 19, 2021

Hello Connections,

I have come with another crispy article. The article creates a High Availability Architecture in Amazon Web Services. It performs practical using the aws-cli. If you don’t know about AWS-CLI, you can visit my previous article “INTERACTING WITH AWS-CLI, A STEP FOR AUTOMATION

This article covers a basic-web site hosted on AWS. The website will be created on an EC2 instance. And the static files will be served using S3 and CloudFront. The web pages will be store in an EBS volume for easily moving and taking snapshots. The practical is performed on a free tier account and services from the free tier.

Steps for creating an architecture:

  • Create an EC2 instance
  • Create an EBS Volume
  • Make volume ready to store data
  • Create an S3 Bucket(S3- Simple Storage Service)
  • Upload files to S3 bucket
  • Create a CloudFront Distribution
  • Install Apache HTTPD Web-Server
  • Mount an EBS Volume to Web-Server Document_Root
  • Create template
  • Start the WebServer
  • Visit the WebSite

Create an EC2 Instance

To create a web-server we need an OS. So let’s create an EC2 instance using CLI. Make sure that you have correctly configured your aws-cli with the proper working region. The following command creates a t2.micro instance with the Amazon Linux 2 AMI.

$ sudo aws ec2 run-instances --image-id ami-08f63db601b82ff5f --instance-type t2.micro --subnet-id subnet-d20d04ba --security-group-ids sg-036cbf64 --key-name arth_practice_acc
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-08f63db601b82ff5f",
"InstanceId": "i-0f3d7e0daaa793f98",
"InstanceType": "t2.micro",
"KeyName": "arth_practice_acc",
"LaunchTime": "2021-01-03T07:17:11+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-south-1a",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-35-144.ap-south-1.compute.internal",
"PrivateIpAddress": "172.31.35.144",
"ProductCodes": [],
"PublicDnsName": "",
"State": {
"Code": 0,
"Name": "pending"
},
"StateTransitionReason": "",
"SubnetId": "subnet-d20d04ba",
..
..

Once the instance is created, get it’s instance-id. We will require it in going further.

Create an EBS Volume

The purpose of creating EBS volume is to store all the web pages. This EBS volume is persistent as it won’t be deleted even if the server is terminated or gets crashed due to any reasons. Also, we could mount it on another instance and start the services from that instance quickly. This would make the website highly durable on an AWS EC2 machine. The following command creates an EBS Volume with 1 GB of Storage in the Mumbai-1a subnet

$ sudo aws ec2 create-volume \
> --size 1 \
> --availability-zone ap-south-1a
{
"AvailabilityZone": "ap-south-1a",
"CreateTime": "2021-01-03T07:26:00+00:00",
"Encrypted": false,
"Size": 1,
"SnapshotId": "",
"State": "creating",
"VolumeId": "vol-042db8ddd87e0744b",
"Iops": 100,
"Tags": [],
"VolumeType": "gp2",
"MultiAttachEnabled": false
}

Once the EBS volume is created we have to attach it to EC2 instance which we have created.The basic arguments required to perform attachment are the device name, the id of the instance to attach to, and the id of the volume created in the above step.

the following cmd is to perform the attaching of EBS volume:

aws ec2 attach-volume --device /dev/sdv --instance-id i-0f3d7e0daaa793f98 --volume-id vol-042db8ddd87e0744b
{
"AttachTime": "2021-01-03T07:30:57.734000+00:00",
"Device": "/dev/sdv",
"InstanceId": "i-0f3d7e0daaa793f98",
"State": "attaching",
"VolumeId": "vol-042db8ddd87e0744b"
}

Make volume ready to store data

After the volume is attached, connect to instance using ssh or the cli (using browser).Note that, only Amazon Linux 2 has the facility of using the CLI via a browser.

The following commands create a partition of 500M and format it with the ext4 file system:

  1. for creating partition

2. For formatting the partition with ext4 file system

Create an S3 Bucket(S3- Simple Storage Service)

Some static files are required for our website so we will be uploading this to S3 bucket. It is web service provided by AWS which provides storage over internet. It is highly-scalable and secure in the cloud.

The following cmd creates a bucket in the Mumbai region. Regions outside of us-east-1 require the appropriate LocationConstraint to be specified in order to create the bucket in the desired region:

$ aws s3api create-bucket --bucket rohanparab516clibuck  --create-bucket-configuration LocationConstraint=ap-south-1
{
"Location": "http://rohanparab516clibuck.s3.amazonaws.com/"
}

Upload files to S3 Bucket

The images which I want to upload are stored in my local directory. The following command uploads all the objects from a Local Directory to the S3 Bucket. Just make sure that you make your uploaded objects publicly readable.

$ aws s3 cp  ~/files_to_upload_to_s3/  s3://rohanparab516clibuck/  --recursive  --acl public-read

Create a CloudFront Distribution

Amazon CloudFront is a fast cast 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.

The following command creates a CloudFront distribution for our above-created S3 bucket. The only argument required is S3 bucket_name

$ aws cloudfront create-distribution \
> --origin-domain-name rohanparab516clibuck.s3.amazonaws.com
"Id": "rohanparab516clibuck.s3.amazonaws.com-1609677851-893925",
"DomainName": "rohanparab516clibuck.s3.amazonaws.com",

The images could be accessible from the url http://<cloud_front_domain>/<object-name.ext> once uploaded to the bucket.

Install Apache httpd Web-Server

To grab the static file we need a web-server. So, we are using the Apache Httpd software. For doing that, first connect your instance and install the apache httpd software on it.

# yum install httpd -y

Mount an EBS Volume to Web-Server Document_Root

Document_root of httpd is “/var/www/html/

Mount the 500M created partition to “/var/www/html/”

Create a Template

Create a index.html webpage that will grab our images

index.html

Start the Web-Server

# systemctl start httpd

For, checking status

systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-01-03 12:33:45 UTC; 3h 37min ago
Docs: man:httpd.service(8) Main PID: 2866 (httpd)
Status: "Total requests: 74; Idle/Busy workers 100/0;Requests/sec: 0.00567; Bytes served/sec: 6 B/sec"
CGroup: /system.slice/httpd.service ├─2866 /usr/sbin/httpd -DFOREGROUND ├─2891 /usr/sbin/httpd -DFOREGROUND

Visit the Website

Hence, the High Availability Architecture on AWS.

Thanks!

--

--