AWS CloudFront for High Availability

AWS Cloud Hands-on Lab Practice Series

acloudguy.in
7 min readNov 28, 2023

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 -

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.

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>/*”
}
]
}

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.

STEP 4 : Create index.html & upload it.

  • Create a file named index.html in notepad, Copy the content from GitHub.
  • 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…
s3 static website

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.
  • Hit the Launch Instance & wait for its status to running.
  • 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
index2.html

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.
CDN

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.

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.

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.

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.

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

PlainEnglish.io 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

acloudguy.in

AWS Solutions Architect | DevOps | Cloud Technology Enabler | Cloud Network & Security | CFT | Terraform | K8s | SysOps | Docker | FinOps | Blog | Share |