AWS Cloudfront invalidation script

2 min. read


It’s very common to see standard invalidation script for AWS Cloudfront / CDN, where invalidation is executed and we just need to wait until the process is finished. In my case, the invalidation is part of the deployment pipeline and it’s quite important to know the status of the invalidation and when to proceed to the next stages.

In the script there is a combination of two aws commands aws cloudfront create-invalidation and aws cloudfront get-invalidation. The first, to trigger invalidation and the second to get current status of it. Please, take a look:

SCRIPT_HEADSIZE=$(head -200 ${0} |grep -n "^# END_OF_HEADER" | cut -f1 -d:)
SCRIPT_NAME="$(basename ${0})"

run_help() { 
  head -${SCRIPT_HEADSIZE:-99} ${0} | grep -e "^#[%+-]" | sed -e "s/^#[%+-]//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g" ; 
}

AWS=aws
HELP=0
INVALIDATION_PATH='/*'

cecho(){
    RED="\033[0;31m"
    GREEN='\033[0;32m'
    YELLOW='\033[1;33m'
    NC='\033[0m' # No Color
    printf "${!1}${2} ${NC}\n"
}

for i in "$@"
do
case $i in
    --distributionId)
    shift
      if [ -z "$1" ]
      then
        cecho "RED" "Distribution ID cannot be empty"
        exit 1
      else
        DISTRIBUTION_ID="$1"
      fi
    shift
    ;;
    --path)
    shift
      INVALIDATION_PATH="$1"
    shift
    ;;
    --help)
    HELP=1
    run_help
    shift
    ;;
    -*)
    cecho "RED" "Error: Unknown option: $1" >&2
    run_help
    exit 1
esac
done

get_invalidation_status(){
  invalidation=$1
  distribution=$2
  status=$(
    ${AWS} cloudfront \
    get-invalidation --id "${invalidation}" \
    --distribution-id "${distribution}" \
    --query Invalidation.Status \
    --output text
  )
  echo "${status}"
}

run_command () {
  
  cecho "GREEN" "Running cdn invalidation for Distribution ID: ${DISTRIBUTION_ID}"

  INVALIDATION_ID=$( ${AWS} cloudfront create-invalidation \
    --distribution-id "${DISTRIBUTION_ID}" \
    --paths ${INVALIDATION_PATH} \
    --region us-west-2 \
    --query Invalidation.Id \
    --output text
  )


  while true; do

    STATUS=$(get_invalidation_status "${INVALIDATION_ID}" "${DISTRIBUTION_ID}")
    DATETIME=$(date +%H:%M:%S)
    echo "-------------------------"
    echo "Distribution: ${DISTRIBUTION_ID}"
    echo "Invalidation: ${INVALIDATION_ID}"
    echo "Status: ${STATUS}"
    echo "Time:" ${DATETIME}
    echo "-------------------------"

    if [ "${STATUS}" == "Completed" ];
    then
      cecho "GREEN" "Completed !"
      break
    fi
    sleep 10
  done
}

if [ "${HELP}" != 1 ];
then
  if [ -z "${DISTRIBUTION_ID}" ];
  then
    cecho "RED" "Distribution Id is missing !"
    exit 1
  else
    run_command
  fi
fi

You can add some feedback to this gist at Github

Don’t just copy & paste

Do not forget to change or modify additional parameters of the script, f.e. aws region, timeout value or others, to make it use in your project!

Interesting note

After a quiet period, I decided to create short post at my blog, thanks to Hugoconf 2022 and a very interesting tech talk about Front Matter VSCode extension, which I use to write this article.

9. July 2022
Posted in Gists
Tomas
Tomas

Software developer, lives in Zilina, Slovakia. Fan of modern web technologies, digitalization, cloud and education. Also co-owner of a local coffee brand - Kava Doppio