Skip to content

Instantly share code, notes, and snippets.

@bahoo
Created December 8, 2016 03:54
Show Gist options
  • Save bahoo/4c71f63a289c14bbb13f216ff1d5cee0 to your computer and use it in GitHub Desktop.
Save bahoo/4c71f63a289c14bbb13f216ff1d5cee0 to your computer and use it in GitHub Desktop.
AWS Application Load Balancer-friendly Fabric Deploys using boto3
from awsfabrictasks.ec2.api import *
from fabric.api import *
from functools import wraps
import boto3
import time
def aws_hosts(load_balancers):
instance_ids = []
session = boto3.Session(profile_name=env.aws_profile_name)
alb_client = session.client('elbv2')
load_balancers = alb_client.describe_load_balancers(Names=env.load_balancer_name)
load_balancer_arn = load_balancers['LoadBalancers'][0]['LoadBalancerArn']
target_groups = alb_client.describe_target_groups(LoadBalancerArn=load_balancer_arn)
target_group_arn = target_groups['TargetGroups'][0]['TargetGroupArn']
target_health = alb_client.describe_target_health(TargetGroupArn=target_group_arn)
instance_ids = map(lambda t: t['Target']['Id'], target_health['TargetHealthDescriptions'])
if not instance_ids:
raise ValueError('No instances found! I\'m getting out of here.')
ec2_client = session.client('ec2')
reservations = ec2_client.describe_instances(InstanceIds=instance_ids)
ec2_hosts = {}
for reservation in reservations['Reservations']:
for i in reservation['Instances']:
ec2_hosts[i['PublicDnsName']] = i['InstanceId']
return ec2_hosts
def elb_operation(operation, instance_id, load_balancer_names):
session = boto3.Session(profile_name=env.aws_profile_name)
alb_client = session.client('elbv2')
load_balancers = alb_client.describe_load_balancers(Names=load_balancer_names)
load_balancer_arn = load_balancers['LoadBalancers'][0]['LoadBalancerArn']
target_groups = alb_client.describe_target_groups(LoadBalancerArn=load_balancer_arn)
target_group_arn = target_groups['TargetGroups'][0]['TargetGroupArn']
return getattr(alb_client, operation)(TargetGroupArn=target_group_arn, Targets=[{'Id': instance_id}])
def remove_from_elbs():
print "Removing %s from the load balancer..." % env.aws_hosts[env.host]
elb_operation('deregister_targets', env.aws_hosts[env.host], env.load_balancer_name)
time.sleep(5) # allow connections to fully drain
def add_to_elbs():
print "Adding %s back to the load balancer..." % env.aws_hosts[env.host]
elb_operation('register_targets', env.aws_hosts[env.host], env.load_balancer_name)
time.sleep(15) # generous to prevent cascading effect of removing machines from ELB
def elb_managed(func):
@wraps(func)
def decorated(*args, **kwargs):
if env.load_balancer_name:
remove_from_elbs()
func(*args, **kwargs)
if env.load_balancer_name:
add_to_elbs()
return decorated
def production():
env.load_balancer_name = ['your-load-balancer-name']
env.aws_profile_name = 'your-aws-profile-name'
env.aws_hosts = aws_hosts(env.load_balancer_name)
env.hosts = env.aws_hosts.keys()
@elb_managed
def deploy():
# ???
# profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment