Static Site Deployment with S3 Sync and CloudFront Invalidation
Deploy a static website to S3 with optimized cache headers and trigger a CloudFront invalidation for instant global propagation.
Prerequisites
- -AWS CLI v2 installed and configured
- -S3 bucket with static website hosting enabled
Steps
Build your static site
Generate the production build output that will be uploaded to S3.
Most frameworks output to a dist/ or build/ directory. Check your framework docs for the exact output path.
Sync build output to S3
Upload new and changed files to the S3 bucket and remove files that no longer exist in the local build directory.
The --delete flag removes files from S3 that are not in your local directory. Make sure you are syncing the correct folder.
Set cache headers for hashed assets
Apply long-lived cache-control headers to fingerprinted assets like JS, CSS, and images so browsers cache them aggressively.
Only apply immutable caching to files with content hashes in their filenames. HTML files should have short or no cache TTL.
Set short cache on HTML files
Ensure HTML entry points are revalidated frequently so users always get the latest version that references the correct hashed assets.
s-maxage controls the CDN cache TTL separately from the browser cache TTL set by max-age.
Invalidate CloudFront cache
Create a CloudFront invalidation to purge cached content at edge locations worldwide so visitors see the latest deployment.
Wildcard invalidations count as a single invalidation path but still take 5-15 minutes to propagate across all edge locations.
Verify the deployment
Check the invalidation status and confirm the deployment went through successfully.
You can also run 'curl -I https://your-domain.com' to inspect the response headers and verify cache-control values.
Full Script
FAQ
Discussion
Loading comments...