AWS CloudFront for High Availability
Tags: aws, cloudfront, cdn, architecture
Published: 2023-11-28 11:07:11


## AWS CloudFront for High Availability

### AWS Cloud Hands on Lab Practice Series


>  **Project Overview —**

This project revolves around AWS CloudFront where we configure an distribution, origin groups for a website to be highly available, accessible globally to securely deliver content with low latency and high transfer speeds. By leveraging AWS S3 static website hosting we configure our bucket as a static website, along with it we deploy one more website on AWS EC2 which can act as failover/redundant website target.

>  **SOLUTIONS ARCHITECTURE OVERVIEW -**

![](https://cdn-images-1.medium.com/max/2000/1*ADMQ36RpchpP66ZMawScSw.png)

>  **First Let’s understand the real world use case :**

* **E-commerce Product Images and Static Assets:** An e-commerce platform wants to optimize the delivery of product images, style sheets, and other static assets to enhance the online shopping experience for users.

* **Use Case:** AWS CloudFront can be utilized to cache and deliver these static assets from edge locations, ensuring faster page loads and a more responsive user interface.

* **Accelerated Gaming Content Delivery:** Online gaming platforms want to deliver game updates, patches, and in-game assets with low latency for a seamless gaming experience.

* **Use Case:** AWS CloudFront accelerates the delivery of gaming content by caching frequently accessed assets at edge locations. This minimizes download times for users and enhances the overall gaming experience.

* **High-Traffic Event Websites:** Event organizers are expecting a high volume of traffic for a specific event website, such as ticket sales or live streaming of an event.

* **Use Case:** AWS CloudFront helps handle the surge in traffic by distributing content across multiple edge locations, ensuring that users experience minimal latency and preventing the origin server from being overwhelmed.

>  **Prerequisite —**

* AWS Account with Admin privileges

>  **AWS Services Usage —**

* AWS CloudFront

* AWS S3

* AWS EC2

* AWS VPC

* AWS IAM

>  **STEP BY STEP GUIDE -**

**STEP 1 : Creating AWS S3 bucket through console.**

* Login to AWS account, Navigate to AWS S3 Service.

* Click on create bucket

* Provide Unique Bucket Name (It will be website name)

* Select the AWS Region according to nearest user location.

* Uncheck the Block all public access.

* Tick Mark — I acknowledge that the current settings might result in this bucket and the objects within becoming public.

* Keep rest of the settings as is & hit Create bucket.

![](https://cdn-images-1.medium.com/max/2000/1*e60PZFdePEf2j4l374IUxw.png)

![](https://cdn-images-1.medium.com/max/2000/1*7ztB4y_ebX7bHjN4ZHaXig.png)

**STEP 2 : Creating AWS S3 bucket policy through console.**

* Navigate to permissions tab of newly created s3 bucket.

* Copy, edit & paste it in the policy section

* Edit the bucket ARN & validate the json formatting & save it.

* This policy allow anyone to make GET request from Internet.

>  {
 “Version”: “2012–10–17”,
 “Statement”: [
 {
 “Effect”: “Allow”,
 “Principal”: “*”,
 “Action”: “s3:GetObject”,
 “Resource”: “<s3-bucket-arn>/*”
 }
 ]
}

![](https://cdn-images-1.medium.com/max/2000/1*yLeHU002uGCVGv42kHVAfw.png)

**STEP 3 : Enable Static website hosting**

* Navigate to properties tab on your bucket.

* Scroll till down to enable static website hosting.

* select Host a static website

* index document → index.html -> save

* You will get a FQDN which is accessible over internet to serve the content.

![](https://cdn-images-1.medium.com/max/2000/1*G8KDCYbVpsl7hDOv3FV3hA.png)

**STEP 4 : Create index.html & upload it.**

* Create a file named index.html in notepad, Copy the content from [GitHub](https://github.com/Kunal-Shah107/aws-cloudfront-lab).

* You can update the content of index.html as per your choice.

* Upload the index.html on s3 bucket.

* Your website is up & ready to be shared !! Congrats…

![](https://cdn-images-1.medium.com/max/2000/1*tTzJ7Kfz9-9ryh0BUnJ1VA.png)

![s3 static website](https://cdn-images-1.medium.com/max/3218/1*SCDrIWNxwrWZlzwuxPvL3g.png)

**STEP 5 : Launch an EC2 Instance with User Script:**

* Navigate to EC2 & hit the Launch Instance in the region of your choice.

* Enter Name -> Select OS -> Select EC2 Type (t2.micro) -> no key pair.

* For Network select default VPC -> Enable Auto Assign Public IP.

* Create New Security Group with Inbound access of HTTP & HTTPS.

* In **ADVANCE SETTING **scroll down till last & copy/upload the **userscript_for_ec2 **from this **[GitHub Repo.](https://github.com/Kunal-Shah107/aws-cloudfront-lab)**

* Hit the Launch Instance & wait for its status to running.

![](https://cdn-images-1.medium.com/max/2390/1*hY7QnSit4voXr7ZtbJHAeg.png)

![](https://cdn-images-1.medium.com/max/2000/1*-lLiZEEaesOiC4viOo8hVQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*RViIuSalKzzFKXa5kWL9RA.png)

![](https://cdn-images-1.medium.com/max/2000/1*RlbZWd8qtnNpaPjBMNfXTw.png)

* Enter the **PUBLIC IPv4 / EC2 PUBLIC DNS** in the browser.

* http://<Public_IP>

* http://<Public_IP>/index.html

* http://<Public_IP>/index2.html

* You can notice we have deployed 2 index pages & are serving as expected from AWS EC2 Instance.

![index.html](https://cdn-images-1.medium.com/max/3842/1*2BVBic-IlvkWli9p0tO-xA.png)

![index2.html](https://cdn-images-1.medium.com/max/3840/1*PNMcPD7RnYSrXiPxxHKg6w.png)

**STEP 6 : Create CloudFront Distribution:**

* Navigate to CloudFront Service on AWS Console.

* Select the Origin -> S3 Bucket Endpoint (Bucket created in step 1)

* Protocol -> HTTP Only

* Keep rest of the settings as default & hit the create distribution.

* Once the CloudFront distribution is deployed.

* Visit the CloudFront distribution Domain Name from Browser.

* You will be redirected to S3 static website.

![](https://cdn-images-1.medium.com/max/2000/1*hy6Kj8J4IXZVCnRXseM_iw.png)

![](https://cdn-images-1.medium.com/max/3668/1*4YIi1LvEuTBavmhssdOGAg.png)

![CDN](https://cdn-images-1.medium.com/max/3870/1*PuVaT5QvxeQz0WdBbKI8LQ.png)

**STEP 7 : Add EC2 as Origin in CDN Distribution:**

* Navigate to Origin Tab of CloudFront Service on AWS Console.

* Create Origin -> Enter EC2 PUBLIC DNS as Origin Domain.

* **NOTE : ONLY EC2 PUBLIC DNS WILL WORK NOT PUBLIC IP.**

* Protocol -> HTTP Only

* Keep rest of the settings as default & hit the create Origin.

![](https://cdn-images-1.medium.com/max/3260/1*E9oVCgQLSJp_WJk1Y34Ftw.png)

**STEP 8 : Create Origin Groups of S3 & EC2:**

* Navigate to Origin Tab of CloudFront Service on AWS Console.

* Create **Origin Group**

* Choose Origins -> 1st s3 Origin & 2nd EC2 Origin.

* Enter Name for **Origin Group **of your choice.

* Select all Failover Criteria.

* Finally, Create Origin Group.

* **IMP: **This is for origin failover for scenarios that require high availability. We created an *origin group* with two origins, a primary(S3) and a secondary(EC2). If the primary origin is unavailable, or returns specific HTTP response status codes that indicate a failure, CloudFront automatically switches to the secondary origin.

![](https://cdn-images-1.medium.com/max/2000/1*xg1_vyjGz_U7_eEErkoXvQ.png)

**STEP 9 : Change Behavior of CDN :**

* Navigate to Behavior's Tab of CloudFront Service on AWS Console.

* Select the one listed & hit the edit button.

* Now change the Origin to Origin Group (created in step 8)

* Save it & lets play around with CloudFront.

![](https://cdn-images-1.medium.com/max/3686/1*POWAAtQH_WAMSno0i9DHzQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*JfA8nguW_d9yP-08mcpT2Q.png)

**STEP 10 : Lets play with CloudFront :**

* Now hit the below URLs to understand the working of distribution.

* http://<cloudfront_distribution_domain_name> (Pointing towards s3 index.html)

* http://<cloudfront_distribution_domain_name>/index.html (Pointing towards s3 index.html)

* http://<cloudfront_distribution_domain_name>index2.html (Pointing towards EC2 index.html)

* Lets Do a FAILOVER →

* Remove index.html from s3 bucket.

* After a while it will point it towards EC2 index.html

* This concludes our lab, which shows how CloudFront Distribution works along with origins & origin groups to achieve High Availability, speed & secure content delivery.

**STEP 10 : Decommission :**

* Terminate the EC2 Instance.

* Disable & Delete the CloudFront Distribution.

* Permanently delete the S3 object & S3 buckets.

## Congrats ! We have successfully completed lab for AWS CloudFront for High Availability.

![](https://cdn-images-1.medium.com/max/3144/1*BxfgHQkXG7JLHn9ohjYVeg.png)

*I am Kunal Shah, AWS Certified Solutions Architect, helping clients to achieve optimal solutions on the Cloud. Cloud Enabler by choice, DevOps Practitioner having 8+ Years of overall experience in the IT industry.*

*I love to talk about Cloud Technology, DevOps, Digital Transformation, Analytics, Infrastructure, Dev Tools, Operational efficiency, Serverless, Cost Optimization, Cloud Networking & Security.*

#aws #community #builders #devops #cloudfront #cdn #s3 #ec2 #content #delivery #network #solution #management #centralize #quick #secure #speed #performance #edge #locations #operations #infrastructure #scalable #reliable #highly #available #design #acloudguy

*You can reach out to me @ [acloudguy.in](https://acloudguy.in/)*