class AWS::EC2::Volume

Represents an Amazon EBS volume.

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

volume = ec2.volumes.create(:size => 15,
                            :availability_zone => "us-east-1a")
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

@attr_reader [Symbol] status The status of the volume.

Possible values:

* +:creating+
* +:available+
* +:in_use+
* +:deleting+
* +:deleted+
* +:error+

@attr_reader [Integer] size The size of the volume in

gigabytes.

@attr_reader [String] availability_zone_name Name of the

Availability Zone in which the volume was created.

@attr_reader [Time] create_time The time at which the volume

was created.

Attributes

id[R]

@return [String] Returns the volume ID.

Public Class Methods

new(id, opts = {}) click to toggle source

@private

# File lib/aws/ec2/volume.rb, line 55
def initialize(id, opts = {})
  @id = id
  super(opts)
end

Public Instance Methods

attach(instance, device) click to toggle source
Alias for: attach_to
attach_to(instance, device) click to toggle source

Attaches the volume to an instance.

@param [Instance] instance The instance to which the volume

attaches. The volume and instance must be within the same
Availability Zone and the instance must be running.

@param [String] device How the device is exposed to the

instance (e.g., /dev/sdh, or xvdh).

@return [Attachment] An object representing the attachment,

which you can use to query the attachment status.
# File lib/aws/ec2/volume.rb, line 115
def attach_to(instance, device)
  resp = client.attach_volume(
    :volume_id => id,
    :instance_id => instance.id,
    :device => device
  )
  instance = Instance.new(resp.instance_id, :config => config)
  Attachment.new(self, instance, resp.device, :config => config)
end
Also aliased as: attach
attachments() click to toggle source

@return [AttachmentCollection] The collection of attachments

that involve this volume.
# File lib/aws/ec2/volume.rb, line 164
def attachments
  AttachmentCollection.new(self, :config => config)
end
availability_zone() click to toggle source

@return [AvailabilityZone] Returns the Availability

Zone in which the volume was created.
# File lib/aws/ec2/volume.rb, line 156
def availability_zone
  if name = availability_zone_name
    AvailabilityZone.new(name, :config => config)
  end
end
create_snapshot(description = nil) click to toggle source

@return [Snapshot] A new snapshot created from the volume.

@param [String] description An optional description of the

snapshot.  May be up to 255 characters in length.
# File lib/aws/ec2/volume.rb, line 98
def create_snapshot description = nil
  opts = { :volume => self }
  opts[:description] = description if description
  SnapshotCollection.new(:config => config).create(opts)
end
delete() click to toggle source

Deletes the volume.

# File lib/aws/ec2/volume.rb, line 89
def delete
  client.delete_volume(:volume_id => id)
  nil
end
detach_from(instance, device, options = {}) click to toggle source

Detaches the volume from an instance.

@param [Instance] instance The instance to detach from. @param [String] device The device name. @param [Hash] options @option (see AWS::EC2::Attachment#delete) @return [Attachment] Returns the no-longer valid attachment.

# File lib/aws/ec2/volume.rb, line 133
def detach_from(instance, device, options = {})
  instance = Instance.new(instance.id, :config => config),
  a = Attachment.new(self, instance, device, :config => config)
  a.delete(options)
  a
end
exists?() click to toggle source

@return [Boolean] True if the volume exists.

# File lib/aws/ec2/volume.rb, line 141
def exists?
  resp = client.describe_volumes(:filters => [
    { :name => 'volume-id', :values => [id] }
  ]) 
  resp.volume_index.key?(id)
end
snapshot() click to toggle source

@return [Snapshot] Snapshot from which the volume was created

(may be nil).
# File lib/aws/ec2/volume.rb, line 150
def snapshot
  snapshot_id ? Snapshot.new(snapshot_id, :config => config) : nil
end