Create Public S3 Bucket

S3 static website hosting is the easiest way to host websites on AWS.

First, let’s create the S3 bucket. S3 has certain rules in place when creating a bucket name, and names must be globally unique. In the example output, you’ll see a random string of numbers appended to the bucket name to provide this uniqueness.:

aws s3 mb s3://your-bucket-name

Now we need to specify the names of our --index-document and --error-document for static website hosting. Hugo generates a 404.html error file instead of the more traditional error.html error file.

aws s3 website s3://your-bucket-name --index-document index.html --error-document 404.html

We then need to set bucket policy permissions. The following statement allows public read access from any principle on the specified bucket.

aws s3api put-bucket-policy --bucket your-bucket-name --policy '{"Version":"2012-10-17","Statement":[{"Sid":"PublicReadGetObject","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::your-bucket-name/*"}]}'

A more easily viewable form of the bucket policy can be seen within the console. Notice that your bucket is now public.

Before executing these commands, remember to use a globally unique S3 bucket name. It is also worth mentioning that if you plan on using TLS encryption and a CloudFront distribution, your S3 bucket name(s) should be the name of your domain and/or root domain (more on this in a later post).

Make S3 Bucket

Public Folder

The hugo command is used to generate the public folder, which contains a collection of files and folders that will ultimately be hosted on S3.

You can remove this public directory if you prefer to start from a clean slate.

sudo rm -R public/
hugo

Copy these files from your public folder to your S3 bucket:

aws s3 cp public s3://your-bucket-name/ --recursive

The sync command works as well, but sync only copies the differences.

aws s3 sync public s3://your-bucket-name/

Sometimes it may be necessary to clear out your S3 bucket.

aws s3 rm s3://your-bucket-name --recursive

At this point, you can gather the URL of your S3 website at the bottom of the properties tab of your S3 bucket for testing.

That’s it! You’ve successfully launched an HTTP site using S3 static website hosting. Continue on to see how to create a TLS certificate and CloudFront cache for your static website.