Skip to main content
duffn.dev

Static Websites with AWS S3 and Cloudflare

Share: 

Amazon Web Service's S3 is an excellent place to deploy a static website. If Cloudflare is your DNS provider, however, there are a few tricks that you need to be aware of when setting up your static site.

We're going to talk about how to get started with your static site on S3 and your DNS in Cloudflare.

AWS #

First, of course, we need a static site deployed to S3. The AWS documentation does a good job at explaining how to setup a site, but we'll briefly run through the steps here as well.

S3 Bucket Configuration #

One of the first things that you'll see in the AWS documentation linked above is that your bucket name must match the name of the website you are hosting. That's true in most cases, but since we're going to be using Cloudflare DNS, that's not true in our case.

Oh, and we don't even need to tell AWS that we're going to be using this bucket to host our site. I'll show you why shortly.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::example.com/*"]
}
]
}

NOTE: This makes items in your bucket available to the public. Do not put things in this bucket that you do not want everybody in the world seeing.

Cloudfront Distribution #

Now, we're going to create a Cloudfront distribution in AWS. While not strictly necessary for a static website on AWS, it is necessary to get Cloudflare's SSL to work with our site.

Cloudflare #

We're in the final stretch now. We only have to setup Cloudflare to point to our Cloudfront distribution. From here on out, I'm going to assume that you already have an account and have setup your domain in Cloudflare.

DNS #

And that's it! Wait a few minutes (most likely seconds), and you'll see your static site served from S3, through a Cloudfront distribution and finally to Cloudflare under your custom domain.