Shell script: sync your static website to S3 and invalidate cloudfront until completed

This is a typical deploy.sh :

(Mostly copied from https://stuff-things.net/2016/07/20/invalidate-cloudfront-and-wait/ )

#!/bin/sh

# Invalidate cloudfront 
invalidate_cf()
{
    distribution_id=$1
    aws cloudfront create-invalidation --distribution-id $distribution_id --path '/*'
}

invalidate_cf_and_wait()
{
    distribution_id=$1
    id=$(invalidate_cf $distribution_id | grep Id | awk -F'"' '{ print $4}' )
    echo "Waiting for invalidation $id "
    aws cloudfront wait invalidation-completed --id $id --distribution-id $distribution_id
    echo "Invalidation $id completed"
}

echo "Starting deployment"
s3_bucket="$1"
distribution_id="$2"
echo "Starting s3 sync"
aws s3 sync build/  $s3_bucket --delete
echo "Starting cloudfront invalidation"
invalidate_cf_and_wait $distribution_id
echo "Finished deployment"

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.