When to use Amazon Cloudfront or S3

Amazon S3 is designed for large-capacity, low-cost file storage in one specific geographical region.* The storage and bandwidth costs are quite low.

Amazon CloudFront is a Content Delivery Network (CDN) which proxies and caches web data at edge locations as close to users as possible.

When end users request an object using this domain name, they are automatically routed to the nearest edge location for high performance delivery of your content. (Amazon)

The data served by CloudFront may or may not come from S3. Since it is more optimized for delivery speed, the bandwidth costs a little more.

If your user base is localized, you won't see too much difference working with S3 or CloudFront (but you have to choose the right location for your S3 bucket: US, EU, APAC). If your user base is spread globally and speed is important, CloudFront may be a better option.

Both S3 and CloudFront allow domain aliases, however CloudFront allows multiple aliases so that d1.mystatics.com, d2.mystatics.com and d3.mystatics.com could all point to the same location increasing the capacity for parallel downloads (this used to be recommended by Google but with the introduction of SPDY and HTTP/2 is of lesser importance).

CloudFront also supports CORS as of 2014 (thanks sergiopantoja).

* Note: S3 can now automatically replicate to additional regions as of 2015.


CloudFront and S3 Bucket is not the same. In layman's terms: CloudFront enables you to accelerate content delivery of your web contents via Content Delivery Network (CDN) in edge locations, whereas S3 Buckets are where you store your actual files. CloudFront sources may not necessarily be from S3 but for easier visualization of S3 integration with CloudFront: enter image description here


Amazon CLOUDFRONT and S3 are two different services provided by Amazon Web Services.

Amazon S3 is a storage service in which we can store static files like:

css, images, javascripts,videos, etc...

Amazon CloudFront is a middle-ware which stands in between a user requesting for a file from AWS and the S3 data center in a specific region, CloudFront is used to speeds up distribution of your static and dynamic web content from S3 to the User.

You can understand it better by an example:-

For example, your S3 is located in AWS region US East (N. Virginia) which a data center location to store your files.

If a user from India tries to access a file from a AWS server in Virginia, then user would need to go to that specific location with a request, and this will take a lot of time.

What CloudFront does is that it stands as a middleware between user and AWS S3.


The most often used files can be cached on CloudFront and what it does is, it replicates those files on edge locations (To deliver content to end users with lower latency, Amazon CloudFront uses a global network of edge locations for content delivery).

If the content is already in the edge location with the lowest latency, CloudFront delivers it immediately. If the content is not currently in that edge location, CloudFront retrieves it from an Amazon S3 bucket and provides it to user as faster as it can.

Every request is given a new DNS from CloudFront to the S3, so this would result in lower traffic as well as more parallel request processing.

Following is the list of current edge locations as per July-2016:-

United States

  • Ashburn, VA (3)
  • Atlanta, GA
  • Chicago, IL
  • Dallas/Fort Worth, TX (2)
  • Hayward, CA
  • Jacksonville, FL
  • Los Angeles, CA (2)
  • Miami, FL
  • New York, NY (3)
  • Newark, NJ
  • Palo Alto, CA
  • San Jose, CA
  • Seattle, WA
  • South Bend, IN
  • St. Louis, MO

Europe

  • Amsterdam, Netherlands (2)

  • Dublin, Ireland

  • Frankfurt, Germany (3)

  • London, England (3)

  • Madrid, Spain

  • Marseille, France

  • Milan, Italy

  • Paris, France (2)

  • Stockholm, Sweden

  • Warsaw, Poland

Asia

  • Chennai, India

  • Hong Kong (2)

  • Mumbai, India

  • Manila, the Philippines

  • New Delhi, India

  • Osaka, Japan

  • Seoul, Korea (3)

  • Singapore (2)

  • Taipei, Taiwan

  • Tokyo, Japan (2)

Australia

  • Melbourne, Australia

  • Sydney, Australia

South America

  • São Paulo, Brazil

  • Rio de Janeiro, Brazil

From this you can conclude that if the users are limited are from the same region as your S3 is hosted on, then you do not require to go for CloudFront, and if the number of users is increased on global level then you should definitely use CloudFront for better latency and traffic control.


Another major difference is that CloudFront allows you to mirror the site on your server. Cloudfront then caches the files such as images, mp3 or video using its content delivery network.

This saves you having to duplicate your assets as you would when you use Amazon S3.

However, after a file expires, CloudFront will fetch it again from your live site (at cost). So cloud front is best for frequently accessed files, and less so for infrequently accessed ones.

One way to set the file expiry for apache is in .htaccess. For example

<filesMatch "\\.(mp3|mp4)$">
Header set Cache-Control "max-age=648000, private"
</filesMatch>