Class: AWS::EC2::Volume

Inherits:
Resource show all
Includes:
TaggedItem
Defined in:
lib/aws/ec2/volume.rb

Overview

Represents an Amazon EBS volume.

Examples:

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

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

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from TaggedItem

#add_tag, #cached_tags, #clear_tags, #tagging_resource_type, #tags

Methods inherited from Core::Resource

attribute_providers, attribute_providers_for, attributes, #attributes_from_response, define_attribute_type, #eql?, #inspect, new_from

Methods included from Core::Cacheable

included, #retrieve_attribute

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(id, opts = {}) ⇒ Volume



56
57
58
59
# File 'lib/aws/ec2/volume.rb', line 56

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

Instance Attribute Details

#availability_zone_nameString (readonly)

Name of the Availability Zone in which the volume was created.



51
52
53
# File 'lib/aws/ec2/volume.rb', line 51

def availability_zone_name
  @availability_zone_name
end

#create_timeTime (readonly)

The time at which the volume was created.



51
52
53
# File 'lib/aws/ec2/volume.rb', line 51

def create_time
  @create_time
end

#idString (readonly)



62
63
64
# File 'lib/aws/ec2/volume.rb', line 62

def id
  @id
end

#sizeInteger (readonly)

The size of the volume in gigabytes.



51
52
53
# File 'lib/aws/ec2/volume.rb', line 51

def size
  @size
end

#statusSymbol (readonly) Also known as: state

The status of the volume. Possible values:

  • :creating

  • :available

  • :in_use

  • :deleting

  • :deleted

  • :error



51
52
53
# File 'lib/aws/ec2/volume.rb', line 51

def status
  @status
end

Instance Method Details

#attach_to(instance, device) ⇒ Attachment Also known as: attach

Attaches the volume to an instance.



114
115
116
117
118
119
120
121
122
# File 'lib/aws/ec2/volume.rb', line 114

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

#attachmentsAttachmentCollection



163
164
165
# File 'lib/aws/ec2/volume.rb', line 163

def attachments
  AttachmentCollection.new(self, :config => config)
end

#availability_zoneAvailabilityZone



155
156
157
158
159
# File 'lib/aws/ec2/volume.rb', line 155

def availability_zone
  if name = availability_zone_name
    AvailabilityZone.new(name, :config => config)
  end
end

#create_snapshot(description = nil) ⇒ Snapshot



97
98
99
100
101
# File 'lib/aws/ec2/volume.rb', line 97

def create_snapshot description = nil
  opts = { :volume => self }
  opts[:description] = description if description
  SnapshotCollection.new(:config => config).create(opts)
end

#deleteObject

Deletes the volume.



88
89
90
91
# File 'lib/aws/ec2/volume.rb', line 88

def delete
  client.delete_volume(:volume_id => id)
  nil
end

#detach_from(instance, device, options = {}) ⇒ Attachment

Detaches the volume from an instance.

Options Hash (options):

  • :force (Boolean)

    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.



132
133
134
135
136
137
# File 'lib/aws/ec2/volume.rb', line 132

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?Boolean



140
141
142
143
144
145
# File 'lib/aws/ec2/volume.rb', line 140

def exists?
  resp = client.describe_volumes(:filters => [
    { :name => 'volume-id', :values => [id] }
  ]) 
  resp.volume_index.key?(id)
end

#snapshotSnapshot



149
150
151
# File 'lib/aws/ec2/volume.rb', line 149

def snapshot
  snapshot_id ? Snapshot.new(snapshot_id, :config => config) : nil
end