Problem: I have a personal static web app that I need to sync to my AWS S3 bucket and then invalidate the index.html
on CloudFront (CDN).
Solution: local bash script that does just that.
#!/bin/bash
# Deps: awscli, jq
BUCKET_NAME="your-s3-bucket"
SOURCE_DIR="local/dir/to/sync"
DISTRBUTION_ID="id-from-cloudfront"
aws s3 sync "$SOURCE_DIR" "s3://${BUCKET_NAME}"
# invalidate all: "/*"; invalidate specific file: "/index.html"
INVALIDATION=$(aws cloudfront create-invalidation --distribution-id "$DISTRBUTION_ID" --paths "/index.html")
INVALIDATION_ID=$(echo "$INVALIDATION" | jq -r .Invalidation.Id)
echo "CloudFront Invalidation: $INVALIDATION_ID"
echo " clearing..."
aws cloudfront wait invalidation-completed --distribution-id "$DISTRBUTION_ID" --id "$INVALIDATION_ID"
echo " completed"