From 4b9b047878f20db9b5a37d2e5b0e98bf43ff312f Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Thu, 26 Feb 2015 15:38:42 -0800 Subject: [PATCH] Use auto scaling group managed ELBs if present. --- cloud/amazon/ec2_elb.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cloud/amazon/ec2_elb.py b/cloud/amazon/ec2_elb.py index 11abd827b2b..396f9ab0c9b 100644 --- a/cloud/amazon/ec2_elb.py +++ b/cloud/amazon/ec2_elb.py @@ -105,6 +105,7 @@ import os try: import boto import boto.ec2 + import boto.ec2.autoscale import boto.ec2.elb from boto.regioninfo import RegionInfo except ImportError: @@ -255,6 +256,9 @@ class ElbManager: for elb lookup instead of returning what elbs are attached to self.instance_id""" + if not ec2_elbs: + ec2_elbs = self._get_auto_scaling_group_lbs() + try: elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) @@ -273,6 +277,32 @@ class ElbManager: lbs.append(lb) return lbs + def _get_auto_scaling_group_lbs(self): + """Returns a list of ELBs associated with self.instance_id + indirectly through its auto scaling group membership""" + + try: + asg = connect_to_aws(boto.ec2.autoscale, self.region, **self.aws_connect_params) + except (boto.exception.NoAuthHandlerFound, StandardError), e: + self.module.fail_json(msg=str(e)) + + asg_instances = asg.get_all_autoscaling_instances([self.instance_id]) + if len(asg_instances) > 1: + self.module.fail_json(msg="Illegal state, expected one auto scaling group instance.") + + if not asg_instances: + asg_elbs = [] + else: + asg_name = asg_instances[0].group_name + + asgs = asg.get_all_groups([asg_name]) + if len(asg_instances) != 1: + self.module.fail_json(msg="Illegal state, expected one auto scaling group.") + + asg_elbs = asgs[0].load_balancers + + return asg_elbs + def _get_instance(self): """Returns a boto.ec2.InstanceObject for self.instance_id""" try: