Skip to content

Instantly share code, notes, and snippets.

@dreamnite
Last active June 9, 2017 21:58
Show Gist options
  • Save dreamnite/a38a6494afd467e5466832252cacee68 to your computer and use it in GitHub Desktop.
Save dreamnite/a38a6494afd467e5466832252cacee68 to your computer and use it in GitHub Desktop.
find instances by name
array_of_zones = Aws::EC2::Client.new().describe_availability_zones.availability_zones.map { |x| x.zone_name }
require 'aws-sdk'
client = Aws::EC2:Client.new()
i = []
found_nodes = []
find_list = %w(name name name)
client.describe_instances[0].each { |x| i.push(x.instances.first) }
find_list.each |find_me|
i.each { |x| x.tags.each { |y| found_nodes.push(x) if y.value =~ %r(^#{find_me}$)}
end
# Found nodes now contains all information about the instances you are looking for
# Getting a map of the instance ids to work with
found_nodes.map {|x| x.instance_id } # ['i-xxx', 'i-yyy',... ]
#stopping instances of the found nodes
client.stop_instances(instance_ids: found_nodes.map {|x| x.instance_id } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment