Creation Of High Availability Architecture with AWS-CLI

EC2 ○ EBS ○ S3 ○ CloudFront

Image for post
Image for post

📄 Problem Statement:

What is CloudFront? How it uses a Content Delivery Network?

Lets Jump to the Practical Part:

Step 1: Configure Web server on the EC2 Instance

$ aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-0f20d73e825bd2692 --security-group-ids sg-0344355829b3965a2 --key-name aws-arth-key
Image for post
Image for post
$ aws ec2 create-tags  --resources  "i-0dc4220a72297cff4"  --tags Key=Name,Value=webserver-instance
$ aws ec2 describe-instances --instance-ids i-0dc4220a72297cff4 --query 'Reservations[*].Instances[*].PublicIpAddress'
Image for post
Image for post
$ ssh -i aws-arth-key.pem ec2-user@13.232.68.159$ sudo su - root
Image for post
Image for post
# yum install -y httpd; systemctl start httpd; systemctl enable httpd
Image for post
Image for post

Step 2: Make the Document Root(/var/www/html) persistent by mounting on EBS Block Device.

$ aws ec2 create-volume --availability-zone ap-south-1a --size 1 --no-encrypted
Image for post
Image for post
$ aws ec2 create-tags  --resources  "vol-01bf2e5f1ef1d89b5"  --tags Key=Name,Value=webserver-ebs
aws ec2 attach-volume --volume-id vol-01bf2e5f1ef1d89b5  --instance-id i-0dc4220a72297cff4 --device /dev/sdf
Image for post
Image for post
Image for post
Image for post
# fdisk /dev/xvdf
Image for post
Image for post
$ mkfs.ext4 /dev/xvdf1
Image for post
Image for post
$ udevadm settle
$ mount /dev/xvdf1 /var/www/html
$ df -h
Image for post
Image for post
$ cd /var/www/html/
$ vi index.html

Step 3: Static objects used in code such as pictures, videos, media files are stored in Amazon S3 Bucket.

$ aws s3 mb s3://mywebserver-s3bucket-us  --region us-east-1
Image for post
Image for post
$ aws s3 cp Tabish.jpg s3://mywebserver-s3bucket-us/Tabish.jpg
$ aws s3 ls s3://mywebserver-s3bucket-us
Image for post
Image for post
$ aws s3api put-bucket-acl --bucket mywebserver-s3bucket-us --acl public-read
$ aws s3api put-object-acl --bucket mywebserver-s3bucket-us --key Tabish.jpg --acl public-read
# vi index.html
Image for post
Image for post
Image for post
Image for post

Step 4: Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.

$ aws cloudfront create-distribution  --origin-domain-name mywebserver-s3bucket-us.s3.amazonaws.com --default-root-object Tabish.jpg
Image for post
Image for post

Step 5: Replace the image URL in the index.html file with Cloud Front URL for security and low latency.

Image for post
Image for post
Image for post
Image for post

Lets code 💻.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store