S3 Static Website
We are going to host a static website in an S3 bucket. We will:
- Create the S3 Bucket
- Enable website hosting on the bucket
- Add a dns record to point to the bucket
Static Site Requirements
You can use any static site you want for this, it just has to be valid HTML and optionally css, javascript, images, and other assets.
If you wan't, you can use this qr code generator as your static site. Just download the dist.zip folder and unzip it. It contains the index.html
file and other assets. Feel free to clone the react code and play around with the app if you like.
The end result will be something like this: https://qr-spa.cloudcourse.dev/ A static site with no server or backend.
Setting up The Bucket
Your S3 dashboard should look like this
The bucket is now setup with some files in it. If anyone tried to access these files, they would only be able to view or download the actual files, not view them as a static website. We need to change a few settings in S3 to tell S3 to serve these files as a website over HTTP.
Enable Website Hosting
You should see a 403 Forbidden page like this. Everything in AWS will always be restricted unless we specifically specify how something can be accesses. Even though we've set this up as a website, we haven't told S3 who is allowed to access the files in the Bucket, so we'll do that next.
Get used to this principle of 'least privilege' in AWS and other cloud services.
Policies are written in JSON and specify rules for how things can be accessed or denied access. We're going to setup a policy right now that allows anyone to access the files in the S3 bucket. You usually don't want to do this unless your bucket contains public files, like a public website.
Make sure you specify the bucket name as the ARN. The /*
at the end represents all objects within the bucket. So any rules we're creating here will only apply to all the objects in that specific bucket.
The resulting JSON should look like this
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Principal": "*",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::qr-codes-1.cloudcourse.dev/*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Principal": "*",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::qr-codes-1.cloudcourse.dev/*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Principal": "*",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::qr-codes-1.cloudcourse.dev/*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Principal": "*",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::qr-codes-1.cloudcourse.dev/*"
]
}
]
}
Now if you visit the s3 website domain again, you should be able to access the site. Now we just need to use a custom domain instead of the default S3 one.
Custom Domain
You should now be able to visit your site using your domain name (the name of the S3 bucket). Sometimes it can take a few minutes or hours to work though, especially if your browser previously cached the domain.
In order to get this working with HTTPS, we need to connect this S3 bucket to cloudfront.