You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.
Class: AWS::EC2::InstanceCollection
- Inherits:
-
Collection
- Object
- Collection
- AWS::EC2::InstanceCollection
- Includes:
- 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-1b814f72")
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(options = {}) ⇒ Instance or Array
(also: #run)
Runs one or more EC2 instances.
-
#each {|Instance| ... } ⇒ Object
Methods included from TaggedCollection
#tagged, #tagged_values, #with_tag
Methods included from FilteredCollection
Instance Method Details
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
330 331 332 |
# File 'lib/aws/ec2/instance_collection.rb', line 330 def [] id super end |
#create(options = {}) ⇒ Instance or Array Also known as: run
Runs one or more EC2 instances.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/aws/ec2/instance_collection.rb', line 212 def create = {} if profile = .delete(:iam_instance_profile) profile = case profile when /^arn:aws:iam::/ then { :arn => profile } when String then { :name => profile } when Hash then profile else msg = "expected a name or ARN string for :iam_instance_profile" end [:iam_instance_profile] = profile end 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] if [:block_device_mappings].is_a?(Hash) [:block_device_mappings] = translate_block_device_mappings([:block_device_mappings]) end [: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 [:placement_group] placement[:group_name] = [:placement_group].to_s .delete(:placement_group) end if [:dedicated_tenancy] placement[:tenancy] = 'dedicated' .delete(:dedicated_tenancy) end [:placement] = placement unless placement.empty? network_interface = {} if [:associate_public_ip_address] if subnet_id = subnet_id_option() network_interface[:subnet_id] = subnet_id .delete(:subnet) .delete(:subnet_id) end if private_ip_address = .delete(:private_ip_address) network_interface[:private_ip_address] = private_ip_address end if security_group_ids = .delete(:security_group_ids) network_interface[:groups] = Array(security_group_ids) end network_interface[:associate_public_ip_address] = true network_interface[:device_index] = 0 end .delete(:associate_public_ip_address) [:network_interfaces] = [network_interface] unless network_interface.empty? if subnet_id = subnet_id_option() [:subnet_id] = subnet_id end security_group_opts() [:client_token] = SecureRandom.uuid unless [:client_token] 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 {|i| self[i[:instance_id]] } end end |
#each {|Instance| ... } ⇒ Object
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/aws/ec2/instance_collection.rb', line 317 def each(&block) response = filtered_request(:describe_instances) response.reservation_set.each do |reservation| reservation.instances_set.each do |i| instance = Instance.new_from(:describe_instances, i, i.instance_id, :config => config) yield(instance) end end end |