Skip to main content

Integrate AWS CloudFront with S3 bucket

Amazon CloudFront and Other Amazon Web Services: 
  • Amazon CloudFront: CloudFront can be used to deliver your entire website or application, including dynamic, static, streaming, and interactive content, using a global network of edge locations.
  • Amazon CloudFront works well with other Amazon Web Services. You can easily use an Amazon S3 bucket, an Amazon EC2 instance, an Amazon Elastic Load Balancing load balancer, or your own web server as the origin. In this tutorial, we will use Amazon S3 as the origin.

Step 1: Prepare Your Content

To deliver content to end users with a web distribution, you need to prepare content.  Amazon CloudFront distributes many types of content, including images, videos, HTML pages, .css files, and .php files.  In this step, we will upload sample content to the cloud using Amazon S3.

a. The sample image we will use in this tutorial is a PNG image called cloudfront-test-image.png.  Click the button on the below to download the sample image and make sure it is saved as cloudfront-test-image.png. If you want, you can use an image you already have saved on your computer.

 download the sample image 


b. Upload cloudfront-test-image.png to an Amazon S3 bucket that the web distribution can reference:
  • If you already own an S3 bucket: Upload the image to an existing S3 bucket, remember to make it public, and skip to Step 2.
  • If you do not own an S3 bucket: You can easily set one up for free - continue on to step 1c.


cloudfront11

Backup Files to S3 - Console
c. When you click here, the AWS management console will open in a new browser window. Find S3 under Storage & Content Delivery, and double click to open the dashboard.



d. Now that you are in the S3 dashboard, click the Create Bucket button.
  • Bucket Name: Enter a name for your bucket - it must be unique and all lower case. For additional restrictions on S3 bucket names, see Bucket Restrictions and Limitations
  • Region: You have the option to choose a region close to you to optimize latency, minimize costs, or to address regulatory requirements.  For this tutorial, use the default of US Standard.
Click Create.

e. On the dashboard, select your newly created bucket, then click Upload.




f. Click Add Files and select cloudfront-test-image.png from your computer.  Then, click the Start Upload button.  Your image will be uploaded to an S3 bucket that CloudFront can reference.





g. You will need to make your image publicly readable.  By default, S3 objects are set to private.  Hover your mouse over cloudfront-test-image.png, right click, and select Make Public.







cloudfront-18

   

Step 2: Enter the CloudFront Console

Once your content is uploaded to Amazon S3, you can use the Amazon CloudFront console to deliver content to end users around the world.

a. When you click here, the AWS management console will open in a new browser tab.  Find CloudFront under Storage & Content Delivery and click to open the Amazon CloudFront Console.

b.  Select Create Distribution.


A web distribution specifies configuration settings so CloudFront knows which origin to get your content from when a user requests it. You can use Amazon S3 buckets (storage folders) and HTTP servers (for example, web servers) as an origin.


a.  You will first choose the delivery method for your content.  A web distribution is used for static and dynamic content while the RTMP distribution is optimal for streaming media files using Adobe Flash Media Server's RTMP protocol.
Because cloudfront-test-image.png is a static image, select Get Started under the Web option.

b.  You will now configure your settings.
In Origin Domain Name, select the name of the S3 bucket you stored cloudfront-test-image.png in.
You can leave the rest of the configuration settings with the default values for this tutorial.
Default Cache Behavior Settings:
Distribution Settings:
  • Price Class CloudFront has three Price Classes determined by the number of Edge Locations you want supported throughout the globe. By default, CloudFront uses all edge locations to ensure the best performance. For more information, see Amazon CloudFront Pricing.
  • AWS WAF Web ACL: if you want to allow or block HTTP and HTTPS requests based on criteria you specify, choose the web ACL to associate with this distribution. For more information, see the AWS WAF Developer Guide.
  • Alternate Domain Names: CloudFront will automatically assign you a domain name when you create a web distribution (for example, http://abcdefghi888.cloudfront.net/filename.jpg). You can also specify your own domain name in the URLs for your objects (such as http://yourDomain.com/filename.jpg) once you update your DNS configuration to route DNS queries to your CloudFront distribution.For this tutorial, leave this field blank.
  • SSL Certificate:  CloudFront provides a default certificate or you can specify custom SSL certificate. 
  • Default Root Object: This is where you specify the object that you want CloudFront to request from your origin when a user requests just the root URL of your distribution. For example, a user requests http://www.example.com/, and you can specify that http://www.example.com/product-description.html is delivered. Specifying a default root object avoids exposing the contents of your distribution.
  • Logging: CludFront can log information about each request for an object and store the log files in an Amazon S3 bucket. There is no extra charge to enable logging, but you accrue Amazon S3 charges for storing and accessing files.
  • Bucket for Logs: If you specified an Amazon S3 bucket to store access logs in, CloudFront can record information about each user request for an object and store the files in the specified Amazon S3 bucket. You can enable or disable logging at any time. For more information about CloudFront access logs, see Access Logs.
  • Log Prefix: If you chose On for Logging, you can specify the string that you want CloudFront to prefix to the access log filenames for this distribution. For more information about CloudFront access logs, see Access Logs.
  • Cookie Logging: When using Amazon S3 as the origin for your objects, Amazon S3 doesn't process cookies, so unless your distribution also includes an Amazon EC2 or other custom origin, you need to select Off for the value of Cookie Logging. For more information about cookies, see Configuring CloudFront to Cache Objects Based on Cookies.
  • Comment: You can enter any comments that you want to save with the distribution.
  • Distribution Status: The default is Enabled. What this means is that your content will be available as soon as the distribution is created.
Once you configure your distribution, CloudFront will create the distribution and propagate it throughout the CloudFront network for you to use.  Click Create Distribution.
After this process is complete, your status column will change from In Progress to Deployed.
Note: this can take a few minutes to complete.
The domain name that CloudFront assigns to your distribution appears in your list of distributions. Keep this in mind, you will need it for the next step. Clicking on the Distribution ID field takes you to a page where you will see the full CloudFront domain under Domain Name.
After you create your web distribution, you can test it out by confirming that CloudFront gets your object from the origin and returns it to a web browser.  For this test, you will need the CloudFront domain name from Step 4 and the image name you uploaded to the S3 bucket in step 1.


a. Open a text editor on your computer. Copy and paste the following HTML code:
 
<html>
<head>My CloudFront Test</head>
<body>
<p>My text content goes here.</p>
<p><img src="http://domain name/object name" alt="my test image">
</body>
</html>
  • Replace domain name with the domain name that CloudFront assigned to your distbribution, such as d111111abcdef8.cloudfront.net.
  • Replace object name with the name of your image file in the Amazon S3 bucket - in our case, cloudfront-test-image.png.
  • Save the text in a file as mycloudfronttest.html.

b. Open your web page in a browser to ensure that you can see your content.

You can easily disable the distribution from the CloudFront console. In fact, it is a best practice to disable distributions you are no longer using so you don’t keep getting charged for them. However, make note that when a distribution is disabled, any website supported by that distribution will be down and the content will no longer be accessible to end users.


a.  Select the checkbox next to the distribution you created and click Disable.

b. You will be asked to confirm - click Yes, Disable.
You created your first Amazon CloudFront web distribution and delivered a piece of static content hosted in the cloud through Amazon S3. With a few configuration changes, you can use CloudFront to deliver dynamic content, live events such as a meeting, conference or concert, in real time over HTTP or HTTPS

Comments

Popular posts from this blog

CDN (Content Delivery Network) - AWS CloudFront

Firstly start with CDN - CDN stands for content delivery network. It is used to deliver different type of data to end users like images,videos,applications and APIs. When you see a dynamic images using adobe flash on website. That's the use of CDN. It just stores the cached copy of contents and delivers it to end users for better speed. How it works? A CDN uses a network of servers in geographically dispersed locations(edge locations) to cache copies of content close to end user, lowering latency when thay download or stream the objects. When a specific page, file, or program is required by a user ,the server dynamically selected to deliver the content is based on closest network proximity or the one with the fewest network hops to the end user. Content is replicated across edge locations thereby providing redundancy.   CDN Benefits Improves customer experiance with faster page loads - bounce rates go down and customers stay on your site longer . SEO benefits: site spee

Basic but lesser known Linux Commands

For Debian/Ubuntu- 1.  sudo!!   : Forgot to run a command with sudo? You need not re-write the whole command, just type “sudo!!” and the last command will run with sudo. 2.  Python -m SimpleHTTPServer  : Creates a simple web page for the current working directory over port 8000. 3.  mtr : A  command which is a combination of ‘ping’ and ‘traceroute’ command. 4.  Ctrl+x+e  : This key combination fires up, an editor in the terminal, instantaneously. 5.  nl  : Outputs the content of text file with lines Numbered. 6.  shuf  : Randomly selects line/file/folder from a file/folder. 7.  ss  : Outputs Socket Statistics. 8.  Last : Want to know history of last logged in users? This command comes to rescue here. 9.  curl ifconfig.me  : Shows machine’s external IP Address. 10.  tree  : Prints files and folders in tree like fashion, recursively. 11.  Pstree  : Prints running processes with child processes, recursively. 12.  <space> command  : A space before a bash command, is