Created
April 28, 2025 10:21
-
-
Save Loupax/83a47d59161d3f5eb66ac5d9acef8bba to your computer and use it in GitHub Desktop.
Cloudfront mass invalidations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# --- Configuration --- | |
DISTRIBUTION_ID="DISTRO_ID" # Replace with your actual Distribution ID | |
URL_FILE="paths" # Your file containing URL paths | |
BATCH_SIZE=15 # Max paths per invalidation request | |
ALL_PATHS=() | |
# Check if the URL file exists | |
if [[ ! -f "$URL_FILE" ]]; then | |
echo "Error: URL file '$URL_FILE' not found." | |
exit 1 | |
fi | |
echo "Reading URLs and extracting paths from '$URL_FILE'..." | |
while IFS= read -r url || [[ -n "$url" ]]; do | |
# Basic path extraction (adjust regex if needed for your URL structure) | |
path=$(echo "$url") | |
# Add the formatted path, quoted for safety in JSON later | |
ALL_PATHS+=("\"$path\"") | |
done < "$URL_FILE" | |
total_paths=${#ALL_PATHS[@]} | |
echo "Found $total_paths paths to invalidate." | |
if [ $total_paths -eq 0 ]; then | |
echo "No valid paths extracted. Exiting." | |
exit 1 | |
fi | |
batch_num=1 | |
for (( i=0; i<total_paths; i+=BATCH_SIZE )); do | |
# Get the slice of paths for the current batch | |
current_batch_paths=("${ALL_PATHS[@]:i:BATCH_SIZE}") | |
batch_quantity=${#current_batch_paths[@]} | |
if [ $batch_quantity -eq 0 ]; then | |
continue # Skip if somehow an empty batch is created | |
fi | |
echo "--- Preparing Batch $batch_num (Processing paths $((i+1)) - $((i+batch_quantity)) of $total_paths) ---" | |
# Join the paths with commas for the JSON array items | |
batch_items_string=$(IFS=,; echo "${current_batch_paths[*]}") | |
# Generate a unique caller reference for THIS batch | |
CALLER_REFERENCE=$(date +%Y%m%d%H%M%S)-batch${batch_num}-${RANDOM} | |
# Construct the JSON payload for this batch | |
invalidation_json=$(cat <<EOF | |
{ | |
"Paths": { | |
"Quantity": $batch_quantity, | |
"Items": [ $batch_items_string ] | |
}, | |
"CallerReference": "$CALLER_REFERENCE" | |
} | |
EOF | |
) | |
echo "Submitting invalidation request for Batch $batch_num..." | |
echo "Caller Reference: $CALLER_REFERENCE" | |
# echo "Payload: $invalidation_json" # Uncomment to see the generated JSON | |
# Execute the AWS CLI command for the current batch | |
aws cloudfront create-invalidation \ | |
--distribution-id "$DISTRIBUTION_ID" \ | |
--invalidation-batch "$invalidation_json" | |
# Check the exit status of the AWS command | |
if [ $? -eq 0 ]; then | |
echo "Batch $batch_num submitted successfully." | |
else | |
echo "Error submitting Batch $batch_num. Check AWS CLI output/logs." | |
# Consider adding logic here: stop script? log error and continue? | |
# exit 1 # Uncomment to stop the script if any batch fails | |
fi | |
# If it's not the last batch, wait for user confirmation instead of sleeping | |
if (( i + BATCH_SIZE < total_paths )); then | |
#read -p "-> Press Enter to submit the next batch..." | |
sleep 20s | |
fi | |
((batch_num++)) | |
done | |
echo "--- All batches submitted. Monitor invalidation status in the AWS Console. ---" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment