Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulswail/0ffbaf56cfc59d79238cc10084925ec4 to your computer and use it in GitHub Desktop.
Save paulswail/0ffbaf56cfc59d79238cc10084925ec4 to your computer and use it in GitHub Desktop.
Invalidate CloudFront Distribution cache using only its origin domain name (AWS CLI)
#!/bin/bash
CLOUDFRONT_DOMAIN="mydomain.com" # change this
# Get Distribution ID (requires node.js to be installed)
DISTRIBUTION_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[].{Id: Id, DomainName: DomainName, OriginDomainName: Origins.Items[0].DomainName }[?contains(OriginDomainName, '$CLOUDFRONT_DOMAIN')] | [0]" | node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).Id")
# Invalidate cache
echo "Invalidating CloudFront distribution $DISTRIBUTION_ID ..."
aws cloudfront create-invalidation --distribution-id=$DISTRIBUTION_ID --paths '/*'
echo "Invalidation requested."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment