Skip to content

Instantly share code, notes, and snippets.

@nbibler
Created September 7, 2010 14:50
Show Gist options
  • Save nbibler/568483 to your computer and use it in GitHub Desktop.
Save nbibler/568483 to your computer and use it in GitHub Desktop.
Ruby script interacting with the Amazon AWS CloudFront invalidation
require 'rubygems'
require 'hmac-sha1'
require 'net/https'
require 'base64'
AWS_KEY = '...'
AWS_SECRET = '...'
CLOUDFRONT_DISTRIBUTION_ID = '...'
date = Time.now.utc
date = date.strftime("%a, %d %b %Y %H:%M:%S %Z")
digest = HMAC::SHA1.new(AWS_SECRET)
digest << date
uri = URI.parse("https://cloudfront.amazonaws.com/2010-08-01/distribution/%s/invalidation" % [CLOUDFRONT_DISTRIBUTION_ID])
req = Net::HTTP::Post.new(uri.path)
req.initialize_http_header({
'x-amz-date' => date,
'Content-Type' => 'text/xml',
'Authorization' => "AWS %s:%s" % [AWS_KEY, Base64.encode64(digest.digest)]
})
req.body = %|<InvalidationBatch><Path>PATH_TO_EXPIRE</Path><CallerReference>SOMETHING_SPECIAL_#{Time.now.utc.to_i}</CallerReference></InvalidationBatch>|
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.request(req)
puts res.inspect
puts res.body
puts res.code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment