Publishing a static site to AWS S3

I am in the process of starting to use Hugo to build my personal website. The Hugo site has great documentation for how to build the site but I wasn’t finding simple documentation for how to publish the site usin AWS S3. This post will document the solution I use for publishing this site to S3.

The first step is to setup a S3 bucket to host your site. The AWS site has great instructions for setting up a S3 bucket to host a site.. Then add a IAM user with access keys and no password which has full access to the S3 bucket you are deploying your site from. Once that is done you can use the following code to upload your site to S3.

#! /bin/bash

export AWS_ACCESS_KEY_ID=ACCESS_KEY_VALUE
export AWS_SECRET_ACCESS_KEY=SECRET_KEY_VALUE
export AWS_DEFAULT_REGION=region

aws s3 sync public/ s3://bucketname.com --delete --cache-control "max-age=600"

You will need to replace the access key values, region, and bucket name in the above script and then run the scrip from the root of the hugo site you are working in after you have run the hugo command to build the site.

Note that this would work for publishing any local directory with a static site to an S3 bucket.