# Shell script to invalidate AWS cache

I made this script to invalidate AWS cache. The purpose of this script is to retry the command multiple times because we make a lot of invalidations and we often reach the number of concurrent invalidations AWS supports. The script will most of the time be called by other scripts but we keep detailed log files thus it outputs a lot to the console.

I have not used shell in a long time and I am interrested in general good practices as well as optimizations specifict to this script especially in the use of set -x, declare and local.

invalidate-cache.sh

#!/bin/sh

set -e
set -x

function usage() {
local -r NAME="$0" echo "usage:$NAME repo-creds distribution-id path"
echo "  repo-creds         the adress of the machine to ssh to"
echo "  distribution-id    the AWS distribution id"
echo "  paths              the paths to the cache files to invalidate"
exit 1
}

if (($# < 3)); then usage "$0"
elif (($# > 3)); then echo "$0 takes 3 parameter, $# were given, excess will be ignored" fi declare -r REPO_CREDS="$1"
declare -r ID="$2" declare -r PATHS="$3"

declare -i ITERATIONS=1
until ssh "$REPO_CREDS" aws cloudfront create-invalidation --distribution-id "$ID" --paths "$PATHS"; do if [${ITERATIONS} -eq 10]; then
exit
fi
echo "couldn't invalidate cache, AWS quota might have been reached, retrying in 1 minute..."
sleep 1m
ITERATIONS+=1
done
exit 0