aws|May 30, 2018|3 min read

Static Website Hosting with AWS S3 and Cloudflare

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.gyanblog.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.gyanblog.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: gyanblog.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:::gyanblog.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.

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…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…