Skip to content

How to host a static website in AWS S3 bucket?

S3 (Simple storage service) in AWS is an object storage service that offers industry-leading scalability, data availability, security, and performance. S3 can be used to host a static website without stressing much about the host environment.

To host a static website in AWS S3 bucket, login to the AWS management console and go to S3 Dashboard.

In S3 Dashboard click on Create Bucket and give the name of the bucket same as website domain name (for eg: “rprateek.com” or in this case “awsweb.ramaregmi.com”) remember bucket name is unique around the world.

Select the region where to host the bucket and untick Block all public access and acknowledge it (to make website publicly accessible). 

Go to the bucket and click on properties and scroll down to “Static website hosting” and Edit => Enable Static website hosting.

Provide the Index document > “index.html” and the Error document > “error.html” then save changes.

Upload files (index.html, error.html, all required files for a website)

Scroll down to the static website hosting section to find URL link (Bucket website endpoint), copy that and paste it into the browser. You will see an Access denied error message as permission or bucket policy to allow traffic is not setup.

Go to the permissions tab and below the “Block all public access” there is a bucket policy section > Edit 

You can create a policy by using the Policy Generator tool or just type in your policy. For this demo enter the generic allow policy:

In the browser refresh the URL link of S3, the static website is up and running.

Things to remember to successfully host a static website in S3:

  1. Enable versioning in the S3 bucket 
  2. Allow public access to S3 bucket
  3. Grant permission via bucket policy to access files in S3 bucket

Redirect subdomain traffic to the main website in S3

After configuring the root domain for website hosting, it is a good idea to configure a subdomain to redirect all requests to the main domain itself. if the root domain is “rprateek.com” if the user type in the “www.rprateek.com” subdomain it should redirect traffic to the root domain “rprateek.com”.

Create another S3 bucket with the subdomain name > www.rprateek.com

Follow all the steps above for static website hosting.

Go to properties > Static website hosting > Edit

Hosting type > Redirect requests for an object

Host name > rprateek.com (redirect to root domain)

Protocol > HTTP > Save Changes

Now all the traffic from the www.rprateek.com subdomain will be redirected to rprateek.com.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *