with Amazon CloudFront
In this tutorial, you will learn how to deliver content and decrease end-user latency of your web application using Amazon CloudFront. CloudFront speeds up content delivery by leveraging its global network of data centers, known as edge locations, to reduce delivery time by caching your content close to your end users. CloudFront fetches your content from an origin, such as an Amazon S3 bucket, an Amazon EC2 instance, an Amazon Elastic Load Balancing load balancer or your own web server, when it's not already in an edge location. CloudFront can be used to deliver your entire website or application, including dynamic, static, streaming, and interactive content.
In the following steps, you will configure an Amazon S3 bucket as the origin and test your distribution using a web browser to ensure that your content is being delivered.
Everything done in this tutorial is free tier eligible.
Deliver Content Faster Requires an Account
Sign in to the ConsoleAWS Free Tier includes 50 GB of Data Transfer Out and 2,000,000 HTTP or HTTPS Requests per month for one year.
In this step, we will upload sample static content to an Amazon S3 bucket. In later steps, we will use this bucket as a CloudFront origin. Amazon S3 is a good choice for an Amazon CloudFront origin that includes static content such as images, videos, HTML pages, .css files, and .js files.
a. The sample image we will use in this tutorial is a PNG image called cloudfront-test-image.png. Click the button on the right to download the sample image and make sure it is saved as cloudfront-test-image.png.
b. When you click here, the AWS management console will open in a new browser window. Type S3 in the search box, and double click to open the dashboard.
c. 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.
Select Next.
On the Set properties panel, you have many useful options for your S3 bucket including Versioning, Server Access Logging, Tags, Object-level Logging and Default Encryption. We won't enable these features for this tutorial.
Select Next.
On the Set permissions panel, you can set access control permissions for our bucket. We won't enable these features for this tutorial.
Select Next.
On the Review panel, select Create bucket.
e. On the Set permissions tab, you will need to make your image publicly readable. By default, S3 objects are set to private. Change Manage public permissions to "Grant public access to objects(s)."
Select Next.
On the Set properties tab, you can set Storage class, Encryption, and Metadata. We won't enable these features for this tutorial.
Select Next.
On the Review tab, review your file upload.
Select Upload.
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.
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. If you would like to learn more about each configuration setting, click the dropdown below.
Once you configure your distribution, CloudFront will create the distribution and propagate it throughout the CloudFront network for you to use. Select 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.
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.
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. Use Amazon Cloudfront to speed delivery of your entire website or application, including dynamic, static, streaming, and interactive content.
Now that you have learned how to set up a web distribution and deliver a piece of static content with Amazon CloudFront, you can ask questions to our CloudFront experts during upcoming Office Hours or upcoming AWS Tech Talks.
Amazon CloudFront Getting Started Page >>