This article will show how to host static websites via S3 buckets by simply enabling the static hosting option and applying an efficient bucket policy to allow public access.
Prerequesites
Basic knowledge of Amazon S3 and Web hosting.
Firstly, we create the S3 bucket with a globally unique name in a specified region we want.
Under the Block Public Access section, we uncheck the box to enable Public access since we are going to be hosting a static website via the bucket.
We disable Versioning and optionally, we can add tags.
Disable Server-side encryption and Create the bucket.
You should get a message like the one below;
Click on the bucket name and go to the properties Tab.
Scroll down to Static website hosting and click on Edit.
Enable Static Web hosting & host a static website. Also input an index document name, typically Index.html and save the changes.
Next, go to the permissions tab of the bucket to input a bucket policy.
Copy and paste the below bucket policy, replacing your actual bucket name with what is listed as "Bucket-name" below and save changes.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::Bucket-Name/"
]
}
]
}
Next, it's about time to upload the necessary documents.
Click on Add Files, add your website files one after another, and upload.
Go back to the properties tab and scroll down to find your S3 endpoint which will be used to access the website.
Click on the endpoint and the static website is now accessible.
In the simplest of forms, this is an easy way to host a static website. More features like using CloudFront and Route 53 can be factored in eventually.
Top comments (0)