Amazon CloudFront: Creation of high availability architecture using AWS CLI
Yashraj Dilip Oswal
Lead Software Engineer | Devops | 1x Azure certified | 1x AWS certified | Jenkins | Kubernetes | Docker | Python | Terraform | ELK | Github Workflow
What exactly is a CloudFront ?
Take an example of an e-commerce website, which sell Household products and that website is deployed in the region called India. When any local client is accessing that website it loads the products faster, with less latency; but when the same website is accessed from others zones like eg from U.S, it will take some time to load the product, which increases the latency. So to avoid increasing latency, AWS provides us with a services called CloudFront, which basically works on the term called CDN, i.e Content Delivery network.
How CloudFront Works ?
?When working of CloudFront is concerned, edge locations comes into picture. Edge location in AWS is considered as a small datacenters which are launched in different locations. AWS has 217 Point of Presence(POP), in which 205 are edge locations and 12 are the actual regions where AWS has deployed Huge Data centers. Edge locations are been designed to provide various assistance. So whenever the client accessing the data from far location, at first it comes all the way to the region where the data is present, then it collects that data and stores as cache in the edge location near to the client, so next time when client access that data, it reduces the time of loading i.e latency and data is served faster to these clients as well.
In AWS CloudFront service is attached to the S3 i.e Simple Storage Service where the data like, pictures, videos, audios etc are been stored. So whenever client request for any services, like view pictures, videos etc, Instead going all the way to S3, it make use of CloudFront which makes it easier to access with faster data providing capabilities.
Now lets get started with the task.
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.
1. Let us first create a new instance in EC2 Mumbai region:
Command: aws ec2 run-instances image-id ami-052c08d70def0ac62 --instance-type t2.micro --count 1 --subnet-id subnet-ab0c05c3 --security-group-ids sg-0d5fa990a1d18ab8c --key-name accesskey
2. To create security group and key-pair through CLI please refer my article :
Click here: Link
3. Install Apache server in the instance:
*In the cloud instance cli, perform the following command* :- Command: yum install httpd After Successful installation, you need to enable the services of apache server. Command: systemctl start httpd To check whether servers are started or not, use Command: systemctl status httpd
4. Creation of EBS-Block storage:
*Perform the following commands in your baseOS Command Prompt* : Command: aws ec2 create-volume --availability-zone ap-south-1a --volume-type gp2 --size 1 The Above command will create an EBS-Block of 1GB in the availability zone ap-south-1a
4.1 Now attach the EBS block to the created instance:
Command: aws ec2 attach-volume --device /dev/sdh --instance-id i-0579f0e74d4b0d44c --volume-id vol-0145dcbdab8b047f1
4.2 Lets check the attach the block in Cloud instance:
Command: fdisk -l
4.2.1 Now Let us create the EBS block storage ready to use:
Following are the mandatory steps involved to prepare the disk ready to use:
Step 1: Create new partition in the disk : Partitioning the disk is a step to allocate the disk space of specific size.
Command: fdisk /dev/xvdh
As partitioning a disk involves its pre-created commands
n -> It is used to create a new partition in the disk
p -> When ask for partitioning type, p denotes primary partition which allows us to create upto 4 partition in the disk.
Next it will ask you to choose the partition size, by default it starts from 2048, as 0 to 2048 bits are reserved sector, which is used to store partition data or metadata.
w -> It is used to save the partition.
Step2: Format the above created partition Formatting the partition is essential, as is a step to activate the partition for storage, and also it creates the inode table, inode table contains a listing of all inode numbers for the respective file system. When users search for or access a file, the UNIX system searches through the inode table for the correct inode number.
Command: mkfs.ext4 /dev/xvdh1
Step3: Mount the partition to the apache html directory
Command: mount /dev/xvdh1 /var/www/html/
5. Creation of S3 Storage:
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance.
Command: aws s3api create-bucket --bucket staticimage --region ap-south-1 --acl public-read --create-bucket-configuration LocationConstraint=ap-south-1
5.1 Upload a image in the S3 block:
Command: aws s3 cp C:\User\Lenevo| Desktop\ironman.jpg s3://staticimage/ --acl public-read
The above command will upload image in the S3 with can be publicly accessed.
6. Create CloudFront and connect it with S3 block:
Command: aws cloudfront create-distribution --origin-domain staticimage.s3.amazonaws.com
7. Creation of website, and attach it with the CloudFront:
Create a simple html code for testing purpose to check whether the cloudfront url is fetching the image from s3 block , and displaying in browers or not.
In the img tag, copy paste the cloudfront url, which is located AWS console-> services -> Under Network and content delivery -> Click on cloudfront, And there you will see the created Cloudfront, There copy the domain name url which is like this : dlcoml78s7bov.cloudfront.net, the paste it in html code dlcoml78s7bov.cloudfront.net/image.jpg
8. Finally lets check whether its working or not:
Oh yes..!, It is working.
Thank-You...! Here the task is successfully completed...!