High Availability Webserver Architecture using AWS CLI

In this post,we are going to configure webserver on AWS instance.In our webpage we will display an image or any static data which will be stored in an S3 bucket which we will provide to the CloudFront service for reducing latency.The whole setup will be done using AWS CLI.

What is S3?

Amazon S3 (Simple Storage Service) provides object storage, which is built for storing and recovering any amount of information or data from anywhere over the internet. It provides this storage through a web services interface. While designed for developers for easier web-scale computing, it provides 99.999999999 percent durability and 99.99 percent availability of objects. It can also store computer files up to 5 terabytes in size.

AWS S3 Benefits

Some of the benefits of AWS S3 are:

  • Durability
  • Low cost
  • Scalability
  • Availability
  • Security
  • Flexibility
  • Simple data transfer

Let’s have a look at some of the major components of the AWS S3 storage service.

AWS Buckets and Objects

An object consists of data, key (assigned name), and metadata. A bucket is used to store objects. When data is added to a bucket, Amazon S3 creates a unique version ID and allocates it to the object.

What is CloudFront?

CloudFront is a CDN (Content Delivery Network). It retrieves data from Amazon S3 bucket and distributes it to multiple datacenter locations. It delivers the data through a network of data centers called edge locations. The nearest edge location is routed when the user requests for data, resulting in lowest latency, low network traffic, fast access to data, etc.

Now that we know what is AWS S3 and CloudFront and why they are used,we can move on to the practical part.

We will first launch an ec2 instance in which the webserver is to be configured.

Launch ec2-instance

For launching ec2 instance we need to mention the ami-id,instance type,key-pair name and security group.

aws ec2 run-instances --image-id <Image_id> --instance-type <Instance_type> --key-name <Key_Name> --security-group-ids <SG_id> --count 1

Create S3 bucket

For creating S3 bucket we have to mention the bucket name and region name.

aws s3api create-bucket --bucket <Bucket_name>--region <Region_name> --create-bucket-configuration LocationConstraint=<Region_name>

Upload object in the bucket

For uploading object we will use the aws s3 sync command.In this,we have to mention the local path of the folder in which the object is stored and the S3 URI

aws s3 sync <Local_path>  s3://<Bucket_name>

If we want to access the S3 object,we can use the url provided by AWS which will be something like this https://<Bucket_name>.s3.<Region_name>.amazonaws.com/<Object>

By default the objects uploaded do not provide public access.So we have to change the permissions of the object to allow public access.This can be done by changing the ACL to public.

aws s3api put-object-acl --bucket <Bucket_name> --acl public-read --key <Object_name>

Create CloudFront distribution

For creating CloudFront Distribution,we just have to provide S3 bucket URL as origin domain name.

aws cloudfront create-distribution --origin-domain-name <bucket_name>.s3.amazonaws.com

Now instead of using the S3 url we can use the CloudFront domain name for accessing the objects of our bucket.

Now we can create our webpage.But before doing that we will create an ebs volume and attach it to our instance.

Create EBS Volume of size 1GB

For creating EBS Volume we have to specify the volume-type and size.We also have to mention the Availability Zone in which we want to create the volume.The AZ should be same as that in which the instance was launched.If there is a mismatch between the AZ in which instance was launched and the one in which the volume is created then the volume will not be attachable to this instance.

aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone <AZ in which instance was launched>

Attach volume to instance

For attaching volume we have to mention the volume-id,instance-id and the device name.The device name should be in this format range /dev/xvda to /dev/xvdf.

aws ec2 attach-volume --instance-id <Instance_id> --volume-id <Vol_id> --device /dev/xvdf

After attaching the volume we have to make partitions in it,format it and then mount it.

For creating partitions in any hard disk in Linux we have to use the fdisk <device_name> command.

After creating partition,we have to format it by using mkfs.ext4 <partition_name> command.

Before mounting our partition,we have to first install the apache webserver software by using the following command.

yum install httpd

After this,we have to mount the partition to a particular folder.In our case,since we want our webpage to be persistent,which is stored in the /var/www/html ,we will mount the folder /var/www/html to the partition.

After this,we have to just write the code for our webpage in /var/www/html .

Now we have to just start httpd services using following command

systemctl start httpd

Our website is up and running and fully accessible with the static data coming from CloudFront.

Thus,we have created a Highly Available Infrastructure using AWS CLI.

Thank You!