Skip to content

Instantly share code, notes, and snippets.

@popowa
Last active August 29, 2015 14:04
Show Gist options
  • Save popowa/b25293ee960e1bbe1dbe to your computer and use it in GitHub Desktop.
Save popowa/b25293ee960e1bbe1dbe to your computer and use it in GitHub Desktop.
delete ec2 resources and extra
# -*- encoding: utf-8 -*-
require 'aws-sdk'
AWS.config({
:access_key_id => '',
:secret_access_key => ''
}
)
ec2 = AWS::EC2::new
#delete EIP
ec2.elastic_ips.each do |eip|
if eip.associated?
puts "Diassociate from #{eip.instance_id}"
eip.disassociate
end
eip.delete
puts "Release: #{eip}"
end
#delete AMIs
ec2.images.with_owner("self").each do |image|
if image.exists?
puts "Deregister: #{image.image_id}/#{image.description}"
image.deregister
end
end
#Terminate instances
ec2.instances.each do |instance|
if instance.exists?
puts "Terminate: #{instance.id}"
instance.terminate
end
end
#Delete Keypair
ec2.key_pairs.each do |keypair|
if keypair.exists?
puts "Delete: #{keypair.name}/fingerprint:#{keypair.fingerprint}"
keypair.delete
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment