Amazon CloudFront – PHP Invalidator

Amazon recently (finally!) added support for invalidation to CloudFront. Frankly, without this feature the service was pretty useless to me since updating (or deleting) the cached resource would mean that the actual operation would take up to 24 hours to be reflected on the CDN. But all is well now as the invalidation API available and is, as expected, very responsive. (Based on initial tests, invalidations take a few minutes to process.)

What was missing though is a PHP API for easily creating and processing invalidation requests, so I put one together, largely inspired by the existing S3 and CloudFront PHP kits.

To invalidate an object, just create an instance of CloudFront and call invalidate() it. Like so:

A couple of things to keep in mind. CloudFront supports very few concurrent invalidation requests so make batch requests as much as possible. If you absolutely need to up this limit, go to http://aws.amazon.com/cloudfront-request/ and submit such a request. Also, keep in mind that updating a resource on CloudFront means that you’ll need to update it on S3 first, then invalidate the CloudFront copy only after the S3 has actually been updated, not when you make the request.

Subchild is a blog about web development. It's author is Aleksandar Kolundzija, himself a web developer
for 10++ years, presently a Hacker-in-Residence at betaworks. Prior to betaworks, Alex worked at Google, Meebo,
MLB Advanced Media (MLB.com), Razorfish, and elsewhere.