??High Availability Architecture with AWS CLI???
Or ever come’s in your mind that how these sites provide low latency and gives Best-User Experience worldwide ??
If yes, then this Article will help you to know about the internals of their sites that what services they use & how these sites manage low latency and provide best-user experience.
So, these companies uses Amazon Cloud Front as a service which is provided by AWS for low latency and best-user experience on their websites.
What is Amazon CloudFront ?
- Amazon CloudFront is a web service that speeds up distribution of your static and dynamic web content, such as .html, .css, .js, and image files, to your users. CloudFront delivers your content through a worldwide network of data centers called edge locations. When a user requests content that you’re serving with CloudFront, the user is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.
- If the content is already in the edge location with the lowest latency, CloudFront delivers it immediately.
- If the content is not in that edge location, CloudFront retrieves it from an origin that you’ve defined — such as an Amazon S3 bucket, a MediaPackage channel, or an HTTP server (for example, a web server) that you have identified as the source for the definitive version of your content.
Lets Understand this by creating an Architecture:
- 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.
So Lets start creating this Architecture and understanding the concepts:
1.) What is AWS ?
Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow. It provides each service at a minimal cost.
In this practical I’m going to use AWS CLI, EC2, EBS, S3 and CloudFront service of AWS. We’re going to do the complete practical using CLI not GUI, which makes the task more interesting.
2.) What is AWS CLI?
The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
2.1) How to install AWS CLIv2
- Download the AWS CLI MSI installer for Windows (64-bit): https://awscli.amazonaws.com/AWSCLIV2.msi
- Run the downloaded MSI installer and follow the on-screen instructions. By default, the AWS CLI installs to C:\Program Files\Amazon\AWSCLIV2.
- To confirm the installation, open the Start menu, search for cmd to open a command prompt window, and at the command prompt use the aws — version command.
C:\Users\user>aws --version aws-cli/2.0.59 Python/3.7.7 Windows/10 exe/AMD64
2.2) How to configure AWS CLIv2 ?
- Open the command prompt in Windows and execute the following command & enter then enter the access key ID, secret access key and availability zone and set the output format to default i.e. json.
Now, let’s launch an instance!
3.) What is Amazon EC2?
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment.
3.1) Launching an EC2 instance using AWS CLI.
An instance is a virtual server in the cloud. Its configuration at launch is a copy of the AMI that you specified when you launched the instance. You can launch different types of instances from a single AMI.
for launching the instance, we need ami image id , instance type, subnet id ,security group id, key name and the number of counts.
So collect all this info in a file from aws webui and then run the command:
aws ec2 run-instances --image-id <id> --instance-type <type> --subnet-id <id> --security-group-ids <id> --key-name <name> --count <num_of_count>
The following instance will be launched:
Now, let’s create a 1 GB volume and attach it!
4.) What is Amazon EBS?
Amazon Elastic Block Store (EBS) is an easy to use, high performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale.
4.1) Create EBS Volume through CLI
- Run the following command in the command prompt to create an EBS volume of 1 GB:
aws ec2 create-volume --availability-zone ap-south-1b --size 1 --volume-type gp2
- The following volume has been created, note the Volume ID, we will need it to attach the volume
4.2) Attach EBS Volume through CLI
- Run the following command in the command prompt to attach the EBS volume to EC2 instance:
aws ec2 attach-volume vol-09b44f65561a6a2f1 --instance-id i-08e9c11ed7385049b --device /dev/sdf
- The volume is attached to our EC2 instance
5.) How to configure the webserver?
- Login your instance using Putty
- Execute the following command in the command prompt of your instance:
sudo yum install httpd
- To start the service of httpd, execute the following command:
systemstl start httpd systemstl status httpd
We’ve successfully configured the webserver & started it’s services.
Now, let’s create partition in our volume!
5.1) How to create, format & mount partition?
- To check the status of your disk, execute the following command in your command prompt:
fdisk -l
- To create a partition in the volume, use these steps:
fdisk /dev/xvdf
- Create a primary partition as follows:
- Use the following command for giving driver to the partition:
udevadm settle
- Use the following command to format the partition:
mkfs.ext4 /dev/xvdf1
- I used the following command to mount my partition on /var/www/html directory:
mount /dev/xvdf1 /var/www/html
Now that we’ve created, formatted & successfully mounted our partition, let’s move on to create a S3 bucket
6.) What is Amazon S3?
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics.
6.1) How to create a S3 bucket?
Execute this command to create a S3 bucket with public access, public access is important to let our webpage be displayed to everyone:
aws s3api create-bucket --bucket my-bucket --region eu-west-1 --create-bucket-configuration LocationConstraint=eu-west-1
- We can check the result using Amazon GUI as well:
6.2 ) How to upload files into this S3 bucket?
- Execute the following command to upload the files of a folder into our bucket:
aws s3 sync "C:\Users\user\Desktop\Spotify.jpg" s3://mananbuckettask6 --acl public-read
As we can see, the two files have been uploaded into our S3 bucket with read access to the public:
6.3) Creating CloudFront Distribution !
Let’s now create a distribution in CloudFront by using the following command:
This is my CloudFront Distribution:
My HTML Code. This file is created inside /var/www/html directory:
And here is the final output!!!
Link for the webpage: https://52.66.211.176/test.html
To see the cache statistics of the distribution:
Total Hits: Total number of times someone accessed the webpage
Total Misses: Total number of times the image in the webpage was not present at the edge location
Total Errors: Total number of times the webpage didn’t display
Now, Anyone can access this site across the worldwide without any delay because we have used CloudFront service for AWS for low latency and for High Availability !
I hope you like the Article and find it useful. Any queries and suggestions are highly accepted !!
Keep Learning, Keep Growing ??
Thankyou!!??
Cloud Engineering @ Alteryx | 3x GCP | Ex - AirAsia
4 年Well explained ??
CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure
4 年Nice ??????