Create High Availability Architecture With AWS CLI
Dhwanil Gangani
AWS CSA || RHCE || EX180 || Aviatrix Certified Engineer || MLOps || DevOps Engineer || Python || Quantum Computing
Heloo Everyone Myself Dhwanil Gangani, Will Show You Some Interesting Things In these Article And Also It Will Help You To Boost Your Knowledge Regarding "How To Create High Availability Architecture With AWS CLI".
So Be With Me Till End of These Article. So Let's Start The Small Journey :
#Vimal Sir Has Given Us A Task Which Is Describe Below :
---------------------------------------------------------------------------------------------
**** TASK DESCRIPTION – AWS ****
* Task 6 : 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 Cloud Front and using the origin domain as S3 bucket.
~ Finally place the Cloud Front URL on the webapp code for security and low latency.
*All the above steps must be performed using CLI
---------------------------------------------------------------------------------------------
* What Is AWS Cloud Front ?
Cloud Front 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.
Amazon Cloud Front 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. Cloud Front delivers your content through a worldwide network of data centers called edge locations.
When a user requests content that you're serving with Cloud Front, 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, Cloud Front delivers it immediately.
- If the content is not in that edge location, Cloud Front retrieves it from an origin that you've defined—such as an Amazon S3 bucket that you have identified as the source of origin.
---------------------------------------------------------------------------------------------
* So let's Start The Journey Of Creating High Availability Architecture With AWS CLI :
---------------------------------------------------------------------------------------------
* Some Pre - Requisite To do these practical is :
Step 1 :- First launch an ec2 instance in particular zone.
Step 2 :- Then Create a 1Gb "EBS Volume" in same Zone where Instance Is Launched.
Step 3 :- Then Attach That Volume of 1Gb to that Ec2 Instance.
Note :- These 3 Step Process is Describe In more Detail way in My One of Article whose Link Is given Below. Plz refer it if you don't Know the above 3 Steps. There I have cover all things in detail with CLI CMDs.
---------------------------------------------------------------------------------------------
Step 4 :- Webserver configured on EC2 Instance
To Configure Web Server First we need check if Webserver is installed or not for that CMD is : "rpm -q httpd".
After that if it is not Installed then to Install we use CMD is : "yum install httpd"
After That we Need to Start Web Server for that CMD is : "systemctl start httpd"
To Make Webserver Running Permanently CMD is : "systemctl enable httpd"
Run Above CMD after installing httpd. That's It Your Web Server is Configured. Well Done.
---------------------------------------------------------------------------------------------
Step 5 :- Do Partioning, Formatting & Mounting this EBS VOLUME To Document Root(/var/www/html) Directory.
Step 1 : First we need to Create Partitions of That EBS which we Attached to EC2 Instance.
Step 2 : Second we Need to Format That Partition.
Note :- To make Storage Device useable or to store data we need too first make partitions and then format then mount. Here Step 1 & Step 2 is described more in detail in these Article. The Link is given below just refer it :
Step 3 : Know its Time to Mount, But wait here we have to mount that partition with Document Root(/var/www/html). For that CMD is :
"mount /dev/xvdf1 /var/www/html"
See here we mounted the partition "xvdf1" with document root "/var/www/html".
So here Step 5 is Completed.
---------------------------------------------------------------------------------------------
Step 6 :- Create a S3 Bucket With a Unique Name In The Region
Know First we Require to Create a S3 Bucket Using CLI. S3 bucket you can create in any region but more feasible it to check your nearest region where you can create.
The Use Case to create S3 Bucket is there we will store our image file which will be treated as origin source for cloud front. Also You use any Storage Device as Origin Source. But here we Choose S3 Bucket.
CMD to Create S3 Bucket we Don't Know So here we will take help from AWS CLI Help Option. From here we will get help for Creating a CMD for S3 Bucket.
Know after going deep dive I have found one CMD to create S3 bucket :
C:\Users\kdhwa>aws s3api create-bucket --bucket aws-arth-cli-bucket-101 --region us-east-1
{
"Location": "/aws-arth-cli-bucket-101"
}
Here We Created S3 Bucket Successfully!
---------------------------------------------------------------------------------------------
Step 7 :- Static Objects Used in Code such as Pictures Stored in S3 Bucket
After that we need to save our image file in S3 Bucket it means we need to Upload The File in S3 Bucket. For That CMD is :
C:\Users\kdhwa\Downloads>aws s3 cp arthpic.jpg s3://aws-arth-cli-bucket-101/arth-images-task6/
upload: .\arthpic.jpg to s3://aws-arth-cli-bucket-101/arth-images-task6/arthpic.jpg
Here "arthpic.jpg" is the image file which we want to upload.
Note :- Make sure the image is in same location "C:\Users\kdhwa\Downloads" where are you. Here my Image File is in Downloads.
Here "S3://aws-arth-cli-bucket-101/arth-images-task6/" this is the location where we want to save it.
We have Uploaded Static Image Successfully!
But here we need to make that Image File Public so anyone from outside can access that image file. Default it is Private and because of that any other user can't access that file. So to make it Public CMD is :
C:\Users\kdhwa\Downloads>aws s3api put-object-acl --bucket aws-arth-cli-bucket-101 --key arth-images-task6/arthpic.jpg --acl public-read
Here "--acl" is option used to give permission to the image file. we are giving "public read" permission.
These is Our Origin Location were have Image file Stored.
So we have completed S3 Bucket Part know Let's Create Cloud Front.
---------------------------------------------------------------------------------------------
Step 8 :- Setting up Content Delivery Network using Cloud Front and using the origin domain as S3 bucket
To Setup CDN using Cloud Front & Origin Domain as S3 Bucket we have CMD for that using that our Cloud Front Distribution will be Setup.
Before That we require "Origin Location" where the image is stored. That We get from S3 Bucket. Also we need to give origin path to it that we can get from "Key" of S3 Bucket.
CMD to Setup Cloud Front is :
C:\Users\kdhwa\Downloads>aws cloudfront create-distribution --origin-domain-name aws-arth-cli-bucket-101.s3.amazonaws.com --default-root-object arth-images-task6/arthpic.jpg
{
"Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/E1UOCEX94EDT9J",
"ETag": "E3C2ETPSR27FIZ",
"Distribution": {
"Id": "E1UOCEX94EDT9J",
"ARN": "arn:aws:cloudfront::015153444493:distribution/E1UOCEX94EDT9J",
"Status": "InProgress",
"LastModifiedTime": "2020-10-29T13:14:31.090000+00:00",
"InProgressInvalidationBatches": 0,
"DomainName": "d11jfe69ioxb7e.cloudfront.net",
"ActiveTrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"DistributionConfig": {
"CallerReference": "cli-1603977268-182445",
"Aliases": {
"Quantity": 0
},
"DefaultRootObject": "arth-images-task6/arthpic.jpg",
"Origins": {
"Quantity": 1,
"Items": [
{
"Id": "aws-arth-cli-bucket-101.s3.amazonaws.com-1603977268-939413",
"DomainName": "aws-arth-cli-bucket-101.s3.amazonaws.com",
"OriginPath": "",
"CustomHeaders": {
"Quantity": 0
},
"S3OriginConfig": {
"OriginAccessIdentity": ""
},
"ConnectionAttempts": 3,
"ConnectionTimeout": 10
}
]
},
"OriginGroups": {
"Quantity": 0
},
"DefaultCacheBehavior": {
"TargetOriginId": "aws-arth-cli-bucket-101.s3.amazonaws.com-1603977268-939413",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "allow-all",
"AllowedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": false,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 0
},
"QueryStringCacheKeys": {
"Quantity": 0
}
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
},
"CacheBehaviors": {
"Quantity": 0
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "",
"Logging": {
"Enabled": false,
"IncludeCookies": false,
"Bucket": "",
"Prefix": ""
},
"PriceClass": "PriceClass_All",
"Enabled": true,
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true,
"MinimumProtocolVersion": "TLSv1",
"CertificateSource": "cloudfront"
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
}
}
Here we have Created The Cloud Front Distribution. It may take some time because it goes to all 250 Edge Location & Update Image File from Origin Location.
After That We need to copy These "DomainName: d11jfe69ioxb7e.cloudfront.net" from these cloud front & Origin Path to our Web Page where we are using these Image.
Cloud Front Created Successfully!
---------------------------------------------------------------------------------------------
Step 8 :- Finally place the Cloud Front URL on the webapp code for security and low latency.
Know we need to create small web page where we will put these Cloud Front URL for Security & Low Latency Purpose.
I already have some basic code written with me, It also contains the Cloud Front URL. So here it is :
Know if anyone Hit to your site then the image will be coming from the clients nearest Edge Location from that Client will get Image Faster means No Latency Problem And It will Create Good User Experience.
If in some region there is no one who have access this website then any client come and visit that site then it might face some Latency Issue or Delay in Opening of Image.
It is because of first time cloud front goes to Origin Location from there it store that image as cache in nearest edge location of that client. Know if second time from that region anyone access that Image it will provide it from edge location. Means Latency issue Solved.
This URL is intelligent enough to create caches in the client nearest edge location accross the Globe.
Now Client Can Access The Site & Only First Time Client Faces Miss {high latency} , But After That Client Faces Hits {low latency} Within the TTL[time to leave] .
---------------------------------------------------------------------------------------------
**** I have Completed this task under the guidance of Mr. Vimal Daga sir in the training – ARTH-The School Of Technologies-2020. *****
**** That's All From My Side ****
---------------------------------------------------------------------------------------------
*** Thank You Everyone ***
DevOps Engineer @Amdocs
4 年Nice work ?