Skip to content

Commit

Permalink
s3 setup and s3 set cahce control scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
imoisharma authored Mar 18, 2021
1 parent 63aaf8b commit 4119877
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
111 changes: 111 additions & 0 deletions scripts/s3-set-cache-control-max-age.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env bash

# Set Cache-Control public with max-age value on AWS S3 bucket website assets for all filetypes
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

BUCKET="YOUR-S3-BUCKET-NAME"
MAXAGE="SET-SECONDS-VALUE-HERE"

# Functions

function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}

function completed(){
echo
horizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
horizontalRule
echo
}

function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}

function horizontalRule(){
echo "====================================================="
}

function message(){
echo
horizontalRule
echo "$*"
horizontalRule
echo
}

function pause(){
read -n 1 -s -p "Press any key to continue..."
echo
}


# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/credentials; then
if ! grep -q aws_access_key_id ~/.aws/config; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi

check_command "aws"

# Check for AWS CLI profile argument passed into the script
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-multiple-profiles
if [ $# -eq 0 ]; then
scriptname=`basename "$0"`
echo "Usage: ./$scriptname profile"
echo "Where profile is the AWS CLI profile name"
echo "Using default profile"
profile=default
else
profile=$1
fi

message "This script will set Cache-Control public with max-age value on AWS S3 bucket website assets."
echo
# pause

# Ensure Variables are set
if [ "$BUCKET" = "YOUR-S3-BUCKET-NAME" ]; then
read -r -p "Enter the S3 bucket name: " BUCKET
if [ -z "$BUCKET" ]; then
fail "Failed to set variables!"
fi
fi
if [ "$MAXAGE" = "SET-SECONDS-VALUE-HERE" ]; then
read -r -p "Enter the new Cache-Control Max-Age value in seconds: " MAXAGE
if [ -z "$MAXAGE" ]; then
fail "Failed to set variables!"
fi
fi

# Validate max-age range 0-31536000
if ! [ "$MAXAGE" -ge "0" ] || ! [ "$MAXAGE" -le "31536000" ]; then
fail "Invalid Cache-Control Max-Age value: $MAXAGE"
fi

# Determine the bucket region
REGION=$(aws s3api get-bucket-location --bucket $BUCKET --output text --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$REGION"
fi
if echo $REGION | grep -q "None"; then
REGION="us-east-1"
fi

message "Setting Cache-Control Max-Age $MAXAGE for all assets in S3 Bucket $BUCKET"
set=$(aws s3 cp --recursive --profile $profile --region $REGION s3://$BUCKET/ s3://$BUCKET/ --cache-control "public, max-age=$MAXAGE" --metadata-directive "REPLACE" 2>&1)
if [ ! $? -eq 0 ]; then
fail "$set"
fi
if echo $set | egrep -iq "error"; then
fail "$set"
else
echo "$set"
fi

completed
61 changes: 61 additions & 0 deletions scripts/s3-setup-buckets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash
# This script will create S3 buckets, set CORS config and tag bucket with client name
# Requires awscli

# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
echo "AWS config not found or CLI not installed. Please run \"aws configure\"."
exit 1
fi
fi

read -r -p "Enter the client name: " CLIENT

function createbucket(){
aws s3api create-bucket --bucket $CLIENT-$ENV
}

function setcors(){
aws s3api put-bucket-cors --bucket $CLIENT-$ENV --cli-input-json \
'{
"CORSConfiguration": {
"CORSRules": [
{
"AllowedOrigins": ["*"],
"AllowedMethods": ["GET"],
"MaxAgeSeconds": 3000,
"AllowedHeaders": ["*"]
}
]
}
}'
}

function tag(){
aws s3api put-bucket-tagging --bucket $CLIENT-$ENV --tagging \
'{
"TagSet": [
{
"Key": "Client",
"Value": "'$CLIENT'"
}
]
}'
}

echo "Creating Buckets, setting CORS Configuration, creating Tags..."
ENV=dev
createbucket
setcors
tag
ENV=production
createbucket
setcors
tag
ENV=staging
createbucket
setcors
tag
echo "Completed!"

0 comments on commit 4119877

Please sign in to comment.