from libcloud.loadbalancer.types import Provider
from libcloud.loadbalancer.providers import get_driver
cls = get_driver(Provider.ELB)
driver = cls(aws_access_key_id='your_access_key_id', aws_secret_access_key='your_secret_access_key', region='your_region')
name = 'MyBalancer'
protocol = 'http'
port = 80
members = [('<backend_ip1>', port), ('<backend_ip2>', port), ('<backend_ip3>', port)]
balancer = driver.create_balancer(name, protocol, port, members=members)
algorithm = 'round-robin'
balancer.balancer_algorithm = algorithm
hc_interval = 10
hc_timeout = 5
hc_healthy_threshold = 3
hc_unhealthy_threshold = 5
balancer.set_healthcheck(hc_interval, hc_timeout, hc_healthy_threshold, hc_unhealthy_threshold)
all_members = balancer.balancer_members
for member in all_members:
if member.state == 'ENABLED':
member.service.down()
if load > threshold:
ip_address = '<new_backend_ip>'
port = 80
balancer.attach_member(ip_address, port)
if load < threshold:
member_to_remove = '<backend_member_to_remove>'
balancer.detach_member(member_to_remove)