What is CloudFront and how to Setup CloudFront with AWS S3 and ALB Distributions

Internet users are always impressed with a high speed & loading capacities websites . Why Not you have a website that loads the content within quick seconds and delivering it ?

In this tutorial you learn what is Cloud Front and how to set Cloud Front Distributions in Amazon cloud. Cloud Front enables and helps users to retrieve their content quickly by utilizing the concept of caching.

Table of Content

  1. What is Cloud Front?
  2. Prerequisites
  3. Creating an IAM user in AWS account with programmatic access
  4. Configuring the IAM user Credentials on local Machine
  5. Setting up Amazon CloudFront
  6. How to Use Custom URLs in CloudFront by Adding Alternate Domain Names (CNAMEs)
  7. Using Amazon EC2 or Other Custom Origins
  8. Conclusion

What is Cloud Front

CloudFront is a Amazon web service that helps in speeding up the distribution of content either static or dynamic such as .html, .css, .js , images and many more to users. CloudFront delivers the content using edge locations when any request is requested by users.

By utilizing the Cloud Front the content is delivered to the users very quickly using edge location. In case content is not available in edge locations then CloudFront request from the origin configured. Origins could be like AWS S3 bucket or HTTP server or Load Balancer etc.

Use cases of Cloud Front

  • It accelerates the delivery of your static website such as images , style sheets , Java script and so on.
  • Live streaming of video
  • Also use of Lambda at edge location with CloudFront adds more variety of ways to customize cloud front.

How CloudFront delivers content to your users

  • User makes a request to website or application let say a html page http://www.example.com/mypage.html
  • DNS server routes the request to Cloud Front edge locations.
  • Cloud Front checks if the request can be fulfilled with edge location .
  • IF Edge location have the files , then CloudFront sends back to the user else
  • CloudFront queries to the origin server
  • Origin server sends the files back to edge location and then Cloud front sends back to the User.

How CloudFront works with regional edge caches

This kind of cache brings the content more closer to the users to help performance. Regional edge caches help with all types of content, particularly content which becomes less popular over time such as user-generated content, such as video, photos, or artwork; e-commerce assets such as product photos and videos etc.

This cache sits in between the origin server and edge locations. The Edge location stores the content and cache but when the content is too old it removes it from its cache. There comes the role of regional cache which has wide coverage to store lots of content.

Prerequisites

  • You must have AWS account in order to setup AWS CloudFront. If you don’t have AWS account, please create a account from here AWS account.
  • You must have IAM user with Administrator rights and setup credentials using AWS CLI or using AWS Profile. You will see this below step to create IAM and configure credentials.
  • AWS S3 bucket

Creating an IAM user in AWS account with programmatic access

In order to connect to AWS Service, you should have an IAM user with an Access key ID and secret keys in the AWS account that you will configure on your local machine to connect to AWS account from your local machine.

There are two ways to connect to an AWS account, the first is providing a username and password on the AWS login page on the browser and the other way is to configure Access key ID and secret keys on your machine and then use command-line tools to connect programmatically.

  1. Open your favorite web browser and navigate to the AWS Management Console and log in.
  2. While in the Console, click on the search bar at the top, search for ‘IAM’, and click on the IAM menu item.
  1. To Create a user click on Users→ Add user and provide the name of the user myuser and make sure to tick the Programmatic access checkbox in Access type which enables an access key ID and secret access key and then hit the Permissions button.
  1. Now select the “Attach existing policies directly” option in the set permissions and look for the “Administrator” policy using filter policies in the search box. This policy will allow myuser to have full access to AWS services.
  1. Finally click on Create user.
  1. Now, the user is created successfully and you will see an option to download a .csv file. Download this file which contains IAM users i.e. myuser Access key ID and Secret access key which you will use later in the tutorial to connect to AWS service from your local machine.

Configuring the IAM user Credentials on local Machine

Now, you have an IAM user myuser created. The next, step is to set the download myuser credentials on the local machine which you will use to connect AWS service via API calls.

  1. Create a new file, C:\Users\your_profile\.aws\credentials on your local machine.
  2. Next, Enter the Access key ID and Secret access key from the downloaded csv file into the credentials file in the same format and save the file.
[default]     # Profile Name
aws_access_key_id = AKIAXXXXXXXXXXXXXXXX
aws_secret_access_key = vIaGXXXXXXXXXXXXXXXXXXXX

credentials files help you to set your profile. By this way, it helps you to create multiple profiles and avoid confusion while connecting to specific AWS accounts.

  1. Similarly, create another file C:\Users\your_profile\.aws\config in the same directory
  2. Next, add the “region” into the config file and make sure to add the name of the profile which you provided in the credentials file, and save the file. This file allows you to work with a specific region.
[default]   # Profile Name
region = us-east-2

Setting up Amazon CloudFront

  • Open your favorite web browser and navigate to the AWS Management Console and log in.
  • While in the Console, click on the search bar at the top, search for ‘CloudFront’, and click on the CloudFront menu item.
  • Click on Create distributions and then Get Started
  • In the Origin setting provide the S3 bucket name and keep other values as default.
  • For the settings under Default Cache Behavior Set and Distribution Settings, accept the default values and then click on Create distribution.
  • AWS S3 bucket is already created before we started this tutorial. Lets upload a index.html ( with a text hello ) to the bucket and set the permission as public access as shown below.
  • Now check the Amazon S3 URL to verify that your content is publicly accessible
  • Check the CloudFront URL by hitting Domain Name /index.html and it should show the same result as your index.html file contains
domainname/index.html

How to Use Custom URLs in CloudFront by Adding Alternate Domain Names (CNAMEs)

In CloudFront as seen above the CloudFront URL is generated with a domain name *.cloudfront.net by default. If you wish to use your own domain name that is CNAME such as abc.com in the URL , you can assign it as yourself.

  • In our case by default the URL is :
http://dsx78lsseoju7.cloudfront.net/index.html
  • If you wish to use alternate domain such below, follow the step as below
http://abc.com/index.html
  • Go back to CloudFront Page and look for the distribution where you need to change the domain and click on Edit
  • Provide the domain name and you must have SSL certificate already in place.
  • Finally Create an alias resource record set in Route 53 by visiting Route53 Page .
  • Go to the Route53 Page by searching on the top of the AWS Page
  • Click on the Hosted Zone and then click on Create Record
  • Now Here, Provide the name of Record which can be anything, Record type and Route traffic as CloudFront distribution

After Successful creation of Route 53 you can verify the index page ( http://mydomain.abc.com/index.html ) if it works fine.

Using Amazon EC2 or Other Custom Origins

A custom Origin can be a Amazon Elastic Compute Cloud (AWS EC2) for example http server. You need to provide the DNS name of the server as custom origin.

Below are some key points to keep in mind while setting the custom origin as AWS EC2.

  • Host and serve the same content on all server in same way.
  • Restrict access requests to the HTTP and HTTPS ports that your custom origin listens on that is AWS EC2.
  • Synchronize the clocks of all servers in your implementation.
  • Use an Elastic Load Balancing load balancer to handle traffic across multiple Amazon EC2 instances
  • When you create your CloudFront distribution, specify the URL of the load balancer for the domain name of your origin server

Conclusion

In this tutorial you learnt what is Cloud Front and how to set Cloud Front Distributions in Amazon cloud. Cloud Front enables and helps users to retrieve their content quickly by utilizing the concept of caching.

By Now, you know what is CloudFront and how to setup CloudFront , what are you going to manage with CloudFront Next ?

One thought on “What is CloudFront and how to Setup CloudFront with AWS S3 and ALB Distributions

Leave a Reply to Ophelia Origami Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s