Skip to content

Instantly share code, notes, and snippets.

@petems
Last active December 20, 2015 06:49
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 petems/6088973 to your computer and use it in GitHub Desktop.
Save petems/6088973 to your computer and use it in GitHub Desktop.
How to mass delete all EC2 instances, useful to run on a cron if you're paranoid about leaving stuff on all the time...
# Get these from https://console.aws.amazon.com/iam/home?#security_credential
export AMAZON_ACCESS_KEY_ID=FOO
export AMAZON_SECRET_ACCESS_KEY=BAR
export EC2_REGION="eu-west-1"
require 'fog'
#Either source .aws_stuff or add it to .bashrc etc
@credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'],
:region => ENV['EC2_REGION'],
}
def connection
@connection ||= Fog::Compute.new(@credentials)
end
connection.servers.all.each do |server|
puts server.id
puts "Destroying server now!"
server.destroy
puts "#{server.id} destroyed successfully"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment