DEV Community

Muhammad Afzal for AWS Community Builders

Posted on

Setting up Static Website on AWS S3

Hello Geeks,

You gotta an overview of article by the title. It's 2022 and world is using CMS System for their websites or custom developed dynamic websites but still there are lot of geeks who have static websites and i am one of them. Back in 2018 i develeoped a javascript enabled web based MQTT Client for MQTT Broker testing and debugging. For the hosting of http://mqtt.run i used lot of different web hosting services but after becoming AWS Community Builder i started reviewing the services related to cloud storage, Already i am using AWS Lightsail and CloudFront for my E-Commerce & Tutorial website https://phpdocs.com but this time when i studied about S3 i made a plan to host one of my static website on S3 to get the taste of cloud for static website.

So if we generally talk about AWS S3 it's a storage services to host docs, images, videos or what ever you want to host on it. S3 is a reasonable and good solution in terms of Space, Bandwidth and region availability, so they are also providing option to host static website. Right now i am going to explore how to host static website on S3 but in future articles i will share how to host video content on S3 for educational website.
**

Firstly login into your AWS Console https://aws.amazon.com/
Step#1

After login type S3 in the search and click on S3 Service.
Image description

Step#2
Click on Create Bucket Button.

Image description

Step#3

After clicking on Bucket you will see a very long tail form but you don't need to worry as all are simple options.

1-Firstly enter the full domain name under bucket name in my scenario i have www.mqtt.run domain name.

2-Select the region which is nearest to you, it will give awesome browsing experience to your users. i am choosing Singapore as it's near to my region.

3-if you want to Copy settings from existing bucket then choose the bucket else don't touch it.

Image description

4-Choose ACL Disabled Under "Object Ownership".

Image description

5- Uncheck the "Block all public access" Under "Block Public Access settings for this bucket" and click on checkbox "I acknowledge that the current settings might result in this bucket and the objects within becoming public."

Image description

6-Leave the other configurations as it is and click on "Create Bucket" Button

Image description

7-after that you will see your buckets like that. now click on bucket for further configuration.

Image description

8-Under Objects tabs click on Upload and upload your files and folders. don't upload zip file as there is no option to extract the zip files. you need to upload files and folder manually. the good thing is that just select the folder and it will take care of all files and folder inside the selected folder.

Image description

9-After that click on Properties tab and scroll down the page to bottom and find the section "Static website hosting" and click on Edit as we need to enable website feature on our S3 Bucket.

10-Select Enable under Static website hosting and Select "Host a static website" under Hosting type. provide Index document which is usually "index.html" or "default.html" but in my case it's "index.html".

11- After selecting all these options just save the changes.

Image description

12- After Changes you will see the URL of your static website.

Image description

But when you click on that url you will get the file permission error as because we didn't set any permission policy on our bucket.

13- Click on "Permission" Tab, Scroll the page and find "Bucket policy" and click on "Edit"

14-Copy the below Policy just write your bucket name instead of www.example.com and click on save changes.

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

Again open the URL and it will work fine Hurry Congrats. Next you need to point Your domain to S3 Bucket URL. Either you can point your domain cname records to S3 Static website URL or you can use AWS Route 53 to setup nameservers for your domain.

Well i am asuming that you don't have access to create cname records in your domain panel.

Step#4

1- Click on Top search bar and type Route 53 and click on Route 53 below from the suggestions.

Image description

2- Click on Hosted Zone.

Image description

3- Click on "Create Hosted Zone".

Image description

4-Enter Domain name and leave the other settings.

Image description

5- Click on "Create Hosted Zone"
Image description

After that you will get your zone listed.
6- Click on your Domain.

Image description

7- Copy Nameservers and set to domain in domain panel.

Image description

8- After setting your domain Nameservers click on "Create Record"
Here select CNAME under Record Type and Enter S3 Website URL under Value textarea and click on "Create Record" Button.

It will take some time to propagate the DNS Settings, later you can browse your website by directly type URL instead of long S3 URL.

Top comments (8)

Collapse
 
spaquet profile image
Stephane Paquet

There is a good follow up to your article: how to use AWS S3 + CloudFront for web site or web app distribution.
You can add load balancing on top to fully benefit from the edge and geography optimization.
The catch is content invalidation for the cache and version management (unlike on solutions such as Firebase hosting)

Collapse
 
mafzal9 profile image
Muhammad Afzal

Thank you Stephane, Yes i have plan for future articles where i will use CloudFront with AWS S3 as i am already using it with LightSail and happy with the results.

Collapse
 
vincenthwells profile image
donaldndelgado

I really searched for this information, but I couldn't find it, I was very happy to find it here, thank you very much.
basketball legends

Collapse
 
mafzal9 profile image
Muhammad Afzal

Thank you.

Collapse
 
pena profile image
Fernando Pena

Very good! Excellent article!

Collapse
 
mafzal9 profile image
Muhammad Afzal

Thank you.

Collapse
 
berviantoleo profile image
Bervianto Leo Pratama

Wonderful. Great article.

Collapse
 
mafzal9 profile image
Muhammad Afzal

Thank you.