High Availability Architecture with AWS CLIv2
5 min readJul 8, 2021

Prerequisite:
- Created an AWS account.
- Install AWS cliv2 in OS.
- Configure AWS cliv2 with IAM user.
Problem Statement:
- 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 the 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.
Step-1) Install AWS CLI and configure it.
- Here I am installing awscliv2 on Redhat Linux.
- Follow the given link to install it.
- After installing it confirms it.
aws --version

- Now we need to configure AWS CLI.
aws configure

- Now you need to type your access and secret key of IAM user.
- After type region name.
Step-2)Create a key pair
- Type the following command:
aws ec2 create-key-pair --key-name KEY_NAME > OUTPUT.pem

- Here I am using (>) for saving our key, it need if anyone wants to login in this instance.
Output:

Step-3)Create a security group
- Type the following command:
aws ec2 create-security-group --group-name GROUP_NAME --description "DESCRIPTION" > OUTPUT.TXT
- Here I am again saving my output while creating a security group because at the time of launching ec2-instance we need the id of this security group.
Output:


Step-4)Launch an instance using the above created key pair and security group.
- Type the following command:
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-1105675d --security-group-ids sg-040c982abe9bb3d48 --key-name key2 > ec2inf.txt
- Now use ami-id which you want, and use security groups id which we have already saved the output.
Output



Step-5)Create an EBS volume of 1 GB.
- Type the following command:
aws ec2 create-volume --volume-type TYPE--size 1 --availability-zone ZONE_YOU_WANT > OUPTUT.TXT
- Here I am again saving my output while creating a volume because at the time of attaching volume into ec2-instance we need the id of volume.
Output

Step-6)Attach the above created EBS volume to the instance you created in the previous steps.
- Type the following command:
aws ec2 attach-volume --instance-id YOUR_INSTANCE_ID --volume-id VOL_ID --device /dev/sdf
- Now use id which we already saved in the output file at the time of creating volume
Output


Step-7)Create partition , format it and mount on /var/www/html
- Now log in to your EC2-instance.
- Type the following commands:
fdisk -l

- Here we can see our EBS has been attached.
fdisk /dev/xvdf
- Creating partition.

mkfs.ext4 /dev/xvdf
- Here I am formatting the partition type ext4.
mount /dev/xvdf /varw/www/html
- After formatting, we need to mount on /var/www/html for making our web code persistent

Output:

Step-7)Create an S3 bucket for static content
- Type the following command for creating an S3 bucket.
aws s3 mb s3://bucketname
Output:


Step-8) Upload content in the S3 bucket
- Type the following command for creating an S3 bucket.
aws s3 cp . s3://BUCKET_NAME --recursive --acl public-read

Step-9) Setting up Content Delivery Network using CloudFront and using the origin domain as an S3 bucket.
- Type the following command for creating an S3 bucket.
aws cloudfront create-distribution --origin-domain-name BUCKET_NAME.s3.amazonaws.com
- Here you need to type your bucket name in the — origin-domain-name .
Output


Step-10) Place the Cloud Front URL on the web app code for security and low latency.
- Now we need to use the domain name provided by CloudFront.

- Now Type Domain name in the img tag.
Final Output of Website


- As you can see the data is coming from S3 using Cloudfront.
Keep Sharing & Keep learning