AWS CLI: A Beginners Guide(Part2)

High availability architecture

In this article, we will discuss how to :

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 a Content Delivery Network using CloudFront and using the origin domain as an S3 bucket.
  • Finally, place the Cloud Front URL on the web app code for security and low latency.

Assuming that we already have a key pair called taskKey and security group with id sg-02acd13d993724f96

To know how it is done, feel free to check out this article:

So let's begin ✨

aws ec2 run-instances --image-id ami-03cfb5e1fb4fac428 --count 1 --instance-type t2.micro --key-name taskKey --security-group-ids sg-02acd13d993724f96 --tag-specifications  ResourceType=instance,Tags=[{Key=Name,Value=myos},{Key=env,Value=testing}]
EC2 instance created

Step2.1: Creating a block storage

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

Step 2.2:We need to attach it to the instance:

aws ec2 attach-volume --volume-id vol-089f493d3638f2341 --instance-id i-02ca1ad19d7a18f1a  --device /dev/sdf
EBS created and attached

In this step we have to do multiple things, like installing a web server, configuring it. Then formatting and mounting the device we attached. We will use the ssh command to perform the specific task. To do ssh we need the public IP of the instance which we will get from the output of Step 1.

1. Listing the devices
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo fdisk -l”
2. Formatting device
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo mkfs.ext4 /dev/xvdf”
3. Installing HTTPD software
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo yum install httpd -y”
4. Mounting the formated drive
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo mount /dev/xvdf /var/www/html”
5. Checking the status of httpd
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo systemctl status httpd”
6. Starting the httpd service
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo systemctl start httpd”
7. Permenantly starting the service
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo systemctl enable httpd”
8. Again checking the status
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo systemctl status httpd”
9. Listing the active partition mounted
ssh -i taskKey.pem ec2-user@65.0.80.239 “sudo df -h”

So what is S3?

AWS S3 provides object storage through a web service interface. Amazon S3 uses the same scalable storage infrastructure that Amazon.com uses to run its global e-commerce network.

Step 4.1: Creating an S3 bucket

Note: The name of the bucket must be unique

aws s3api create-bucket --bucket cdntest3004 --acl public-read-write --region us-east-1

Here, ACL refers to the access control list, we have made it public because we have to use it on the website which needs to be public for all the clients. region refers to the zone where we want to keep the bucket. AWS CLI V2 doesn’t support ap-south-1 for the S3 bucket hence we have used us-east-1.

S3 bucket created

Step 4.2: Uploading objects into the bucket

aws s3api put-object --acl public-read-write --bucket cdntest3004 --key /image/shirsha.jpeg --body C:\Users\hp\OneDrive\Desktop\vit\meh.jpeg

Here, only key and body is a new parameter or fields we need to understand. key refers to the path we want to save in the S3 bucket and the body refers to the object we want to put in the key location.

Image uploaded to the bucket

So what is a 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. CloudFront is integrated with AWS — both physical locations that are directly connected to the AWS global infrastructure, as well as other AWS services.

aws cloudfront create-distribution --origin-domain-name cdntest3004.s3.amazonaws.com --default-root-object //image/shirsha.jpeg

This is the crucial and main command to implement the CDN through edge locations of Amazon. origin-domain-name refers to the place from where we have to pick the data to distribute over the edge locations of amazon. default-root-object refers to the object in the S3 bucket we want to create a distribution of. This will give a domain name as output for the object which we have to use on the website. Then we have to run the following final step.

CDN created
ssh -i taskKey.pem ec2-user@65.0.80.239
sudo su
vim /var/www/html/my.html

And add the below-given code:

Our site is accessible using the inside publcIP/filename.

We have finally created a high availability architecture.

Hope you liked the article. If you have any queries feel free to connect with me on LinkedIn.

Thank you.

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.