Using AWS-CLI To Create A High Availability CloudFront Distribution

Prajwal Patil
4 min readOct 28, 2020

Hello Everyone, In this Article I am integrating AWS-S3 with CloudFront and hosting webserver using EBS.

For More Understanding, Refer my previous article Here

So Lets start:->

What is AWS-S3 ?

Object storage built to store and retrieve any amount of data from anywhere. Get started with Amazon S3. Request more information. Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. It is analogous to any public cloud storage like Google Drive etc.

What is CloudFront ?

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.

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 webapp code for security and low latency.

Step 1: Create an EC2 instance to configure the Webserver


# aws ec2 run-instances --image-id <AMI-Id> --instance-type t2.micro --count 1 --subnet-id <Subnet-Id> --security-group-ids <Security-Group-Id> --key-name <Key-Name>

Step 2: Create the EBS Volume


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

Step 3: Attaching the EBS volume to the EC2 Instance


# aws ec2 attach-volume --volume-id <EBS_Volume_ID> --instance-id <EC2_Instance_Id> --device /dev/sdf

Step4: Creating the partition for newly attached EBS volume

For this, First we need to access CLI of Instance

ssh -i "<Key_Name>" ec2-user@<Public_IP>

Now Format the partition created.

Step5: Install Apache Httpd webserver

# yum install httpd -y

Step6: Mount The Partition at Root directory of Httpd (/var/www/html)

# mount /dev/xvdf /var/www/html

Step7: Create a AWS-S3 bucket so store static data on it.

# aws s3api create-bucket --bucket <Name> --region <Region_Code> --create-bucket-configuration LocationConstraint=ap-south-1

Now add items in this bucket.

# aws s3api put-object --bucket slayer227 --key 1.jpg --body "Local-Path-of-Item"

Change the policies of bucket as well as object (For public access)

  1. Bucket accessible publicly
# aws s3api put-bucket-acl --acl public-read --bucket <Bucket-Name>

2. Objects in bucket accessible publicly.

# aws s3api put-object-acl --bucket <Bucket-Name> --key 1.jpg --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers

Step8: Create CloudFront Distribution for S3 bucket.

# aws cloudfront create-distribution --origin-domain-name <Bucket-Name>.s3.amazonaws.com --default-root-object <Object-Name>

Step9: Now configure Httpd web-server and start it.

and, restart the Httpd service if already running.

Here we go…

This static image is stored in CloudFront

Thank You for Reading :)

You can connect me on Linkedin

--

--