Hosting a Static Website in AWS S3
2024/05/24
Tags: Website Hosting AWS S3 Route 53
Introduction
A cheap and hassle free method for hosting a static website (one where the content doesn't change) uses S3 in AWS. The following goes over the steps for deploying such a website to S3.
In the following steps, replace mysite.com with your actual website name.
It's also assumed in this tutorial that you have already acquired the domain via Route 53 (another AWS service).
Create the S3 Buckets
- Create two buckets: mysite.com and www.mysite.com leaving all default values
- Upload website files to the mysite.com bucket
Enable Static Website Hosting
- Select the mysite.com bucket
- Select "Properties"
- Under "Static website hosting" click "Edit"
- "Enable" static website hosting
- Under "Index document" enter the filename of the index document, e.g. index.html
- "Save changes"
Allow Public Access
- Select the mysite.com bucket
- Select "Permissions"
- Under "Block public access (bucket settings)" choose "Edit"
- Clear the ticket "Block all public access"
- "Save changes"
Attach Public Read Access Policy
- Select the mysite.com bucket
- Select "Permissions"
- Under "Bucket Policy" choose "Edit"
- Paste the following in:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::mysite.com/*"
]
}
]
}
- "Save changes"
Test the Website Works
- Select the mysite.com bucket
- Select "Properties"
- Under "Static website hosting" click the link under "Bucket website endpoint"
- The website should load
Subdomain Bucket Redirect
- Select the www.mysite.com bucket
- Select "Properties"
- Under "Static website hosting" choose "Edit"
- Check "Enable"
- Check "Redirect requests for an object"
- In "Host name" enter mysite.com
- Set "Protocol" to "http"
- "Save changes"
Create Alias Records for the Root Domain and Subdomain
- Open Route 53
- Choose "Hosted Zones"
- Select the mysite.com domain
- Choose "Create Record"
- Choose "Simple routing" (may need to select "Switch to wizard" first)
- Click "Next"
- Choose "Define simple record"
- In "Value/Route traffic to", choose "Alias to S3 website endpoint"
- Choose the Region
- Choose the S3 bucket - mysite.com
- Set "Evaluate target health" to "No"
- Click "Define simple record"
Repeat the above steps for the subdomain. This is the same except when defining the simple record enter www under "Record name" for the subdomain and choose the S3 bucket for www.mysite.com
The End
You should now be able to access your website by visiting either mysite.com or www.mysite.com