Skip to content

Instantly share code, notes, and snippets.

@andycochrane
Last active September 17, 2017 14:36
Show Gist options
  • Save andycochrane/aeeaf7972bc11a6f013a84dca41661ed to your computer and use it in GitHub Desktop.
Save andycochrane/aeeaf7972bc11a6f013a84dca41661ed to your computer and use it in GitHub Desktop.
Create a new s3 bucket and configure it to host a static website using the aws cli. (creates a JSON file in same directory)
#!/bin/sh
# Check for bucket name argument
if [ ! -z "$1" ]; then
# Assign first argument to BUCKET variable
BUCKET=$1;
# Assign second argument to REGION variable or fallback to EU (Ireland) region if not provided.
REGION=${2:-"eu-west-1"}
# Save bucket policy JSON (with bucket name argument value) to a file named bucket-policy.json
echo "{
\"Version\": \"2012-10-17\",
\"Statement\": [
{
\"Sid\": \"PublicReadGetObject\",
\"Effect\": \"Allow\",
\"Principal\": \"*\",
\"Action\": \"s3:GetObject\",
\"Resource\": \"arn:aws:s3:::$BUCKET/*\"
}
]
}" > bucket-policy.json && \
# Create the bucket
aws s3api create-bucket --bucket "$BUCKET" --region "$REGION" --create-bucket-configuration LocationConstraint="$REGION" && \
# Add the bucket policy from the bucket-policy.json file
aws s3api put-bucket-policy --bucket "$BUCKET" --region "$REGION" --policy file://bucket-policy.json && \
# Configure bucket to host a static website
aws s3 website s3://"$BUCKET"/ --region "$REGION" --index-document index.html
else
printf "Please provide a bucket name\n";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment