Skip to content

Instantly share code, notes, and snippets.

@ravikyada
Created July 30, 2024 09:50
Show Gist options
  • Save ravikyada/28c5af6fda65aa211cb9c63c9c8e2056 to your computer and use it in GitHub Desktop.
Save ravikyada/28c5af6fda65aa211cb9c63c9c8e2056 to your computer and use it in GitHub Desktop.
Bash Script to Create S3 Bucket with Public Website Hosting

S3 Bucket Setup Script

This script automates the process of setting up an S3 bucket in AWS. It performs the following tasks:

  1. Checks if the specified S3 bucket already exists.
  2. Creates the bucket if it does not exist.
  3. Removes Block Public Access settings from the bucket.
  4. Attaches a specified bucket policy to allow public read access.
  5. Configures CORS settings for the bucket.
  6. Enables static website hosting on the bucket.

Usage

./s3_setup.sh --bucket-name BUCKET_NAME --region REGION --profile PROFILE
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 --bucket-name BUCKET_NAME --region REGION --profile PROFILE"
exit 1
}
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--bucket-name) BUCKET_NAME="$2"; shift ;;
--region) REGION="$2"; shift ;;
--profile) PROFILE="$2"; shift ;;
*) usage ;;
esac
shift
done
# Validate required arguments
if [ -z "$BUCKET_NAME" ] || [ -z "$REGION" ] || [ -z "$PROFILE" ]; then
usage
fi
# Check if the bucket already exists
if aws s3api head-bucket --bucket "$BUCKET_NAME" --profile $PROFILE 2>/dev/null; then
echo "Bucket already exists."
else
# If the bucket does not exist, create it
echo "Bucket does not exist. Creating bucket..."
aws s3api create-bucket --bucket $BUCKET_NAME --region $REGION --create-bucket-configuration LocationConstraint=$REGION --profile $PROFILE
echo "Bucket created successfully."
fi
# Remove Block Public Access settings
echo "Removing Block Public Access settings..."
aws s3api delete-public-access-block --bucket $BUCKET_NAME --profile $PROFILE
# Attach the specified bucket policy
POLICY='{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::'$BUCKET_NAME'",
"arn:aws:s3:::'$BUCKET_NAME'/*"
]
}
]
}'
aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy "$POLICY" --profile $PROFILE
# Configure CORS for the bucket
CORS='{
"CORSRules": [
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"PUT",
"POST",
"GET"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 0
}
]
}'
aws s3api put-bucket-cors --bucket $BUCKET_NAME --cors-configuration "$CORS" --profile $PROFILE
# Enable static website hosting
aws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document error.html --profile $PROFILE
echo "All configurations applied successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment