Created
November 13, 2014 18:44
-
-
Save milesmatthias/0f877782fdb800da7ca8 to your computer and use it in GitHub Desktop.
simple ruby script to invalidate cloudfront objects. requires having the aws gem installed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## | |
# usage: AWS_SECRET_KEY=xxx AWS_ACCESS_KEY=xxx CF_DIST_ID=xxx ./invalidate_cf.rb file1.html file2.html | |
# | |
# | |
require 'aws' | |
require 'date' | |
require 'pry' | |
aws_secret_key = ENV['AWS_SECRET_KEY'] | |
aws_access_key = ENV['AWS_ACCESS_KEY'] | |
cf_dist_id = ENV['CF_DIST_ID'] | |
if [aws_secret_key, aws_access_key, cf_dist_id].include?(nil) | |
abort 'usage: AWS_SECRET_KEY=xxx AWS_ACCESS_KEY=xxx CF_DIST_ID=xxx ./invalidate_cf.rb "/file1.html" "/file2.html"' | |
end | |
# | |
files = ARGV | |
if files.count == 0 | |
abort 'usage: AWS_SECRET_KEY=xxx AWS_ACCESS_KEY=xxx CF_DIST_ID=xxx ./invalidate_cf.rb "/file1.html" "/file2.html"' | |
end | |
# | |
cf = AWS::CloudFront.new({ | |
:access_key_id => aws_access_key, | |
:secret_access_key => aws_secret_key | |
}) | |
invalidation = cf.client.create_invalidation({ | |
:distribution_id => cf_dist_id, | |
:invalidation_batch => { | |
:paths => { | |
:quantity => files.count, | |
:items => files | |
}, | |
:caller_reference => "INVALIDATE_CF_" + DateTime.now.to_s | |
} | |
}).data | |
puts "invalidation status: #{invalidation[:status]}" | |
# | |
status = invalidation[:status] | |
while status == 'InProgress' | |
response = cf.client.get_invalidation({ | |
:distribution_id => cf_dist_id, | |
:id => invalidation[:id] | |
}) | |
status = response.data[:status] | |
puts "status = #{ status }" | |
sleep(rand(30)) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment