High Availability Architecture with AWS CLI

Yash Hirulkar
FAUN — Developer Community 🐾
8 min readApr 22, 2021

--

The AWS Command Line Interface (AWS CLI) is an open-source tool that enables you to interact with AWS services using commands in your command-line shell. With minimal configuration, the AWS CLI enables you to start running commands that implement functionality equivalent to that provided by the browser-based AWS Management Console from the command prompt in your terminal program.

🤔Why AWS CLI?

AWS CLI gives you the ability to automate the entire process of controlling and managing AWS services through scripts. These scripts make it easy for users to fully automate cloud infrastructure. Prior to AWS CLI, users needed a dedicated CLI tool for just the EC2 service.

💥Installing AWS CLI :

Installation of AWS CLI is so simple. You just need to download the application from the below mentioned link and like we install any other application, just run the application and keep on clicking and it will be installed.

✔Check if it has installed properly :

aws --version

💥Configuring AWS CLI :

Before starting, we need an AWS access key and secret key for configuration. Because for humans we use username and password for authentication. But to authenticate any program we use an access key and secret key.

  • To, get those things you need to go to your AWS account from AWS Web Console. Then go to “IAM” service => click on “Users” => then click on “Add User” and create one user.
  • Then click on “Programmatic access” and if you read the description of this access you can see, it’s giving us “access key” and “secret key”. For reference follow the below-mentioned screenshot…

Next, click on “Tags” and give any desired tag. Then click on “review” and then click on “Create User”. Next, it will provide you one option to see your Access Key and Secret Key. Don’t forget to “Download” the credentials by clicking on the download button for future reference.

Step 1:Login to AWS Using security credentials of IAM User we created.

aws configure

✅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 web app code for security and low latency.

👉 Everything is done by AWS CLI👈

AWS: Amazon Web Services is a Public Cloud Service by Amazon Company. AWS provides Infrastructure As A Service, Platform As A Service, and Software As A Service. In This task, I am going to use AWS CLI, EC2, EBS, S3, Cloudfront. AWS Provides High Availability, Isolation, and Security of services used by us. AWS provides each service with minimal cost. AWS works on a pay-as-we-go model.

🔅REMEMBER TO ALWAYS MAKE USE OF ‘HELP’ OPTION IN CLI

👉Create KeyPair :

This is the Command for generating key and storing on our given location for local P.C.

aws ec2 create-key-pair --key-name <key name> --query "KeyMaterial" --output text >  <key name>.pem

👉Create Security-Group and allow Ingress to ports 22 and 80 :

aws ec2 create-security-group --group-name <security group name> --description "security group for task2" --vpc-id <vpc id>
aws ec2 authorize-security-group-ingress --group-name <security group name> --protocol tcp --port 22 --cidr 0.0.0.0/0


aws ec2 authorize-security-group-ingress --group-name <security group name> --protocol tcp --port 80 --cidr 0.0.0.0/0

👉EC2 Instance :

aws ec2 run-instances --image-id <image ami id> --count <no of instances> --instance-type <instance type> --key-name <key name> --security-group-id  <security group id> --subnet-id <subnet id>

Now to give a tag to my instance I have used this command

aws ec2 create-tags --resources <instance id> --tags Key=Name ,   Value=AwsTask

👉EBS Volume :

The command to create EBS Volume is

aws ec2 create-volume --availability-zone <zone name> --volume-type <vol type> --size <size>

Volume is Created but not attached

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

The instance is successfully launched and also volume is attached.

👉Partition :

We have successfully attached 1 Gib EBS Volume to EC2 Instance So we have to follow 3 steps now so that we will mount 1 Gib Volume to “/var/www/html” directory.

First, check how many volumes are attached to this instance by the command “ fdisk -l

The command to do partitioning is “ fdisk /dev/xvdf

Press “n” to create a new partition.

Press “p” to create the primary partition.

Press “w” to save the partition made.

See the partition is created.

👉 Format

mkfs.ext4 /dev/xvdf1 ” is the command to format the partition.

Before mounting, install httpd which is Apache Tool to make an instance as a web server.

The command to install httpd is “ yum install httpd -y

👉 Mount

“/var/www/html” is by default a folder made by httpd as this is the main folder that is accessed by httpd while launching the website.

The command to mount partition is “mount /dev/xvdf1 /var/www/html

By “df -h” command you can see that “/var/www/html” is mounted to “/dev/xvdf1”.

👉S3 Bucket :

S3 here is used to store static files which are used in websites. AWS gives high Availability and Durability Guarantee on S3.

aws s3api create-bucket --bucket <bucket name> --region <AZ region> --create-bucket-configuration LocationConstraint= < AZ - region>

aws s3 ls” command is used to see how many buckets are present in s3.

aws s3 cp <Path of the s3 object> >s3://<Bucket name>/

👉 S3 OBJECT PUBLIC READ

Make S3 Object Publicly readable.

Now to make the object publicly readable use this command

aws s3api put-object-acl --bucket <bucket name> --key <object name> --acl public-read

Now you can view your s3 object using the object URL.

👉CloudFront

CloudFront plays a very important role in low latency. When the origin is far from the client then edge location is used to store cache so that it will be fastly accessible. As in CloudFront, we can set Time To Live [TTL] so that only for that time cache will be stored in edge location. Caches are temporary in nature.

aws cloudfront create-distribution --origin-domain-name <s3 bucket Domain name> --default-root-object <s3 object name>

Distribution is Created and as you can see that we have our own CloudFront Domain name as shown above.

👉Create a file :

Now create an HTML file so that it will be publicly accessible but the image URL used is of S3.

NOTE — create your program file in “/var/www/html” directory as httpd by default access that folder files.

cd /var/www/html
vi index.html
--------------Copy the below HTML code in above file--------------<body>
<h1 style="text-align:centre">High Availability Architecture with AWS CLI</h1>
<img src="https://<your CloudFront Domain Name>" width="1000" height="500"
</body>

Now start httpd as this is very important otherwise you will not be able to see your Web page.

systemctl start httpd

Now we can use public IP of our ec2 instance to view the Web page .

Here, the High Availability Architecture on AWS is ready with the help of CLI..!

Please buy me a coffee, if you liked my article :)

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--