đź”° Create High Availability Architecture With AWS CLI đź”°

Sunil Sirvi
3 min readMar 11, 2021

--

Creating security group for EC2 instance.

aws ec2 create-security-group --group-name SecurityGroup --description "Allow all traffic"

Now, I have created my security group named “SecurityGroup”. Now we have to create inbound rule -

aws ec2 authorize-security-group-ingress --group-name SecurityGroup --protocol all  --cidr 0.0.0.0/0

The above command will create inbound rules for the security group which will allow all traffic. Next we have to create private key-pair -

aws ec2 create-key-pair --key-name Keypair --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath keypair.pem

Launching an EC2 instance -

I have Key-pair named as Keypair. Let’s launch the instance -

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --availability-zone ap-south-1a --count 1 --tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=AWSCLIPRACTICE}] --security-groups SecurityGroup --key-name Keypair

Creating an EBS Volume and attaching it to the EC2 instance.

Now EC2 is successfully launched. The next step is to create an EBS volume and attach it to the EC2 instance. To create a volume in EBS size of 2 Gib and in Mumbai south-1a region -

aws ec2 create-volume --size 2 --availability-zone ap-south-1a --tag-specifications ResourceType=volume,Tags=[{Key=Name,Value=AWSCLIEBS}]

To attach the EBS volume to EC2 instance -

aws ec2 attach-volume --volume-id [volume-id] --instance-id [instance-id] --device /dev/xvdf

Now, EC2 instance is successfully launched and EBS volume is also successfully connected to EC2 instance.

Creating a S3 Bucket for storing static objects.

To create S3 bucket -

Remember that your bucket name should be unique otherwise it will not create.

aws s3api create-bucket  --bucket s3bucketsunil  --create-bucket-configuration LocationConstraint=ap-south-1

Transferring static files like images to S3 Bucket we created in previous step.

Now we have to upload the static data like images on the S3 bucket:

(I already have one jpg file in my root folder — sunil_sirvi.jpg, so I am going to upload it to S3 bucket. And “-acl public-read” will give read access to the public.)

aws s3 cp /root/sunil_sirvi.jpg  s3://s3bucketsunil --acl public-read

Setting up Content Delivery Network(CDN) using Cloudfront and using the origin domain as S3 bucket.

Cloudfront is a service which comes under networking and content delivery. let’s create a Cloudfront with “s3bucketsunil S3 bucket” as the origin domain -

aws cloudfront create-distribution --origin-domain-name s3bucketsunil.s3.amazonaws.com

Cloudfront will automatically provide domain after running above command.

Webserver configured on EC2 Instance.

Next step is to configure web-server on top of our EC2 Instance. If you have launched the instance from amazon AMI, then you can directly access it in the browser. First you have to login as root -

sudo su - root

And then install the httpd software -

yum install httpd -y

Then start httpd service -

systemctl start httpd

Document Root made persistent by mounting on EBS Block Device.

Let’s mount the document root /var/www/html to EBS Volume. Before mounting, we need to create and format the partition in EBS volume.

fdisk /dev/xvdfpress 'n' to create a new partition
press enter 4 times
press 'w' to save the partition
The partition table has been altered.

To format the partition /dev/xvdf1 -

mkfs.ext4 /dev/xvdf1

To mount this partition over document root (/var/www/html) -

mount /dev/xvdf1  /var/www/html

To confirm that the partition has been successfully created and mounted, use “fdisk -l” -

fdisk -l

- or “lsblk” command.

lsblk

Finally placing the Cloud Front URL on the webapp code for security and low latency.

cd /var/www/html
cat > webpage.html

Create the web-page and include text and the image(which is located in S3 — instead of S3 URL, provide the URL of Cloudfront for security and low latency), and access it in the browser. Now you can observe that the images and static objects are provided by S3, whereas the data of the web-page is coming from EBS storage which is persistent.

--

--

No responses yet