Skip to content
Latchkey

aws cloudfront create-invalidation: Purge CDN in CI

Invalidate cached objects so a CloudFront distribution serves fresh content.

aws cloudfront create-invalidation tells a CloudFront distribution to refetch listed paths from the origin. In CI it runs right after an s3 sync deploy so users see the new build instead of stale cached assets.

Common flags

  • --distribution-id - the CloudFront distribution to purge (required)
  • --paths "/*" - paths to invalidate (wildcards allowed; /* purges everything)
  • --query "Invalidation.Id" - capture the invalidation ID to poll

Example in CI

Purge the whole distribution after a static deploy.

shell
aws cloudfront create-invalidation --distribution-id ${CF_DIST_ID} --paths "/*"

Common errors in CI

  • An error occurred (NoSuchDistribution) - wrong distribution ID
  • An error occurred (AccessDenied) - role lacks cloudfront:CreateInvalidation
  • An error occurred (TooManyInvalidationsInProgress) - throttle; batch paths instead of /*
  • Invalidations beyond the free 1000/month tier incur charges - scope paths narrowly

Key takeaways

  • create-invalidation purges cached paths so CloudFront serves the new build.
  • Run it after s3 sync; /* is simplest but only the first 1000 paths/month are free.
  • It returns immediately - poll the invalidation ID to confirm completion.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →