Skip to content

Instantly share code, notes, and snippets.

@mgeeky
Last active March 23, 2021 21:48
Show Gist options
  • Save mgeeky/cebe7a05557569008c892e2130ec1ec9 to your computer and use it in GitHub Desktop.
Save mgeeky/cebe7a05557569008c892e2130ec1ec9 to your computer and use it in GitHub Desktop.
Utterly simple approach to create an AWS S3 static website via AWS CLI.
#!/bin/bash
if [ $# -ne 2 ] ; then
echo "Usage: ./create-s3-website.sh <s3-bucket-name> <website-local-dir>"
exit 1
fi
S3_BUCKET_NAME=$1
WEBSITE_DIR=$2
INDEX_DOCUMENT=index.html
REGION=$(aws configure get region)
aws s3 mb s3://$S3_BUCKET_NAME || { echo '[!] Could not create S3 bucket.' ; exit 1; }
aws s3 sync $WEBSITE_DIR s3://$S3_BUCKET_NAME/ || { echo '[!] Could not create S3 bucket.' ; exit 1; }
aws s3 mb s3://$S3_BUCKET_NAME || { echo '[!] Could not create S3 bucket.' ; exit 1; }
cat <<EOT > /tmp/.s3_bucket_policy
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::$S3_BUCKET_NAME/*"]
}
]
}
EOT
aws s3api put-bucket-policy --bucket $S3_BUCKET_NAME --policy file:///tmp/.s3_bucket_policy || { echo '[!] Could not put bucket policy.' ; rm /tmp/.s3_bucket_policy ; exit 1; }
rm /tmp/.s3_bucket_policy
aws s3 website s3://$S3_BUCKET_NAME --index-document $INDEX_DOCUMENT || { echo '[!] Could not create S3 static website.' ; exit 1; }
echo
echo "[+] S3 static website created. URL:"
echo -e "\thttp://$S3_BUCKET_NAME.s3-website-$REGION.amazonaws.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment