~/aws/static-website-hosting-aws-s3-cloudflare.md

Static Website Hosting with AWS S3 and Cloudflare

3 min readMay 30, 2018by Gorav Singal#aws#cloudflare#static-website
// TL;DR

Set up an AWS S3 bucket for static website hosting and configure Cloudflare as the CDN and DNS provider for your domain.

Static Website Hosting with AWS S3 and Cloudflare

Static websites have several advantages over dyanamic websites. If you are running your personal blog or portfolio or small websites. Static websites is the way to go.

Although, this post is not for listing benefits of static website. I will write more on this in another post.

Pre-Requisite

  1. You have AWS account
  2. You have cloudflare account (Free account)

Configure S3 bucket

  1. Create an S3 bucket with the name of your website say: www.gyanbyte.com
  2. After creating bucket, goto bucket. Then, click on Properties.
  3. Click on Static website hosting. Click on first option: Use this bucket to host a website In index document, put index.html (the starter or homepage html file) In Error document, put the html file like 404.html

Static Website Hosting with AWS S3 and Cloudflare

  1. Save it.
  2. Click on Permission tab
  3. Click on bucket policy. In the editor, type below:
{
    "Version": "2012-10-17",
    "Id": "Policy1527570565509",
    "Statement": [
        {
            "Sid": "Stmt1527570562766",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::www.gyanbyte.com/*"
        }
    ]
}

Remember to replace your bucket name in Resource above.

  1. Save

Note: It will now show your bucket as Public access. You require to have this bucket public read access.

non www bucket

Note, for SEO purpose you would want your website to open on either www. version or non-www version. Create another bucket with non-www version, say: gyanbyte.com

  1. Again, goto Public tab on this bucket. Put above policy in Bucket policy. (Exactly same, except bucket name)
{
    "Version": "2012-10-17",
    "Id": "Policy1527570565509",
    "Statement": [
        {
            "Sid": "Stmt1527570562766",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::gyanbyte.com/*"
        }
    ]
}

Note: the non-www bucket above.

  1. Goto Static website hosting tab
  2. Click on Redirect requests
  3. In target bucket or domain, type your domain name with www, and protocol https.

Static Website Hosting with AWS S3 and Cloudflare

Note: We will have our website to open on https protocol. With cloudflare free SSL support.

Copy Website files

Now, copy your website files over ot this bucket.

Test your URL for s3 bucket website

Note down your URL from static website hosting tab. Try opening that URL in browser. You should see your website. If you are not seeing expected result. You should see above steps carefully.

Proceed only when you see your website above.

Cloudflare configuration

I assume you have registered on cloudflare free account, and added your website.

  1. Goto DNS tab.
  2. If you are doing it for first time. Delete all records there.
  3. Add a CNAME record with name as www, and value as URL of your bucket. Remove http:// in the begining and ending slash at the end of your bucket URL.
  4. Add a CNAME record with name as @, and value as URL of your non-www bucket.

Static Website Hosting with AWS S3 and Cloudflare

DNS changes in your registrar end

From where you purchased your domain, goto its DNS setting. And, change its nameservers as mentioned in DNS tab of cloudflare. In my case,

Static Website Hosting with AWS S3 and Cloudflare

Wait for few minutes, to allow DNS changes to be done. It should be done with in 10 minutes. But, in some cases, it might take hours.

Now, if you open your website name. You would see your static website pages.

All set.

Hope you enjoyed the post.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to upload files on AWS S3 by using curl, without having command line aws or other tool

How to upload files on AWS S3 by using curl, without having command line aws or other tool

Introduction There were few files that I need to take backup from a machine that…

Troubleshoot AWS Lambda unknown error!

Troubleshoot AWS Lambda unknown error!

After 2 days, there was my demo. I deployed my nodejs code on lambda function…

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

Introduction I have my main website, which I run on Lets say: . Now, there is my…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

File Uploads and S3 Integration in Node.js

File Uploads and S3 Integration in Node.js

File Upload Architecture There are two main patterns for handling file uploads…

Deploying Node.js to AWS

Deploying Node.js to AWS

Choosing Your Deployment Strategy Service Best For Scaling Cold Start ECS…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…