Skip to content

Commit

Permalink
cache flush script
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Dec 23, 2024
1 parent 157b452 commit 6c2980f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,5 @@ To learn React, check out the [React documentation](https://reactjs.org/).

```
./scripts/deploy-s3.sh
./scripts/flush-cache-prod.sh DIST.cloudfront.net
```
83 changes: 83 additions & 0 deletions frontend/scripts/flush-cache-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
#
# Name: flush-cache-prod.sh
#
# Usage: ./flush-cache-prod.sh "<distribution-domain-name>"
#
# Description:
# 1. Looks up a CloudFront distribution by matching the input domain name
# (e.g., "d123abcxyz.cloudfront.net") to the distribution's DomainName field.
# 2. Issues a cache invalidation for all files ("/*").
# 3. Monitors the invalidation until it is completed.
#
# Requirements:
# - AWS CLI installed and configured
# - jq installed (for JSON parsing)
# - A CloudFront distribution whose DomainName matches the supplied input

set -euo pipefail

if [ "$#" -ne 1 ]; then
echo "Usage: $0 \"<distribution-domain-name>\""
echo "Example: $0 \"d123abcxyz.cloudfront.net\""
exit 1
fi

DISTRIBUTION_DOMAIN="$1"

echo "Looking up the distribution by domain name: \"$DISTRIBUTION_DOMAIN\" ..."

# Step 1: Query the distribution list, matching the DomainName to DISTRIBUTION_DOMAIN
DISTRIBUTION_ID=$(
aws cloudfront list-distributions --output json \
| jq -r --arg DOMAIN "$DISTRIBUTION_DOMAIN" '
.DistributionList.Items[]
| select(.DomainName == $DOMAIN)
| .Id
'
)

if [ -z "$DISTRIBUTION_ID" ]; then
echo "Error: No distribution found with domain name: \"$DISTRIBUTION_DOMAIN\""
exit 1
fi

echo "Found distribution ID: $DISTRIBUTION_ID"

# Step 2: Issue a cache invalidation for all files
echo "Creating invalidation for all paths (/*) ..."
INVALIDATION_JSON=$(
aws cloudfront create-invalidation \
--distribution-id "$DISTRIBUTION_ID" \
--paths "/*"
)

# Extract the invalidation ID from the result
INVALIDATION_ID=$(echo "$INVALIDATION_JSON" | jq -r '.Invalidation.Id')

echo "Invalidation created. ID: $INVALIDATION_ID"

# Step 3: Monitor the invalidation until it completes
echo "Monitoring invalidation status..."

while true; do
STATUS=$(
aws cloudfront get-invalidation \
--distribution-id "$DISTRIBUTION_ID" \
--id "$INVALIDATION_ID" \
--output json \
| jq -r '.Invalidation.Status'
)

echo "Current status: $STATUS"

if [ "$STATUS" == "Completed" ]; then
echo "Invalidation $INVALIDATION_ID has completed!"
break
fi

# Sleep for a few seconds before checking again (avoid spamming AWS)
sleep 5
done

echo "Cache flush complete."

0 comments on commit 6c2980f

Please sign in to comment.