How to setup AWS cloudfront, S3 with subdomain.

If you are planning to use CDN then AWS -Amazon is a good choice? Why :

  • It’s cheap
  • It provides a lot of options and customization
  • Very good documentation
  • Options for SSL certificate Management, backups, authentication and much more…

Some other popular CND providers are MaxCDN, Cloudflare, KeyCDN, Google Cloud…

You can probably migrate images/media files etc to Amazon – CloudFront (their CDN). You have an option to create a subdomain (like which will point to your CloudFront directory.

To get started here are a few things you need to have an AWS account, you can open it for free! You won’t be charged unless you see some service.

You can check AWS Cloudfront pricing here

We will be using Amazon S3 as the origin (whatever is uploaded to origin with public permissions will be visible to CloudFront).

The goal of this tutorial is to create AWS CDN (CloudFront) and access via your own subdomain.


Create an S3 Bucket:

Once you login to AWS console, you will see below screen



Click S3 under Storage section.

Once you click it then you can click on “Create Bucket”


Bucket name has to be unique (just like a domain name).

Click Next and proceed with default options (we will look in setting up permissions later in this tutorial). By default –  account owner will get all access and public will have no access.

Once you create test bucket, you can upload files to the bucket. You have a lot of options within S3 (goal of this tutorial is to setup cloudfront and Not go over S3 in detail).

After uploading file, you can access it via URL (but since there is no public access so you cannot access it) like<bucket name>/<filename>. For CloudFront to work with S3 you need to grant public read access to files which you want to be able to access via cloudfront. You can select the file in S3 and grant public access to it. Once you give access you will be able to access<bucket name>/<filename>.

Goto CloudFront

Go to CloudFront and click on “create distribution” then select contribution type as “web”.

Fill details for the distribution (like S3 bucket details, protocol policy, TTL, CNAME etc).

If you have your own SSL certificate, you can upload to Amazon SSL Certificate manager (ACM)  and use it!  You will probably prefer to use https (for all your files) with your own SSL certificate.

“Origin Domain Name” is <bucket name>

Note: The files in your origin must be publicly readable (else you won’t be able to access via CloudFront and you will get “permission denied” error)



Once you create a distribution Cloudfront will create it’s own subdomain which can be used like :

http://<xyzsdsd><files> and you can use this type of URL in your pages. Another option is to use a subdomain.

Route 53 – use your own subdomain

Goto AWS – Route 53

Create a new record set


Set “Alias” to “Yes”

“Alias Target” is the name of the CloudFront subdomain. (like <xyzsas> of your distribution.

Now go back to your CloudFront Distribution and set  “Alternate Domain Names (CNAMEs)” to name of your subdomain.

You should be all set now!

You should be able to access your files from URL like https://<subdomain>.<yourdomain>/<files>. (if you are using SSL).

If you are a CLI user you can use aws cli to automatically upload/sync your files to S3 (so that you don’t have to manually upload them).

CloudFront Usage Reports

You can checkCloudFront usage Report under CloudFront distribution (like number of access request for your files, you can filter by date, region, protocol etc)


+ There are no comments

Add yours