Skip to content

Instantly share code, notes, and snippets.

@jeremyj
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyj/a1b98d321a335c3ed8a8 to your computer and use it in GitHub Desktop.
Save jeremyj/a1b98d321a335c3ed8a8 to your computer and use it in GitHub Desktop.
delete leftover CreateImage snapshots
#!/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