Simple AWS CloudFront Invalidations in PHP and CodeIgniter

Posted on 01/22/2016 by Brian Carey

Call me old school, but nowadays it seems like everywhere you look the PHP libraries being made available are looking like Ruby gems or Python modules.  Sometimes you just need a simple class that does a one or a handful of things and you don't want to have to bring along a bunch of dependencies to get it.  Recently I needed to do some quick AWS CloudFront invalidations from a basic PHP application.  I did not need to manage our entire AWS account, just that one single function.  A quick Google search didn't turn up much other than this library.  At first glance I was in luck as it looked like just the simple tool I needed but alas, it required the HTTP_Request2 pear module.  I made it work for the moment since it was still a trivial requirement and I needed to get work done, but decided to circle back and modify that class to work with cURL that is pretty much standard nowadays with any PHP install.

And now we have our new PHP class that can be used either in a standalone PHP application or easily pulled into a CodeIngniter framework as a library: https://github.com/kissit/php-cloudfront-invalidator.

Using the class in standalone PHP

First, download the class to your desired location.  For example:

$ wget https://raw.githubusercontent.com/kissit/php-cloudfront-invalidator/master/Cloudfront_invalidator.php

Require/include it as needed.

require 'Cloudfront_invalidator.php';

Instantiate an object passing your AWS credentials and CloudFront distribution id.

$invalidator = new Cloudfront_invalidator('AWS ACCESS KEY', 'AWS SECRET', 'CLOUDFRONT DIST ID');

And then finally make an invalidation call.

try {
    $invalidator->invalidate('/images/*');
} catch(Exception $e) {
    echo $e->getMessage() . "\n";
}

Using the class as a CodeIgniter library

Anyone familiar with CodeIgniter knows that their structure has specific requirements for using custom libraries and such.  This class is CodeIgniter compatible!

First, download the class to your application/libraries directory.  For example:

$ wget -O application/libraries/ https://raw.githubusercontent.com/kissit/php-cloudfront-invalidator/master/Cloudfront_invalidator.php

Load the library where desired

$this->load->library('cloudfront_invalidator');

Set your AWS credentials and CloudFront distribution id.  Note, this could also be passed to the load library call into constructor.

$this->cloudfront_invalidator->setAwsInfo($aws_key, $aws_secret, $aws_dist_id);

And then finally make an invalidation call.

try {
    $invalidator->invalidate('/images/*');
} catch(Exception $e) {
    echo $e->getMessage() . "\n";
}

Multiple invalidations per call

Note that you can also pass an array of paths to invalidate to have them all handled via the same request.  This is especially important if do frequent invalidations of multiple paths since you only get 1000 invalidation requests per month for free and have to pay for any above that.

try {
    $invalidator->invalidate(array('/images/*', '/some/other/path/*'));
} catch(Exception $e) {
    echo $e->getMessage() . "\n";
}

Conclusion

In conclusion, I hope someone finds this tool useful.  I hope everyone would agree it doesn't get much simpler than this.  As always, please contact us with any questions or inquiries about this post.