Skip to content

Instantly share code, notes, and snippets.

@everdrone
Last active April 5, 2019 08:59
Show Gist options
  • Save everdrone/c0037488c88faf6d9740f543153a4111 to your computer and use it in GitHub Desktop.
Save everdrone/c0037488c88faf6d9740f543153a4111 to your computer and use it in GitHub Desktop.
Creates a AWS S3 static website bucket

Usage

./create-s3-website.sh -n my.test.bucket -r eu-central-1 -d build

By default, the -e <error-document option is index.html, unless specified

#!/usr/bin/env bash
usage() { echo "Usage: $0 -n <bucket> -r <region> -d <directory> [-e <error-document>]" 1>&2; exit 1; }
while getopts ":n:r:d:" o; do
case "${o}" in
n)
n=${OPTARG}
;;
r)
r=${OPTARG}
;;
d)
d=${OPTARG}
;;
e)
e=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${n}" ] || [ -z "${r}" ] || [ -z "${d}" ]; then
usage
fi
uri=s3://"${n}"
policy={\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"PublicReadGetObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":[\"arn:aws:s3:::${n}/*\"]}]}
aws s3 mb $uri --region ${r}
if [ -z "${e}" ]; then
aws s3 website ${uri} --index-document index.html --error-document index.html
else
aws s3 website ${uri} --index-document index.html --error-document ${e}
fi
aws s3api put-bucket-policy --bucket ${n} --policy ${policy}
aws s3 sync ${d} ${uri}
echo -e "\nhttp://${n}.s3-website.${r}.amazonaws.com\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment