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

Class: Aws::AutoScaling::ScalingPolicy

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

Defined Under Namespace

Classes: Collection

Actions collapse

Associations collapse

Read-Only Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ScalingPolicy #initialize(options = {}) ⇒ ScalingPolicy

Returns a new instance of ScalingPolicy

Overloads:

  • #initialize(name, options = {}) ⇒ ScalingPolicy

    Parameters:

    • name (String)

    Options Hash (options):

  • #initialize(options = {}) ⇒ ScalingPolicy

    Options Hash (options):

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


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

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

Instance Method Details

#adjustment_typeString

The adjustment type, which specifies how ScalingAdjustment is interpreted. Valid values are ChangeInCapacity, ExactCapacity, and PercentChangeInCapacity.

Returns:

  • (String)


56
57
58
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 56

def adjustment_type
  data[:adjustment_type]
end

#alarmsArray<Types::Alarm>

The CloudWatch alarms related to the policy.

Returns:



114
115
116
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 114

def alarms
  data[:alarms]
end

#auto_scaling_group_nameString

The name of the Auto Scaling group.

Returns:

  • (String)


36
37
38
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 36

def auto_scaling_group_name
  data[:auto_scaling_group_name]
end

#clientClient

Returns:



127
128
129
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 127

def client
  @client
end

#cooldownInteger

The amount of time, in seconds, after a scaling activity completes before any further dynamic scaling activities can start.

Returns:

  • (Integer)


87
88
89
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 87

def cooldown
  data[:cooldown]
end

#dataTypes::ScalingPolicy

Returns the data for this Aws::AutoScaling::ScalingPolicy. Calls Client#describe_policies if #data_loaded? is false.

Returns:



147
148
149
150
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 147

def data
  load unless @data
  @data
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.



155
156
157
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 155

def data_loaded?
  !!@data
end

#delete(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


scaling_policy.delete({
  auto_scaling_group_name: "ResourceName",
})

Parameters:

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

    ({})

Options Hash (options):

  • :auto_scaling_group_name (String)

    The name of the Auto Scaling group.

Returns:

  • (EmptyStructure)


265
266
267
268
269
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 265

def delete(options = {})
  options = options.merge(policy_name: @name)
  resp = @client.delete_policy(options)
  resp.data
end

#estimated_instance_warmupInteger

The estimated time, in seconds, until a newly launched instance can contribute to the CloudWatch metrics.

Returns:

  • (Integer)


108
109
110
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 108

def estimated_instance_warmup
  data[:estimated_instance_warmup]
end

#execute(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


scaling_policy.execute({
  auto_scaling_group_name: "ResourceName",
  honor_cooldown: false,
  metric_value: 1.0,
  breach_threshold: 1.0,
})

Parameters:

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

    ({})

Options Hash (options):

  • :auto_scaling_group_name (String)

    The name of the Auto Scaling group.

  • :honor_cooldown (Boolean)

    If this parameter is true, Auto Scaling waits for the cooldown period to complete before executing the policy. Otherwise, Auto Scaling executes the policy without waiting for the cooldown period to complete.

    This parameter is not supported if the policy type is StepScaling.

    For more information, see Auto Scaling Cooldowns in the Auto Scaling User Guide.

  • :metric_value (Float)

    The metric value to compare to BreachThreshold. This enables you to execute a policy of type StepScaling and determine which step adjustment to use. For example, if the breach threshold is 50 and you want to use a step adjustment with a lower bound of 0 and an upper bound of 10, you can set the metric value to 59.

    If you specify a metric value that doesn't correspond to a step adjustment for the policy, the call returns an error.

    This parameter is required if the policy type is StepScaling and not supported otherwise.

  • :breach_threshold (Float)

    The breach threshold for the alarm.

    This parameter is required if the policy type is StepScaling and not supported otherwise.

Returns:

  • (EmptyStructure)


314
315
316
317
318
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 314

def execute(options = {})
  options = options.merge(policy_name: @name)
  resp = @client.execute_policy(options)
  resp.data
end

#groupAutoScalingGroup?

Returns:



323
324
325
326
327
328
329
330
331
332
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 323

def group
  if data[:auto_scaling_group_name]
    AutoScalingGroup.new(
      name: data[:auto_scaling_group_name],
      client: @client
    )
  else
    nil
  end
end

#loadself Also known as: reload

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

scaling_policy.reload.data

Returns:

  • (self)


137
138
139
140
141
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 137

def load
  resp = @client.describe_policies(policy_names: [@name])
  @data = resp.scaling_policies[0]
  self
end

#metric_aggregation_typeString

The aggregation type for the CloudWatch metrics. Valid values are Minimum, Maximum, and Average.

Returns:

  • (String)


101
102
103
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 101

def metric_aggregation_type
  data[:metric_aggregation_type]
end

#min_adjustment_magnitudeInteger

The minimum number of instances to scale. If the value of AdjustmentType is PercentChangeInCapacity, the scaling policy changes the DesiredCapacity of the Auto Scaling group by at least this many instances. Otherwise, the error is ValidationError.

Returns:

  • (Integer)


72
73
74
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 72

def min_adjustment_magnitude
  data[:min_adjustment_magnitude]
end

#min_adjustment_stepInteger

Available for backward compatibility. Use MinAdjustmentMagnitude instead.

Returns:

  • (Integer)


63
64
65
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 63

def min_adjustment_step
  data[:min_adjustment_step]
end

#nameString Also known as: policy_name

Returns:

  • (String)


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

def name
  @name
end

#policy_arnString

The Amazon Resource Name (ARN) of the policy.

Returns:

  • (String)


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

def policy_arn
  data[:policy_arn]
end

#policy_typeString

The policy type. Valid values are SimpleScaling and StepScaling.

Returns:

  • (String)


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

def policy_type
  data[:policy_type]
end

#scaling_adjustmentInteger

The amount by which to scale, based on the specified adjustment type. A positive value adds to the current capacity while a negative number removes from the current capacity.

Returns:

  • (Integer)


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

def scaling_adjustment
  data[:scaling_adjustment]
end

#step_adjustmentsArray<Types::StepAdjustment>

A set of adjustments that enable you to scale based on the size of the alarm breach.

Returns:



94
95
96
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 94

def step_adjustments
  data[:step_adjustments]
end

#target_tracking_configurationTypes::TargetTrackingConfiguration

A target tracking policy.



120
121
122
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 120

def target_tracking_configuration
  data[:target_tracking_configuration]
end

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

Deprecated.

Use [Aws::AutoScaling::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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'gems/aws-sdk-autoscaling/lib/aws-sdk-autoscaling/scaling_policy.rb', line 237

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