Last active
August 29, 2015 14:07
-
-
Save jeremyj/a1b98d321a335c3ed8a8 to your computer and use it in GitHub Desktop.
delete leftover CreateImage snapshots
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/ruby | |
require 'rubygems' | |
require 'aws-sdk' | |
KEY = 'xxx' | |
SEC = 'xxx' | |
REGION = 'eu-west-1' | |
ec2 = AWS::EC2.new(:access_key_id=> KEY, :secret_access_key=> SEC, :region=> REGION) | |
AWS.memoize do | |
snapshots = ec2.snapshots.with_owner(:self) | |
images = ec2.images.with_owner(:self).collect { |i| i.id } | |
snapshots.each do |s| | |
if s.description =~ /Created by CreateImage/ | |
m = s.description.match(/ami\-.{8}/).to_s | |
if images.include?(m) | |
puts "#{m} is a registered AMI" | |
else | |
puts "#{s.id} is a useless snapshot. Deleting..." | |
s.delete | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment