You are viewing documentation for version 3 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: Aws::EC2::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb

Defined Under Namespace

Classes: Collection

Actions collapse

Associations collapse

Read-Only Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Snapshot #initialize(options = {}) ⇒ Snapshot

Returns a new instance of Snapshot

Overloads:

  • #initialize(id, options = {}) ⇒ Snapshot

    Parameters:

    • id (String)

    Options Hash (options):

  • #initialize(options = {}) ⇒ Snapshot

    Options Hash (options):

    • :id (required, String)
    • :client (Client)


19
20
21
22
23
24
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 19

def initialize(*args)
  options = Hash === args.last ? args.pop.dup : {}
  @id = extract_id(args, options)
  @data = options.delete(:data)
  @client = options.delete(:client) || Client.new(options)
end

Instance Method Details

#clientClient

Returns:



132
133
134
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 132

def client
  @client
end

#copy(options = {}) ⇒ Types::CopySnapshotResult

Examples:

Request syntax with placeholder values


snapshot.copy({
  description: "String",
  destination_region: "String",
  encrypted: false,
  kms_key_id: "String",
  presigned_url: "String",
  source_region: "String", # required
  dry_run: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :description (String)

    A description for the EBS snapshot.

  • :destination_region (String)

    The destination region to use in the PresignedUrl parameter of a snapshot copy operation. This parameter is only valid for specifying the destination region in a PresignedUrl parameter, where it is required.

    CopySnapshot sends the snapshot copy to the regional endpoint that you send the HTTP request to, such as ec2.us-east-1.amazonaws.com (in the AWS CLI, this is specified with the --region parameter or the default region in your AWS configuration file).

  • :encrypted (Boolean)

    Specifies whether the destination snapshot should be encrypted. You can encrypt a copy of an unencrypted snapshot using this flag, but you cannot use it to create an unencrypted copy from an encrypted snapshot. Your default CMK for EBS is used unless a non-default AWS Key Management Service (AWS KMS) CMK is specified with KmsKeyId. For more information, see Amazon EBS Encryption in the Amazon Elastic Compute Cloud User Guide.

  • :kms_key_id (String)

    The full ARN of the AWS Key Management Service (AWS KMS) CMK to use when creating the snapshot copy. This parameter is only required if you want to use a non-default CMK; if this parameter is not specified, the default CMK for EBS is used. The ARN contains the arn:aws:kms namespace, followed by the region of the CMK, the AWS account ID of the CMK owner, the key namespace, and then the CMK ID. For example, arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456a-a12b-a123b4cd56ef. The specified CMK must exist in the region that the snapshot is being copied to. If a KmsKeyId is specified, the Encrypted flag must also be set.

  • :presigned_url (String)

    The pre-signed URL that facilitates copying an encrypted snapshot. This parameter is only required when copying an encrypted snapshot with the Amazon EC2 Query API; it is available as an optional parameter in all other cases. The PresignedUrl should use the snapshot source endpoint, the CopySnapshot action, and include the SourceRegion, SourceSnapshotId, and DestinationRegion parameters. The PresignedUrl must be signed using AWS Signature Version 4. Because EBS snapshots are stored in Amazon S3, the signing algorithm for this parameter uses the same logic that is described in Authenticating Requests by Using Query Parameters (AWS Signature Version 4) in the Amazon Simple Storage Service API Reference. An invalid or improperly signed PresignedUrl will cause the copy operation to fail asynchronously, and the snapshot will move to an error state.

  • :source_region (required, String)

    The ID of the region that contains the snapshot to be copied.

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Returns:



355
356
357
358
359
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 355

def copy(options = {})
  options = options.merge(source_snapshot_id: @id)
  resp = @client.copy_snapshot(options)
  resp.data
end

#create_tags(options = {}) ⇒ Tag::Collection

Examples:

Request syntax with placeholder values


tag = snapshot.create_tags({
  dry_run: false,
  tags: [ # required
    {
      key: "String",
      value: "String",
    },
  ],
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

  • :tags (required, Array<Types::Tag>)

    One or more tags. The value parameter is required, but if you don't want the tag to have a value, specify the parameter with no value, and we set the value to an empty string.

Returns:



383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 383

def create_tags(options = {})
  batch = []
  options = Aws::Util.deep_merge(options, resources: [@id])
  resp = @client.create_tags(options)
  options[:tags].each do |t|
    batch << Tag.new(
      resource_id: @id,
      key: t[:key],
      value: t[:value],
      client: @client
    )
  end
  Tag::Collection.new([batch], size: batch.size)
end

#dataTypes::Snapshot

Returns the data for this Aws::EC2::Snapshot. Calls Client#describe_snapshots if #data_loaded? is false.

Returns:



152
153
154
155
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 152

def data
  load unless @data
  @data
end

#data_encryption_key_idString

The data encryption key identifier for the snapshot. This value is a unique identifier that corresponds to the data encryption key that was used to encrypt the original volume or snapshot copy. Because data encryption keys are inherited by volumes created from snapshots, and vice versa, if snapshots share the same data encryption key identifier, then they belong to the same volume/snapshot lineage. This parameter is only returned by the DescribeSnapshots API operation.

Returns:

  • (String)


42
43
44
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 42

def data_encryption_key_id
  data[:data_encryption_key_id]
end

#data_loaded?Boolean

Returns true if this resource is loaded. Accessing attributes or #data on an unloaded resource will trigger a call to #load.

Returns:

  • (Boolean)

    Returns true if this resource is loaded. Accessing attributes or #data on an unloaded resource will trigger a call to #load.



160
161
162
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 160

def data_loaded?
  !!@data
end

#delete(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


snapshot.delete({
  dry_run: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Returns:

  • (EmptyStructure)


410
411
412
413
414
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 410

def delete(options = {})
  options = options.merge(snapshot_id: @id)
  resp = @client.delete_snapshot(options)
  resp.data
end

#describe_attribute(options = {}) ⇒ Types::DescribeSnapshotAttributeResult

Examples:

Request syntax with placeholder values


snapshot.describe_attribute({
  attribute: "productCodes", # required, accepts productCodes, createVolumePermission
  dry_run: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :attribute (required, String)

    The snapshot attribute you would like to view.

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Returns:



431
432
433
434
435
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 431

def describe_attribute(options = {})
  options = options.merge(snapshot_id: @id)
  resp = @client.describe_snapshot_attribute(options)
  resp.data
end

#descriptionString

The description for the snapshot.

Returns:

  • (String)


48
49
50
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 48

def description
  data[:description]
end

#encryptedBoolean

Indicates whether the snapshot is encrypted.

Returns:

  • (Boolean)


54
55
56
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 54

def encrypted
  data[:encrypted]
end

#idString Also known as: snapshot_id

Returns:

  • (String)


29
30
31
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 29

def id
  @id
end

#kms_key_idString

The full ARN of the AWS Key Management Service (AWS KMS) customer master key (CMK) that was used to protect the volume encryption key for the parent volume.

Returns:

  • (String)


62
63
64
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 62

def kms_key_id
  data[:kms_key_id]
end

#loadself Also known as: reload

Loads, or reloads #data for the current Aws::EC2::Snapshot. Returns self making it possible to chain methods.

snapshot.reload.data

Returns:

  • (self)


142
143
144
145
146
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 142

def load
  resp = @client.describe_snapshots(snapshot_ids: [@id])
  @data = resp.snapshots[0]
  self
end

#modify_attribute(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


snapshot.modify_attribute({
  attribute: "productCodes", # accepts productCodes, createVolumePermission
  create_volume_permission: {
    add: [
      {
        group: "all", # accepts all
        user_id: "String",
      },
    ],
    remove: [
      {
        group: "all", # accepts all
        user_id: "String",
      },
    ],
  },
  group_names: ["String"],
  operation_type: "add", # accepts add, remove
  user_ids: ["String"],
  dry_run: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :attribute (String)

    The snapshot attribute to modify.

    Only volume creation permissions may be modified at the customer level.

  • :create_volume_permission (Types::CreateVolumePermissionModifications)

    A JSON representation of the snapshot attribute modification.

  • :group_names (Array<String>)

    The group to modify for the snapshot.

  • :operation_type (String)

    The type of operation to perform to the attribute.

  • :user_ids (Array<String>)

    The account ID to modify for the snapshot.

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Returns:

  • (EmptyStructure)


482
483
484
485
486
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 482

def modify_attribute(options = {})
  options = options.merge(snapshot_id: @id)
  resp = @client.modify_snapshot_attribute(options)
  resp.data
end

#owner_aliasString

Value from an Amazon-maintained list (amazon | aws-marketplace | microsoft) of snapshot owners. Not to be confused with the user-configured AWS account alias, which is set from the IAM console.

Returns:

  • (String)


119
120
121
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 119

def owner_alias
  data[:owner_alias]
end

#owner_idString

The AWS account ID of the EBS snapshot owner.

Returns:

  • (String)


68
69
70
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 68

def owner_id
  data[:owner_id]
end

#progressString

The progress of the snapshot, as a percentage.

Returns:

  • (String)


74
75
76
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 74

def progress
  data[:progress]
end

#reset_attribute(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


snapshot.reset_attribute({
  attribute: "productCodes", # required, accepts productCodes, createVolumePermission
  dry_run: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :attribute (required, String)

    The attribute to reset. Currently, only the attribute for permission to create volumes can be reset.

  • :dry_run (Boolean)

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Returns:

  • (EmptyStructure)


504
505
506
507
508
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 504

def reset_attribute(options = {})
  options = options.merge(snapshot_id: @id)
  resp = @client.reset_snapshot_attribute(options)
  resp.data
end

#start_timeTime

The time stamp when the snapshot was initiated.

Returns:

  • (Time)


80
81
82
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 80

def start_time
  data[:start_time]
end

#stateString

The snapshot state.

Returns:

  • (String)


86
87
88
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 86

def state
  data[:state]
end

#state_messageString

Encrypted Amazon EBS snapshots are copied asynchronously. If a snapshot copy operation fails (for example, if the proper AWS Key Management Service (AWS KMS) permissions are not obtained) this field displays error state details to help you diagnose why the error occurred. This parameter is only returned by the DescribeSnapshots API operation.

Returns:

  • (String)


97
98
99
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 97

def state_message
  data[:state_message]
end

#tagsArray<Types::Tag>

Any tags assigned to the snapshot.

Returns:



125
126
127
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 125

def tags
  data[:tags]
end

#volumeVolume?

Returns:



513
514
515
516
517
518
519
520
521
522
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 513

def volume
  if data[:volume_id]
    Volume.new(
      id: data[:volume_id],
      client: @client
    )
  else
    nil
  end
end

#volume_idString

The ID of the volume that was used to create the snapshot. Snapshots created by the CopySnapshot action have an arbitrary volume ID that should not be used for any purpose.

Returns:

  • (String)


105
106
107
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 105

def volume_id
  data[:volume_id]
end

#volume_sizeInteger

The size of the volume, in GiB.

Returns:

  • (Integer)


111
112
113
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 111

def volume_size
  data[:volume_size]
end

#wait_until(options = {}, &block) ⇒ Resource

Deprecated.

Use [Aws::EC2::Client] #wait_until instead

Note:

The waiting operation is performed on a copy. The original resource remains unchanged

Waiter polls an API operation until a resource enters a desired state.

Basic Usage

Waiter will polls until it is successful, it fails by entering a terminal state, or until a maximum number of attempts are made.

# polls in a loop until condition is true
resource.wait_until(options) {|resource| condition}

Example

instance.wait_until(max_attempts:10, delay:5) {|instance| instance.state.name == 'running' }

Configuration

You can configure the maximum number of polling attempts, and the delay (in seconds) between each polling attempt. The waiting condition is set by passing a block to #wait_until:

# poll for ~25 seconds
resource.wait_until(max_attempts:5,delay:5) {|resource|...}

Callbacks

You can be notified before each polling attempt and before each delay. If you throw :success or :failure from these callbacks, it will terminate the waiter.

started_at = Time.now
# poll for 1 hour, instead of a number of attempts
proc = Proc.new do |attempts, response|
  throw :failure if Time.now - started_at > 3600
end

  # disable max attempts
instance.wait_until(before_wait:proc, max_attempts:nil) {...}

Handling Errors

When a waiter is successful, it returns the Resource. When a waiter fails, it raises an error.

begin
  resource.wait_until(...)
rescue Aws::Waiters::Errors::WaiterFailed
  # resource did not enter the desired state in time
end

attempts attempt in seconds invoked before each attempt invoked before each wait

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :max_attempts (Integer) — default: 10

    Maximum number of

  • :delay (Integer) — default: 10

    Delay between each

  • :before_attempt (Proc) — default: nil

    Callback

  • :before_wait (Proc) — default: nil

    Callback

Returns:

  • (Resource)

    if the waiter was successful

Raises:

  • (Aws::Waiters::Errors::FailureStateError)

    Raised when the waiter terminates because the waiter has entered a state that it will not transition out of, preventing success.

    yet successful.

  • (Aws::Waiters::Errors::UnexpectedError)

    Raised when an error is encountered while polling for a resource that is not expected.

  • (NotImplementedError)

    Raised when the resource does not



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 260

def wait_until(options = {}, &block)
  self_copy = self.dup
  attempts = 0
  options[:max_attempts] = 10 unless options.key?(:max_attempts)
  options[:delay] ||= 10
  options[:poller] = Proc.new do
    attempts += 1
    if block.call(self_copy)
      [:success, self_copy]
    else
      self_copy.reload unless attempts == options[:max_attempts]
      :retry
    end
  end
  Aws::Waiters::Waiter.new(options).wait({})
end

#wait_until_completed(options = {}) ⇒ Snapshot

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :max_attempts (Integer) — default: 40
  • :delay (Float) — default: 15
  • :before_attempt (Proc)
  • :before_wait (Proc)

Returns:



170
171
172
173
174
175
176
177
178
179
180
# File 'gems/aws-sdk-ec2/lib/aws-sdk-ec2/snapshot.rb', line 170

def wait_until_completed(options = {})
  options, params = separate_params_and_options(options)
  waiter = Waiters::SnapshotCompleted.new(options)
  yield_waiter_and_warn(waiter, &Proc.new) if block_given?
  resp = waiter.wait(params.merge(snapshot_ids: [@id]))
  Snapshot.new({
    id: @id,
    data: resp.data.snapshots[],
    client: @client
  })
end