Class: AwsHelpers::AutoScalingGroup::UpdateDesiredCapacity

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_helpers/auto_scaling_group/update_desired_capacity.rb

Instance Method Summary collapse

Constructor Details

#initialize(auto_scaling_client, elastic_load_balancing_client, auto_scaling_group_name, desired_capacity, timeout) ⇒ UpdateDesiredCapacity


8
9
10
11
12
13
14
# File 'lib/aws_helpers/auto_scaling_group/update_desired_capacity.rb', line 8

def initialize(auto_scaling_client, elastic_load_balancing_client, auto_scaling_group_name, desired_capacity, timeout)
  @auto_scaling_client = auto_scaling_client
  @elastic_load_balancing_client = elastic_load_balancing_client
  @auto_scaling_group_name = auto_scaling_group_name
  @desired_capacity = desired_capacity
  @timeout = timeout
end

Instance Method Details

#executeObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aws_helpers/auto_scaling_group/update_desired_capacity.rb', line 16

def execute
  puts "Setting #{@auto_scaling_group_name} desired capacity to #{@desired_capacity}"

  begin
    @auto_scaling_client.suspend_processes(
      auto_scaling_group_name: @auto_scaling_group_name,
      scaling_processes: ['AlarmNotification']
    )
    @auto_scaling_client.set_desired_capacity(
      auto_scaling_group_name: @auto_scaling_group_name,
      desired_capacity: @desired_capacity)
    auto_scaling_groups = @auto_scaling_client.describe_auto_scaling_groups(auto_scaling_group_names: [@auto_scaling_group_name])[:auto_scaling_groups]
    load_balancer_names = auto_scaling_groups.detect { |auto_scaling_group| auto_scaling_group[:auto_scaling_group_name] == @auto_scaling_group_name }[:load_balancer_names]
    load_balancer_names.each { |load_balancer_name|
      ElasticLoadBalancing::PollHealthyInstances.new(@elastic_load_balancing_client, load_balancer_name, @desired_capacity, @timeout).execute
    }
  ensure
    @auto_scaling_client.resume_processes(auto_scaling_group_name: @auto_scaling_group_name)
  end


end