class AWS::EC2::Attachment

Represents an attachment of an Amazon EBS volume to an instance.

@example Create an empty 15GiB volume and attach it to an instance

volume = ec2.volumes.create(:size => 15,
                            :availability_zone => "us-west-2a")
attachment = volume.attach_to(ec2.instances["i-123"], "/dev/sdf")
sleep 1 until attachment.status != :attaching

@example Remove all attachments from a volume and then delete it

volume.attachments.each do |attachment|
  attachment.delete(:force => true)
end
sleep 1 until volume.status == :available
volume.delete

Attributes

device[R]

@return [String] Returns how the device is exposed to the instance

(e.g. '/dev/sdh')
instance[R]

@return [Instance] Returns the EC2 instance the volume is attached to.

volume[R]

@return [Volume] Returns the volume that is attached.

Public Class Methods

new(volume, instance, device, options = {}) click to toggle source

@api private

Calls superclass method
# File lib/aws/ec2/attachment.rb, line 34
def initialize volume, instance, device, options = {}
  @volume = volume
  @instance = instance
  @device = device
  super
end

Public Instance Methods

delete(options = {}) click to toggle source

Detaches the volume from its instance. @option options [Boolean] :force Forces detachment if the

previous detachment attempt did not occur cleanly (logging
into an instance, unmounting the volume, and detaching
normally). This option can lead to data loss or a
corrupted file system. Use this option only as a last
resort to detach a volume from a failed instance. The
instance will not have an opportunity to flush file system
caches or file system metadata. If you use this option,
you must perform file system check and repair procedures.
# File lib/aws/ec2/attachment.rb, line 99
def delete options = {}
  client.detach_volume(options.merge(resource_options))
end
exists?() click to toggle source

@return [Boolean] Returns true if the attachment exists.

# File lib/aws/ec2/attachment.rb, line 85
def exists?
  !describe_attachment.nil?
end

Protected Instance Methods

describe_call() click to toggle source
# File lib/aws/ec2/attachment.rb, line 113
def describe_call
  client.describe_volumes(:volume_ids => [self.volume.id])
end
resource_identifiers() click to toggle source
# File lib/aws/ec2/attachment.rb, line 104
def resource_identifiers
  [
    [:volume_id, volume.id],
    [:instance_id, instance.id],
    [:device, device],
  ]
end

Private Instance Methods

describe_attachment() click to toggle source
# File lib/aws/ec2/attachment.rb, line 118
def describe_attachment
  find_attachment(describe_call)
end
find_attachment(resp) click to toggle source
# File lib/aws/ec2/attachment.rb, line 122
def find_attachment(resp)
  vol = resp.volume_index[volume.id] and
  attachments = vol.attachment_set and
  attachments.find do |att|
    att.instance_id == instance.id &&
      att.volume_id == volume.id &&
      att.device == device
  end
end