Class: AWS::EC2::InstanceCollection
- Inherits:
-
Collection
- Object
- Collection
- AWS::EC2::InstanceCollection
- Defined in:
- lib/aws/ec2/instance_collection.rb
Overview
Represents a collection of EC2 instances. Typically you should get an instance of this class by calling #instances.
To run an instance:
ec2.instances.create(:image_id => "ami-8c1fece5")
To get an instance by ID:
i = ec2.instances["i-12345678"]
i.exists?
To get a map of instance IDs to instance status:
ec2.instances.inject({}) { |m, i| m[i.id] = i.status; m }
# => { "i-12345678" => :running, "i-87654321" => :shutting_down }
Instance Method Summary collapse
-
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
-
#create(opts = {}) ⇒ Instance or Array
(also: #run)
Runs one or more EC2 instances.
- #each {|Instance| ... } ⇒ Object
Instance Method Details
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
233 234 235 |
# File 'lib/aws/ec2/instance_collection.rb', line 233 def [] id super end |
#create(opts = {}) ⇒ Instance or Array Also known as: run
Runs one or more EC2 instances.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/aws/ec2/instance_collection.rb', line 167 def create(opts = {}) if image = opts.delete(:image) opts[:image_id] = image.id end if kernel = opts.delete(:kernel) opts[:kernel_id] = kernel.id end if ramdisk = opts.delete(:ramdisk) opts[:ramdisk_id] = ramdisk.id end if key_pair = opts.delete(:key_pair) opts[:key_name] = key_pair.name end opts = count_opts(opts).merge(opts) opts.delete(:count) opts[:user_data] = Base64.encode64(opts[:user_data]).strip if opts[:user_data] opts[:block_device_mappings] = translate_block_device_mappings(opts[:block_device_mappings]) if opts[:block_device_mappings] opts[:monitoring] = { :enabled => true } if opts[:monitoring_enabled] opts.delete(:monitoring_enabled) opts[:placement] = { :availability_zone => opts[:availability_zone].to_s } if opts[:availability_zone] opts.delete(:availability_zone) opts[:security_groups] = group_opts(opts[:security_groups]) if opts[:security_groups] opts[:client_token] = UUIDTools::UUID..to_s resp = client.run_instances(opts) if opts[:min_count] == opts[:max_count] and opts[:min_count] == 1 self[resp.instances_set.first.instance_id] else resp.instances_set.map do |i| self[i.instance_id] end end end |
#each {|Instance| ... } ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/aws/ec2/instance_collection.rb', line 222 def each(&block) response = filtered_request(:describe_instances) response.reservation_set.each do |r| r.instances_set.each do |i| yield(Instance.new(i.instance_id, :config => config)) end end end |