High Availability Architecture with S3 and Cloudfront using AWS CLI

Palak Jain
4 min readNov 1, 2020

In this article, I’m going to demonstrate how we can create web pages and their content available to every user without any latency by using AWS services

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.

STEPS

Step 1: Configuring the AWS account with CLI

For configuring the AWS account with CLI we will use command - aws configure

Configuring

Step 2: Create an EC2 instance to configure the webserver on it

For creating an instance, we will use the command: aws ec2 run-instances –image-id image_id –instance-type type_of_instance –count count –security-group-ids security_group_id –key-name name_of_key

Now, we can check it on the dashboard that it has been created.

Step 3: Create and attach EBS volume to the EC2 instance

For creating an EBS volume we will use command: aws ec2 create-volume — volume-type type_of_volume — size size — availability-zone zone

For attaching a volume to the EC2 instance we will use command: aws ec2 attach-volume — volume-id volume_id — instance-id instance_id — device device

Step 4: Configuring the Web Server in the Instance

Here i’ve used putty program to connect to the EC2 instance.

Now to configure WebServer , we need httpd which we will install using command: yum install httpd -y

After completion, we will start it by using command: sytemctl start httpd

Now to check whether it’s running or not we will use command: systemctl status httpd

Step 5: Attaching the external EBS volume to WebServer document root

I don't want to use my full storage, so partitioning has been done here.

First we will check the present disks using command: fdisk -l

Then we will do partitioning, using command: fdisk /dev/xvdf

And after this , we will give the size of which we want the disk to be partitioned and complete this whole partitioning by ‘w’ which is write or else you will lose the partitioned disk as it won’t save.

We have to mount it but before that, we need to set the driver for the same, then need to format it.

Step 6: Create S3 bucket to store static content

For creating S3 bucket we will use command: aws s3api create-bucket — bucket bucket_name — region region —create-bucket-configuration Configuration —acl public_read

We can check it on dashboard, whether it’s created or not.

Now to add an object, we will use command: aws s3 sync location_of_object s3://bucket_name — acl public-read

Step 7: Create a Cloud Front Distribution order to provide CDN

For creating distribution, we will use command: aws cloudfront create-distribution — origin-domain-name domain_name

We can check it on dashboard, it’ll first be in progress stage then get enabled soon i.e it gets deployed. After deployment we get Domain Name through which we can access any content fro the configured S3 bucket at any location without any latency.

Step8: Create a webpage and access it from the webserver that we have created

Use your public IP and the html page.

Hope you like the blog!

Keep learning!!

--

--