Class: RightAws::ElbInterface

Inherits:
RightAwsBase show all
Includes:
RightAwsBaseInterface
Defined in:
lib/elb/right_elb_interface.rb

Overview

RightAWS::ElbInterface – RightScale Amazon Elastic Load Balancer interface

The RightAws::ElbInterface class provides a complete interface to Amazon’s Elastic Load Balancer service.

For explanations of the semantics of each call, please refer to Amazon’s documentation at docs.amazonwebservices.com/ElasticLoadBalancing/latest/DeveloperGuide/

Create an interface handle:

elb = RightAws::ElbInterface.new(aws_access_key_id, aws_security_access_key)

Create an new load balancer:

elb.create_load_balancer( 'test-kd1',
                         ['us-east-1a', 'us-east-1b'],
                         [ { :protocol => :http, :load_balancer_port => 80,  :instance_port => 80 },

{ :protocol => :tcp, :load_balancer_port => 443, :instance_port => 443 } ])

Configure its health checking:

elb.configure_health_check( 'test-kd1',
                            { :healthy_threshold => 9,
                              :unhealthy_threshold => 3,
                              :target => "TCP:433",
                              :timeout => 6,
                              :interval => 31}

Register instances with the balancer:

elb.register_instances_with_load_balancer('test-kd1', 'i-8b8bcbe2', 'i-bf8bcbd6') #=> ["i-8b8bcbe2", "i-bf8bcbd6"]

Add new availability zones:

elb.enable_availability_zones_for_load_balancer("test-kd1", "us-east-1c")

Defined Under Namespace

Classes: AvailabilityZonesForLoadBalancerParser, CreateLoadBalancerParser, DeleteLoadBalancerParser, DescribeInstanceHealthParser, DescribeLoadBalancersParser, HealthCheckParser, InstancesWithLoadBalancerParser

Constant Summary collapse

API_VERSION =

Amazon ELB API version being used

"2009-05-15"
DEFAULT_HOST =
"elasticloadbalancing.amazonaws.com"
DEFAULT_PATH =
'/'
DEFAULT_PROTOCOL =
'https'
DEFAULT_PORT =
443
@@bench =
AwsBenchmarkingBlock.new

Constants included from RightAwsBaseInterface

RightAwsBaseInterface::DEFAULT_SIGNATURE_VERSION

Constants inherited from RightAwsBase

RightAwsBase::AMAZON_PROBLEMS

Instance Attribute Summary

Attributes included from RightAwsBaseInterface

#aws_access_key_id, #cache, #connection, #last_errors, #last_request, #last_request_id, #last_response, #logger, #params, #signature_version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RightAwsBaseInterface

#amazonize_list, #cache_hits?, caching, caching=, #caching?, #generate_request_impl, #get_connection, #init, #on_exception, #request_cache_or_info, #request_info_impl, #signed_service_params, #update_cache

Methods inherited from RightAwsBase

amazon_problems, amazon_problems=

Constructor Details

#initialize(aws_access_key_id = nil, aws_secret_access_key = nil, params = {}) ⇒ ElbInterface

Create a new handle to an ELB account. All handles share the same per process or per thread HTTP connection to Amazon ELB. Each handle is for a specific account. The params have the following options:

  • :endpoint_url a fully qualified url to Amazon API endpoint (this overwrites: :server, :port, :service, :protocol). Example: ‘elasticloadbalancing.amazonaws.com

  • :server: ELB service host, default: DEFAULT_HOST

  • :port: ELB service port, default: DEFAULT_PORT

  • :protocol: ‘http’ or ‘https’, default: DEFAULT_PROTOCOL

  • :multi_thread: true=HTTP connection per thread, false=per process

  • :logger: for log messages, default: RAILS_DEFAULT_LOGGER else STDOUT

  • :signature_version: The signature version : ‘0’,‘1’ or ‘2’(default)

  • :cache: true/false(default): caching works for: describe_load_balancers



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/elb/right_elb_interface.rb', line 91

def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
  init({ :name                => 'ELB',
         :default_host        => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).host   : DEFAULT_HOST,
         :default_port        => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).port   : DEFAULT_PORT,
         :default_service     => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).path   : DEFAULT_PATH,
         :default_protocol    => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).scheme : DEFAULT_PROTOCOL,
         :default_api_version => ENV['ELB_API_VERSION'] || API_VERSION },
       aws_access_key_id    || ENV['AWS_ACCESS_KEY_ID'] ,
       aws_secret_access_key|| ENV['AWS_SECRET_ACCESS_KEY'],
       params)
end

Class Method Details

.bench_serviceObject



75
76
77
# File 'lib/elb/right_elb_interface.rb', line 75

def self.bench_service
  @@bench.service
end

.bench_xmlObject



72
73
74
# File 'lib/elb/right_elb_interface.rb', line 72

def self.bench_xml
  @@bench.xml
end

Instance Method Details

#configure_health_check(load_balancer_name, health_check) ⇒ Object

Define an application healthcheck for the instances. Returns an updated health check configuration for the load balancer.

hc = elb.configure_health_check( 'test-kd1',
                                 { :healthy_threshold => 9,
                                   :unhealthy_threshold => 3,
                                   :target => "TCP:433",
                                   :timeout => 6,
                                   :interval => 31}
pp hc #=> { :target=>"TCP:433", :timeout=>6, :interval=>31, :healthy_threshold=>9, :unhealthy_threshold=>3 }


222
223
224
225
226
227
# File 'lib/elb/right_elb_interface.rb', line 222

def configure_health_check(load_balancer_name, health_check)
  request_hash = { 'LoadBalancerName' => load_balancer_name }
  health_check.each{ |key, value| request_hash["HealthCheck.#{key.to_s.camelize}"] = value }
  link = generate_request("ConfigureHealthCheck", request_hash)
  request_info(link, HealthCheckParser.new(:logger => @logger))
end

#create_load_balancer(load_balancer_name, availability_zones = [], listeners = []) ⇒ Object

puts lb #=> “test-kd1-1519253964.us-east-1.elb.amazonaws.com”



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/elb/right_elb_interface.rb', line 151

def create_load_balancer(load_balancer_name, availability_zones=[], listeners=[])
  request_hash = { 'LoadBalancerName' => load_balancer_name }
  # merge zones
  request_hash.merge!( amazonize_list("AvailabilityZones.member", availability_zones.to_a) )
  # merge listeners
  if listeners.blank?
    listeners = { :protocol           => :http,
                  :load_balancer_port => 80,
                  :instance_port      => 80 }
  end
  listeners = [listeners] unless listeners.is_a?(Array)
  request_hash.merge!( amazonize_list( ['Listeners.member.?.Protocol',
                                        'Listeners.member.?.LoadBalancerPort',
                                        'Listeners.member.?.InstancePort'],
                                         listeners.map{|i| [ (i[:protocol] || 'HTTP').to_s.upcase,
                                                             (i[:load_balancer_port] || 80),
                                                             (i[:instance_port] || 80) ] } ) )
  link = generate_request("CreateLoadBalancer", request_hash)
  request_info(link, CreateLoadBalancerParser.new(:logger => @logger))
end

#delete_load_balancer(load_balancer_name) ⇒ Object

Delete load balancer. Returns true on success.

elb.delete_load_balancer('test-kd1') #=> true

Amazon: Because this API has been designed to be idempotent, even if the LoadBalancer does not exist or has been deleted, DeleteLoadBalancer still returns a success.



180
181
182
183
# File 'lib/elb/right_elb_interface.rb', line 180

def delete_load_balancer(load_balancer_name)
  link = generate_request("DeleteLoadBalancer", 'LoadBalancerName' => load_balancer_name)
  request_info(link, DeleteLoadBalancerParser.new(:logger => @logger))
end

#deregister_instances_with_load_balancer(load_balancer_name, *instances) ⇒ Object

Remove instance(s) from the load balancer. Returns an updated list of instances for the load balancer.

elb.deregister_instances_with_load_balancer('test-kd1', 'i-8b8bcbe2') #=> ["i-bf8bcbd6"]


272
273
274
275
276
277
278
# File 'lib/elb/right_elb_interface.rb', line 272

def deregister_instances_with_load_balancer(load_balancer_name, *instances)
  instances.flatten!
  request_hash = amazonize_list("Instances.member.?.InstanceId", instances)
  request_hash.merge!( 'LoadBalancerName' => load_balancer_name )
  link = generate_request("DeregisterInstancesFromLoadBalancer", request_hash)
  request_info(link, InstancesWithLoadBalancerParser.new(:logger => @logger))
end

#describe_instance_health(load_balancer_name, *instances) ⇒ Object

Describe the current state of the instances of the specified load balancer. Returns a list of the instances.

elb.describe_instance_health('test-kd1', 'i-8b8bcbe2', 'i-bf8bcbd6') #=>
    [ { :description => "Instance registration is still in progress",
        :reason_code => "ELB",
        :instance_id => "i-8b8bcbe2",
        :state       => "OutOfService" },
      { :description => "Instance has failed at least the UnhealthyThreshold number of health checks consecutively.",
        :reason_code => "Instance",
        :instance_id => "i-bf8bcbd6",
        :state       => "OutOfService" } ]


246
247
248
249
250
251
252
# File 'lib/elb/right_elb_interface.rb', line 246

def describe_instance_health(load_balancer_name, *instances)
  instances.flatten!
  request_hash = amazonize_list("Instances.member.?.InstanceId", instances)
  request_hash.merge!( 'LoadBalancerName' => load_balancer_name )
  link = generate_request("DescribeInstanceHealth", request_hash)
  request_info(link, DescribeInstanceHealthParser.new(:logger => @logger))
end

#describe_load_balancers(*load_balancers) ⇒ Object


Load Balancers

Describe load balancers. Returns an array of load balancers.

elb.describe_load_balancers #=>
  [ { :health_check =>
      { :healthy_threshold => 10,
        :unhealthy_threshold => 2,
        :target => "TCP:80",
        :timeout => 5,
        :interval => 30},
      :load_balancer_name => "test-kd1",
      :availability_zones => ["us-east-1a", "us-east-1b"],
      :listeners =>
       [ { :protocol => "HTTP", :load_balancer_port => "80",  :instance_port => "80" },
         { :protocol => "TCP",  :load_balancer_port => "443", :instance_port => "443" } ],
      :created_time => Wed May 27 11:59:11 UTC 2009,
      :dns_name => "test-kd1-1519253964.us-east-1.elb.amazonaws.com",
      :instances => [] } ]


135
136
137
138
139
140
# File 'lib/elb/right_elb_interface.rb', line 135

def describe_load_balancers(*load_balancers)
  load_balancers = load_balancers.flatten.compact
  request_hash = amazonize_list("LoadBalancerNames.member", load_balancers)
  link = generate_request("DescribeLoadBalancers", request_hash)
  request_cache_or_info(:describe_load_balancers, link,  DescribeLoadBalancersParser, @@bench, load_balancers.blank?)
end

#disable_availability_zones_for_load_balancer(load_balancer_name, *availability_zones) ⇒ Object

Remove one or more zones from a load balancer. Returns a list of updated availability zones for the load balancer.

elb.disable_availability_zones_for_load_balancer("test-kd1", "us-east-1c") #=> ["us-east-1a"]


203
204
205
206
207
208
209
# File 'lib/elb/right_elb_interface.rb', line 203

def disable_availability_zones_for_load_balancer(load_balancer_name, *availability_zones)
  availability_zones.flatten!
  request_hash = amazonize_list("AvailabilityZones.member", availability_zones)
  request_hash.merge!( 'LoadBalancerName' => load_balancer_name )
  link = generate_request("DisableAvailabilityZonesForLoadBalancer", request_hash)
  request_info(link, AvailabilityZonesForLoadBalancerParser.new(:logger => @logger))
end

#enable_availability_zones_for_load_balancer(load_balancer_name, *availability_zones) ⇒ Object

Add one or more zones to a load balancer. Returns a list of updated availability zones for the load balancer.

elb.enable_availability_zones_for_load_balancer("test-kd1", "us-east-1c") #=> ["us-east-1a", "us-east-1c"]


190
191
192
193
194
195
196
# File 'lib/elb/right_elb_interface.rb', line 190

def enable_availability_zones_for_load_balancer(load_balancer_name, *availability_zones)
  availability_zones.flatten!
  request_hash = amazonize_list("AvailabilityZones.member", availability_zones)
  request_hash.merge!( 'LoadBalancerName' => load_balancer_name )
  link = generate_request("EnableAvailabilityZonesForLoadBalancer", request_hash)
  request_info(link, AvailabilityZonesForLoadBalancerParser.new(:logger => @logger))
end

#generate_request(action, params = {}) ⇒ Object

:nodoc:



103
104
105
# File 'lib/elb/right_elb_interface.rb', line 103

def generate_request(action, params={}) #:nodoc:
  generate_request_impl(:get, action, params )
end

#register_instances_with_load_balancer(load_balancer_name, *instances) ⇒ Object

Add new instance(s) to the load balancer. Returns an updated list of instances for the load balancer.

elb.register_instances_with_load_balancer('test-kd1', 'i-8b8bcbe2', 'i-bf8bcbd6') #=> ["i-8b8bcbe2", "i-bf8bcbd6"]


259
260
261
262
263
264
265
# File 'lib/elb/right_elb_interface.rb', line 259

def register_instances_with_load_balancer(load_balancer_name, *instances)
  instances.flatten!
  request_hash = amazonize_list("Instances.member.?.InstanceId", instances)
  request_hash.merge!( 'LoadBalancerName' => load_balancer_name )
  link = generate_request("RegisterInstancesWithLoadBalancer", request_hash)
  request_info(link, InstancesWithLoadBalancerParser.new(:logger => @logger))
end

#request_info(request, parser) ⇒ Object

Sends request to Amazon and parses the response Raises AwsError if any banana happened



109
110
111
# File 'lib/elb/right_elb_interface.rb', line 109

def request_info(request, parser)  #:nodoc:
  request_info_impl(:lbs_connection, @@bench, request, parser)
end