- Newest
- Most votes
- Most comments
The behavior you're experiencing with inconsistent CORS headers across different CloudFront edge locations is not uncommon and can be considered expected behavior in certain circumstances. This situation arises due to several factors related to how CloudFront handles caching and propagation of changes:
-
Propagation Delays: When you make changes to your S3 bucket CORS settings or CloudFront distribution settings, it can take some time for these changes to propagate across all edge locations. This means that some edge locations may still be serving older versions of your content or configurations.
-
Caching Behavior: CloudFront's caching mechanism is designed to optimize content delivery, which can sometimes lead to inconsistencies across edge locations. Even after a cache invalidation, it's possible that some edge locations may not immediately reflect the changes.
-
Invalidation Timing: While you performed a cache invalidation using /*, it's important to note that invalidations are not instantaneous across all edge locations. There can be a delay between when the invalidation is initiated and when it takes effect at each edge location.
To address these issues and minimize regional caching inconsistencies, consider the following best practices:
-
Use versioning in your file names (e.g., schema-v1.json, schema-v2.json) to force cache busting when you make updates. This ensures that new versions are treated as entirely new objects by CloudFront.
-
Implement more frequent or strategic cache invalidations. You might want to set up automated invalidations after each deployment or content update.
-
Consider using a CloudFront Function or Lambda@Edge to add CORS headers directly at the edge. This can ensure consistent CORS behavior regardless of what's cached.
-
Monitor CloudFront distribution metrics and logs to identify patterns in cache hit/miss rates and potential inconsistencies across edge locations.
-
For critical updates, you may temporarily switch to the "CachingDisabled" policy in CloudFront to ensure all requests go back to the origin, though this should be used sparingly due to performance implications.
-
Ensure that your CloudFront distribution is configured to forward the Origin header to S3, allowing S3 to send appropriate CORS headers based on the request origin.
While this behavior can be frustrating, it's generally not considered a bug but rather a consequence of CloudFront's distributed nature and caching mechanisms. By implementing these best practices, you can minimize inconsistencies and provide a more uniform experience across different geographical locations.
Sources
CloudFront Cache Issue | AWS re:Post
Static JS files served from S3 via CloudFront lead to intermittent CORS issues | AWS re:Post
CloudFront will miss cache if request is from CURL | AWS re:Post
answered a year ago
Relevant content
asked 2 years ago

I want to point out that enabling CORS and invalidating the first time happened ~1 month ago after my colleague tried to access it. Therefore, I don't think it's a delay / timing issue.