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
aws s3 sync: Sync a Directory to S3 in CIaws s3 sync uploads only changed files to S3, making it ideal for static site deploys in CI. Learn the delete…
aws s3 cp: Copy Files to S3 in CIaws s3 cp copies files and directories to and from S3. Learn the recursive, exclude, and ACL flags plus a CI…
aws cloudformation deploy: Deploy a Stack in CIaws cloudformation deploy creates or updates a stack via change sets. Learn the parameter-overrides and capab…