Class: AWS::EC2::InstanceCollection
- Inherits:
-
Collection
- Object
- Collection
- AWS::EC2::InstanceCollection
- Includes:
- BlockDeviceMappings, TaggedCollection
- 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 Attribute Summary
Attributes included from Core::Model
Instance Method Summary collapse
-
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
-
#create(options = {}) ⇒ Instance or Array
(also: #run)
Runs one or more EC2 instances.
- #each {|Instance| ... } ⇒ Object
Methods included from TaggedCollection
Methods included from FilteredCollection
#filter, #filtered_request, #initialize
Methods included from Core::Model
#client, #config_prefix, #initialize, #inspect
Instance Method Details
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
273 274 275 |
# File 'lib/aws/ec2/instance_collection.rb', line 273 def [] id super end |
#create(options = {}) ⇒ Instance or Array Also known as: run
Runs one or more EC2 instances.
sleep 1 while instances.any? {|i| i.status == :pending }
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/aws/ec2/instance_collection.rb', line 193 def create = {} if image = .delete(:image) [:image_id] = image.id end if kernel = .delete(:kernel) [:kernel_id] = kernel.id end if ramdisk = .delete(:ramdisk) [:ramdisk_id] = ramdisk.id end if key_pair = .delete(:key_pair) [:key_name] = key_pair.name end = ().merge() .delete(:count) [:user_data] = Base64.encode64([:user_data]).strip if [:user_data] [:block_device_mappings] = translate_block_device_mappings([:block_device_mappings]) if [:block_device_mappings] [:monitoring] = { :enabled => true } if [:monitoring_enabled] .delete(:monitoring_enabled) placement = {} if [:availability_zone] placement[:availability_zone] = [:availability_zone].to_s .delete(:availability_zone) end if [:dedicated_tenancy] placement[:tenancy] = 'dedicated' .delete(:dedicated_tenancy) end [:placement] = placement unless placement.empty? if subnet_id = subnet_id_option() [:subnet_id] = subnet_id end security_group_opts() [:client_token] = UUIDTools::UUID..to_s resp = client.run_instances() if [:min_count] == [:max_count] and [: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
262 263 264 265 266 267 268 269 |
# File 'lib/aws/ec2/instance_collection.rb', line 262 def each(&block) response = filtered_request(:describe_instances) response.reservation_set.each do |reservation| reservation.instances_set.each do |i| yield(Instance.new(i.instance_id, :config => config)) end end end |