Amazon CloudFront

Simple CloudFront Invalidation of a Single File via HTTP 7


Amazon Web Services ButtFrontI found a great post by Clay Loveless who wrote a PHP script to batch invalidate files files on an Amazon Web Services CloudFront distribution. You rock, Clay. Amazon doesn’t even let us do that (yet?) via the AWS Mangament Console.

Until now, when I wanted to manually invalidate a single file, I had to fire up CloudBerry S3 Explorer and scroll through thousands of files to get to the one I wanted to invalidate. For automated on-the-fly batch sync and CloudFront invalidation, I still rely on the excellent s3cmd command-line tool.

I’m not a developer, but I can lightly hack my way around PHP code – especially when it’s as clean as Clay’s.  I took his excellent script one small step farther to allow invalidating a single file via HTTP (using your browser) by including the to-be-invalidated filename in a URL, with the ability to optionally pass my CloudFront Distribution ID via a separate variable in the URL (handy for those of us managing multiple distributions). I highly recommend making sure you place this file in a password protected area of your website.

So here’s my modified version of Clay’s PHP script providing a simple way to invalidate a single file on CloudFront using your browser:

  • Steve, thank you for this useful utility! I know that most credit goes to Clay for his original script, but still, it’s nice to find something that’s already been tweaked and tested to exactly what you need! I like having to invalidate CloudFront via and HTTP GET on a file-by-file basis.

    • Hey, Michael. Glad you like it. You’re right, I can’t take hardly any credit, but I do know exactly what you mean about being able to find exactly the utility you’re looking for without having to tweak, so I’m glad this one works for you. 🙂

  • DT

    Script works great and solves a simple problem I was looking for. I use very long expire times so try to avoid needing to invalidate files because of browser caches but on the rare occasion it is nice to have.

    Thanks!

  • David

    Thank you very much for this script. Perhaps I’m a complete idiot, but I can’t seem to get this to work.

    I’ve entered in all my credentials, but I’m still getting this error:
    SenderInvalidArgumentThere are one or more invalid invalidation paths.

    I’m simply trying to invalidate an image (47.png) that’s several subfolders below my main bucket. Do I put something special in place of FILENAME?

    A sample HTTP request would really help me, as I’ve used all sort of variations of:
    http://mydomain.com/invalidate.php?filename=47.png
    http://mydomain.com/invalidate.php?filename=cdn.myawsdomain.com/wp-content/uploads/userphoto/47.png
    etc…

    Sorry for the support request, but I feel like I’m missing something obvious.

    Thanks

  • Came across this post while looking for solution to batch invalidation. I wasn’t aware of amazon api and this was quite useful! I guess this post was posted some time ago. But for anyone who stumbled on this, I like to point out that this script only invalidate 1 file per request. After 3 continues request (3 files invalidation), you will get error. Amazon actually allow up to 1000 files per request. You may want to modify this script if you’re invalidating quite a bit of files

  • Nice work. I had a different kind of situation and ended up developing a daemon this weekend. If you are interested in invalidating multiple objects, i have made a daemon in PHP which takes object names from command line and can be called from any type of program. Also monitors a submitted request for completion and writes to a logfile. Its at https://github.com/codingthoughts/Cloudfront-Invalidator-Daemon

  • luvsolution

    Hi,

    I don’t understand why you don’t have quotes in your $xml = <<<EOD…

    I can't seem to get it to work without quotes but I also couldn't get it to work with it…

    What am I missing?

    Thanks,
    D