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
Methods included from FilteredCollection
Instance Method Details
#[](id) ⇒ Instance
Returns an object representing the EC2 instance with the given ID.
299 300 301 |
# File 'lib/aws/ec2/instance_collection.rb', line 299 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 }
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 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 |
# File 'lib/aws/ec2/instance_collection.rb', line 210 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] [: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 {|i| self[i[:instance_id]] } end end |
#each {|Instance| ... } ⇒ Object
288 289 290 291 292 293 294 295 |
# File 'lib/aws/ec2/instance_collection.rb', line 288 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 |