Skip to content

Instantly share code, notes, and snippets.

@larsschenk
Created August 21, 2021 11:49
Show Gist options
  • Save larsschenk/5d313dc7b33ed2ed972253fd149bd74c to your computer and use it in GitHub Desktop.
Save larsschenk/5d313dc7b33ed2ed972253fd149bd74c to your computer and use it in GitHub Desktop.
Legacy code from Oct 5 2012. Deleting registered ec2 snapshots by first unregistering the ami and then deleting the snapshot.
#!/usr/bin/env ruby -W0
# -W0 = disable Ruby “Insecure world writable dir” warnings
# ec2-delete-registered-snapshot
#
# This is a simple timesaver script for deleting registered snapshots by
# first unregistering the ami and then deleting the snapshot.
# I use it for the EBS based backups that I create by another script.
#
# The ec2-descripe-snapshots output is as follows:
# SNAPSHOT snap-12345678 vol-90abcdef completed 2010-07-27T21:52:51+0000 100% 266627911898 15 Created by CreateImage(i-87654321) for ami-abcdefab from vol-90abcdef
if __FILE__ == $0
snapshot = ARGV[0]
output = `ec2-describe-snapshots #{snapshot}`
if output =~ /for\s(ami-.+)\sfrom/
#puts "We found #{$&} via\n#{$1}"
ami = $1
#puts ami
if output =~ /.+?\s+?.+?\s+?.+?\s+?.+?\s+?(.+?)\s+?/
snapdate = $1
end
puts "#{snapshot} created on #{snapdate} is #{ami} will be deregistered and deleted\n"
# deregister the ami
result = `ec2-deregister #{ami}`
puts result
# delete the snapshot
result = `ec2-delete-snapshot #{snapshot}`
puts result
else
puts "No ami found for #{snapshot}.\n"
end
puts "bye...\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment