Skip to content

CloudFront Invalidation from Ruby

Since none of the examples that I could find on the internet of how to invalidate a cloudfront asset in ruby were correct, I decided to post my solution:

require 'rubygems' # may not be needed
require 'openssl'
require 'digest/sha1'
require 'net/https'
require 'base64'

class CloudfrontInvalidator
	def initialize(aws_account, aws_secret, distribution)
		@aws_account = aws_account
		@aws_secret = aws_secret
		@distribution = distribution
	def invalidate(path)
		date ="%a, %d %b %Y %H:%M:%S %Z")
		digest = Base64.encode64(OpenSSL::HMAC.digest('sha1'), @aws_secret, date)).strip
		uri = URI.parse("{@distribution}/invalidation")

		req =
		  'x-amz-date' => date,
		  'Content-Type' => 'text/xml',
		  'Authorization' => "AWS %s:%s" % [@aws_account, digest]
		req.body = %|<InvalidationBatch><Path>#{path}</Path><CallerReference>SOMETHING_SPECIAL_#{}</CallerReference></InvalidationBatch>|
		http =, uri.port)
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		res = http.request(req)
		# it was successful if response code was a 201
		return res.code == '201'

Then just run it with:



  1. Bob Tekiela wrote:

    Thanks. This is exactly what I was looking for today. Worked like a charm.

    Wednesday, October 26, 2011 at 12:50 pm | Permalink
  2. This is great! FYI i’ve modified it slightly to take any number of path args like so:

    def invalidate(*paths)
    paths ={|p| “#{p}”}.join


    Monday, January 9, 2012 at 10:43 am | Permalink
  3. That previous post stripped out my Path tags that wrap the #{p} in the path string mapping

    Monday, January 9, 2012 at 10:46 am | Permalink
  4. Beth wrote:

    Incredible points. Sound arguments. Keep up the amazing

    Saturday, June 21, 2014 at 2:25 am | Permalink
  5. Juliana wrote:

    It’s hard to find your articles in google. I found it on 11 spot,
    you should build quality backlinks , it will help you to rank to google top
    10. I know how to help you, just type in google – k2 seo tips

    Saturday, July 5, 2014 at 7:45 am | Permalink
  6. Greetings I am so happy I found your webpage, I really found you by accident, while I
    was browsing on Askjeeve for something else, Regardless I am here now
    and would just like to say kudos for a marvelous post and a all round exciting blog (I also love the theme/design), I don’t
    have time to go through it all at the moment but I have book-marked it and also added your
    RSS feeds, so when I have time I will be back to read more, Please do keep up the great jo.

    Thursday, July 17, 2014 at 5:31 pm | Permalink
  7. domain wrote:

    Hi there! Quick question that’s entirely off topic. Do you know how to
    make your site mobile friendly? My website looks weird when browsing from my apple iphone.
    I’m trying to find a theme or plugin that might be able to correct this problem.
    If you have any recommendations, please share.
    Appreciate it!

    Monday, August 25, 2014 at 7:07 pm | Permalink
  8. Denisha wrote:

    I read a lot of interesting content here. Probably you spend a lot of time writing,
    i know how to save you a lot of work, there is an online tool that
    creates high quality, SEO friendly posts in minutes, just type in google – laranitas free content source

    Sunday, September 14, 2014 at 1:22 am | Permalink
  9. Sujet ѕuper attrayant

    Tuesday, September 23, 2014 at 2:25 am | Permalink
  10. XXX wrote:

    Un post ѵraiment plein de vérité

    Monday, October 13, 2014 at 5:59 pm | Permalink
  11. Tout сes articles sont clairement іnstructifs

    Saturday, December 6, 2014 at 12:58 pm | Permalink
  12. It’s actually a nice and useful piece of information. I am satisfied that you simply shared this helpful info with us.

    Please keep us up to date like this. Thank you for sharing.

    Sunday, January 4, 2015 at 9:56 am | Permalink
  13. pute sexy wrote:

    J’aі point fini de regarԀer par contre јe passerai demain

    Tuesday, January 13, 2015 at 7:28 pm | Permalink
  14. Vraiment plaisant : selon mοi cet argicle intéresserait ma

    Saturday, February 7, 2015 at 7:54 am | Permalink

4 Trackbacks/Pingbacks

  1. click to find out more on Friday, April 29, 2016 at 4:04 am

    click to find out more

    confabulus › CloudFront Invalidation from Ruby


    confabulus › CloudFront Invalidation from Ruby

  3. cheap furniture stores in glendale az on Saturday, August 20, 2016 at 6:38 pm

    cheap furniture stores in glendale az

    confabulus › CloudFront Invalidation from Ruby

  4. automotive electrical supplies uk on Thursday, July 20, 2017 at 7:15 am

    automotive electrical supplies uk

    confabulus › CloudFront Invalidation from Ruby

Post a Comment

Your email is never published nor shared.