Class: AWS::EC2::Instance

Inherits:
Resource
  • Object
show all
Includes:
TaggedItem
Defined in:
lib/aws/ec2/instance.rb

Overview

Represents an EC2 instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TaggedItem

#add_tag, #clear_tags, #tags

Constructor Details

#initialize(instance_id, opts = {}) ⇒ Instance

Creates an object that represents the instance with the given ID. It’s usually easier to get an instance of this class by calling AWS::EC2::InstanceCollection#[] or AWS::EC2::InstanceCollection#each.



150
151
152
153
154
# File 'lib/aws/ec2/instance.rb', line 150

def initialize(instance_id, opts = {})
  super
  @id = instance_id
  @reservation_attributes = {}
end

Instance Attribute Details

#ami_launch_indexInteger (readonly)

The AMI launch index, which can be used to find this instance within the launch group.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def ami_launch_index
  @ami_launch_index
end

#api_termination_disabledBoolean

True if the instance cannot be terminated using the #terminate method. This attribute can be changed at any time.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def api_termination_disabled
  @api_termination_disabled
end

#architectureSymbol (readonly)

The architecture of the image.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def architecture
  @architecture
end

#client_tokenString (readonly)

Idempotency token you provided when you launched the instance.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def client_token
  @client_token
end

#dns_nameString (readonly) Also known as: public_dns_name

The DNS name of the instance on the internet.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def dns_name
  @dns_name
end

#hypervisorSymbol (readonly)

The instance’s hypervisor type. Valid values:

  • :ovm

  • :xen



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def hypervisor
  @hypervisor
end

#idString (readonly)



157
158
159
# File 'lib/aws/ec2/instance.rb', line 157

def id
  @id
end

#image_idString (readonly)

Image ID of the AMI used to launch the instance.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def image_id
  @image_id
end

#instance_initiated_shutdown_behaviorString

Valid values are:

“stop”

When the instance shuts down, it will go into a “stopped” state.

“terminate”

When the instance shuts down, it will be terminated.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def instance_initiated_shutdown_behavior
  @instance_initiated_shutdown_behavior
end

#instance_typeString

The instance type, e.g. “m1.small”. The instance must be in a stopped state to change the instance type.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def instance_type
  @instance_type
end

#ip_addressString (readonly) Also known as: public_ip_address

The IP address of the instance.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def ip_address
  @ip_address
end

#kernel_idString

The ID of the kernel that the image currently uses. The instance must be in a stopped state to change this attribute.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def kernel_id
  @kernel_id
end

#key_nameString (readonly)

The name of the key pair with which this instance was associated at launch.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def key_name
  @key_name
end

#launch_timeTime (readonly)

The time at which the instance was launched.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def launch_time
  @launch_time
end

#monitoringSymbol (readonly)

The status of CloudWatch monitoring for the instance. Valid values:

  • :enabled

  • :disabled

  • :pending



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def monitoring
  @monitoring
end

#owner_idString (readonly)

ID of the AWS account that owns the reservation in which the instance was launched.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def owner_id
  @owner_id
end

#platformString (readonly)

A string describing the platform of the image (e.g. “windows”).



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def platform
  @platform
end

#private_dns_nameString (readonly)

The DNS name of the instance within the EC2 network.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def private_dns_name
  @private_dns_name
end

#private_ip_addressString (readonly)

The private IP address assigned to the instance.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def private_ip_address
  @private_ip_address
end

#ramdisk_idString

The ID of the RAM disk that the image currently uses. The instance must be in a stopped state to change this attribute.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def ramdisk_id
  @ramdisk_id
end

#requester_idString (readonly)

ID of the requester that launched the instance on your behalf (e.g., AWS Management Console, Auto Scaling).



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def requester_id
  @requester_id
end

#reservation_idString (readonly)

The ID of the reservation in which this instance was launched.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def reservation_id
  @reservation_id
end

#root_device_nameString (readonly)

The name of the root device.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def root_device_name
  @root_device_name
end

#root_device_typeSymbol (readonly)

The root device type used by the AMI. The AMI can use an Amazon EBS or instance store root device. Valid values:

  • :ebs

  • :instance_store



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def root_device_type
  @root_device_type
end

#state_transition_reasonString (readonly)

A string describing the reason for the last state transition.



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def state_transition_reason
  @state_transition_reason
end

#user_dataString

Arbitrary metadata that is available to the instance while it is running. This interface handles the details of encoding the user data for transmission; you should set the user data exactly as you want it to be made available to the instance.

The instance must be in a stopped state to change user data; for example:

i.user_data             # => "HELLO"
i.status                # => :running
i.user_data = "GOODBYE" # raises an exception
i.stop; sleep 1 until i.status == :stopped
i.user_data = "GOODBYE" # => "GOODBYE"


142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def user_data
  @user_data
end

#virtualization_typeSymbol (readonly)

The instance’s virtualization type. Valid values:

  • :paravirtual

  • :hvm



142
143
144
# File 'lib/aws/ec2/instance.rb', line 142

def virtualization_type
  @virtualization_type
end

Instance Method Details

#associate_elastic_ip(elastic_ip) ⇒ nil Also known as: ip_address=

Associates the elastic IP address with this instance.



421
422
423
424
425
426
# File 'lib/aws/ec2/instance.rb', line 421

def associate_elastic_ip elastic_ip
  client.associate_address(
    :public_ip => elastic_ip.to_s,
    :instance_id => self.id)
  nil
end

#availability_zoneString



260
261
262
263
264
# File 'lib/aws/ec2/instance.rb', line 260

def availability_zone
  if p = placement
    p.availability_zone
  end
end

#block_device_mappingsHash



390
# File 'lib/aws/ec2/instance.rb', line 390

def block_device_mappings; end

#create_image(name, options = {}) ⇒ Image

Creates an AMI from this instance.

Options Hash (options):

  • :description (String)

    A description of the new image.

  • :no_reboot (Boolean)

    By default this option is set to false, which means Amazon EC2 attempts to cleanly shut down the instance before image creation and reboots the instance afterwards. When the option is set to true, Amazon EC2 does not shut down the instance before creating the image. When this option is used, file system integrity on the created image cannot be guaranteed.



309
310
311
312
313
# File 'lib/aws/ec2/instance.rb', line 309

def create_image(name, options = {})
  ImageCollection.new(:config => config).
      create(options.merge(:instance_id => id,
                           :name => name))
end

#disassociate_elastic_ipObject

Disassociates an attached elastic IP address from this instance. Raises an exception if there is no elastic IP address associated with this instance.



433
434
435
436
437
438
439
# File 'lib/aws/ec2/instance.rb', line 433

def disassociate_elastic_ip
  if ip = self.elastic_ip
    ip.disassociate
  else
    raise "instance #{id} does not have an associated elastic ip"
  end
end

#elastic_ipElasticIp?



449
450
451
452
# File 'lib/aws/ec2/instance.rb', line 449

def elastic_ip
  ElasticIpCollection.new(:config => config).
    filter('instance-id', id).first
end

#exists?Boolean



161
162
163
164
165
166
167
# File 'lib/aws/ec2/instance.rb', line 161

def exists?
  client.describe_instances(:filters =>
                            [{
                               :name => "instance-id",
                               :values => [id]
                             }]).instance_index.key?(id)
end

#has_elastic_ip?Boolean



443
444
445
# File 'lib/aws/ec2/instance.rb', line 443

def has_elastic_ip?
  !elastic_ip.nil?
end

#imageImage



274
275
276
# File 'lib/aws/ec2/instance.rb', line 274

def image
  Image.new(image_id, :config => config)
end

#key_pairKeyPair



282
283
284
# File 'lib/aws/ec2/instance.rb', line 282

def key_pair
  KeyPair.new(key_name, :config => config) if key_name
end

#monitoring_enabled=(value) ⇒ Object

Enables or disables monitoring for this instance.



215
216
217
218
219
220
221
222
223
# File 'lib/aws/ec2/instance.rb', line 215

def monitoring_enabled=(value)
  if value
    client.monitor_instances(:instance_ids => [id])
    true
  else
    client.unmonitor_instances(:instance_ids => [id])
    false
  end
end

#monitoring_enabled?Boolean



210
211
212
# File 'lib/aws/ec2/instance.rb', line 210

def monitoring_enabled?
  monitoring == :enabled
end

#rebootnil

Reboots the instance.



185
# File 'lib/aws/ec2/instance.rb', line 185

def reboot; end

#reset_kernel_idObject

Resets the kernel to its default value.



358
359
360
361
# File 'lib/aws/ec2/instance.rb', line 358

def reset_kernel_id
  client.reset_instance_attribute(:instance_id => id,
                                  :attribute => "kernel").return
end

#reset_ramdisk_idObject

Resets the RAM disk to its default value.



368
369
370
371
# File 'lib/aws/ec2/instance.rb', line 368

def reset_ramdisk_id
  client.reset_instance_attribute(:instance_id => id,
                                  :attribute => "ramdisk").return
end

#security_groupsArray Also known as: groups



228
229
230
# File 'lib/aws/ec2/instance.rb', line 228

def security_groups
  group_set
end

#spot_instance?Boolean



250
251
252
# File 'lib/aws/ec2/instance.rb', line 250

def spot_instance?
  instance_lifecycle == :spot
end

#startnil

Starts the instance, assuming it is in a stopped state.

See Also:



191
# File 'lib/aws/ec2/instance.rb', line 191

def start; end

#statusSymbol



409
410
411
412
413
# File 'lib/aws/ec2/instance.rb', line 409

def status
  retrieve_attribute(:status) do
    client.describe_instances(:instance_ids => [id])
  end
end

#stopnil

Stops the instance, eventually putting it into a stopped state.



196
# File 'lib/aws/ec2/instance.rb', line 196

def stop; end

#terminatenil Also known as: delete

Terminates the instance.



179
# File 'lib/aws/ec2/instance.rb', line 179

def terminate; end