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(options = {}) ⇒ 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.
239 240 241 |
# File 'lib/aws/ec2/instance_collection.rb', line 239 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 }
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 219 220 221 222 223 |
# File 'lib/aws/ec2/instance_collection.rb', line 171 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] = { :availability_zone => [:availability_zone].to_s } if [:availability_zone] .delete(:availability_zone) [:security_groups] = group_opts([:security_groups]) if [:security_groups] [: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
228 229 230 231 232 233 234 235 |
# File 'lib/aws/ec2/instance_collection.rb', line 228 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 |