Cache Invalidation After Deploy

CloudFront caches aggressively — invalidate paths after a deploy so users see the new version.

CloudFront caches your files at the edge for speed, but that means after you upload a new version, users may keep getting the old cached copy. You fix this with an invalidation, which tells CloudFront to forget cached paths and re-fetch from S3.

Two strategies

  • Invalidate everything (/*) after each deploy — simple, and the first 1,000 paths per month are free.
  • Content hashing — build tools rename files to app.4f3a.js per build, so new files have new names and never collide. Only index.html needs invalidating.

Content hashing is the professional approach: it lets you cache assets for a year while still shipping updates instantly.

Example

Example · bash
# After uploading a new build, bust the cache
aws s3 sync ./dist s3://myapp-prod-web-2026 --delete

aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE123 \
  --paths "/*"

When to use it

  • A team runs a CloudFront invalidation after every CI deploy so users see the updated JavaScript bundle instead of a cached old version.
  • An on-call engineer invalidates a specific path after a hotfix so only the affected page is purged without flushing the entire cache.
  • A deploy script creates an invalidation for index.html after each release because it is served with no-cache but CloudFront still needs to re-fetch it.

More examples

Invalidate all cached paths

Purges every cached object in the distribution — useful after a full site redeploy.

Example · bash
aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE \
  --paths '/*'

Invalidate specific paths only

Invalidates only the changed files so the rest of the cache remains warm, reducing origin load.

Example · bash
aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE \
  --paths '/index.html' '/app.js' '/manifest.json'

Wait for invalidation to complete

Creates an invalidation and blocks the script until CloudFront confirms the purge is done before proceeding.

Example · bash
INVAL_ID=$(aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE \
  --paths '/*' \
  --query 'Invalidation.Id' \
  --output text)

aws cloudfront wait invalidation-completed \
  --distribution-id E1EXAMPLE \
  --id $INVAL_ID
echo "Invalidation complete"

Discussion

  • Be the first to comment on this lesson.
Cache Invalidation After Deploy — AWS Deployment | SoundsCode