コード例 #1
0
ファイル: scaler.py プロジェクト: miraculixx/ec2autoscaler
 def scale_up(self):
     '''
     Find a stopped instance that can be started, i.e. any instance
     that matches the KAS_KEYFILTER and is either stopping or
     stopped. Only starts one instance at a time.
     '''
     try:
         mgr = KooabaWorkerManager()
         list = mgr.get_instances(key_name=os.environ['KAS_KEYFILTER'])
         for id in list:
             inst = list[id]
             if inst.state == 'stopped':
                 note('Trying to start instance %s', inst.id)
                 inst.start()
                 note('Adding instance %s to load balancer', inst.id)
                 elb = self.connect_elb()
                 elb.register_instances(os.environ['KAS_KEY'], [inst.id])
                 # FIXME not a good idea, it resets the manual triggers
                 # make sure the instance is monitored
                 # self.monitor_instance(inst.id)
                 return inst.id
         # no instance found thus far
         note('No instance available to start')
         return None
     except Exception as e:
         note('Error starting an instance, %s', e)
コード例 #2
0
ファイル: scaler.py プロジェクト: miraculixx/ec2autoscaler
 def scale_up(self):
     '''
     Find a stopped instance that can be started, i.e. any instance
     that matches the KAS_KEYFILTER and is either stopping or
     stopped. Only starts one instance at a time.
     '''
     try:
         mgr = KooabaWorkerManager()
         list = mgr.get_instances(key_name=os.environ['KAS_KEYFILTER'])
         for id in list:
             inst = list[id]
             if inst.state == 'stopped':
                 note('Trying to start instance %s', inst.id)
                 inst.start()
                 note('Adding instance %s to load balancer', inst.id)
                 elb = self.connect_elb()
                 elb.register_instances(os.environ['KAS_KEY'], [inst.id])
                 # FIXME not a good idea, it resets the manual triggers
                 # make sure the instance is monitored
                 # self.monitor_instance(inst.id)
                 return inst.id
         # no instance found thus far
         note('No instance available to start')
         return None
     except Exception as e:
         note('Error starting an instance, %s', e)
コード例 #3
0
ファイル: elb.py プロジェクト: krux/python-krux-boto-elb
 def add_instance(self, instance_id, load_balancer_name):
     """
     Adds the given instance to the ELB with the given name.
     """
     elb = self._get_connection()
     try:
         elb.register_instances(load_balancer_name, [instance_id])
     except boto.exception.BotoServerError:
         trace = sys.exc_info()[2]
         raise ELBInstanceMismatchError(), None, trace
     self._logger.info('Added instance %s to load balancer %s', instance_id, load_balancer_name)
コード例 #4
0
ファイル: test.py プロジェクト: LucaSimonelli/lang_tests
def setup_elb(instance_ids):
    """ Setup the ELB and register the provided EC2 instances """
    if dry_run:
        return

    elb_conn = boto.ec2.elb.connect_to_region(region_name=ec2_region)
    elb = elb_conn.create_load_balancer(elb_name, elb_region,
                                        listeners=elb_listeners,
                                        subnets=elb_subnets,
                                        security_groups=elb_security_groups,
                                        scheme=elb_scheme)
    elb.register_instances(instance_ids)
    print "ELB: name=%s, dns=%s, instances=%s" % (elb.name, elb.dns_name, elb.instances)
コード例 #5
0
ファイル: aws.py プロジェクト: Multifarious/fabulous
def _assign_to_elb_(elb_name = None, nodes = None):
    """Adds nodes to the the Elastic Load Balancer.
    :type: list
    :param: Nodes to assign or None for env.nodes

    :type: str
    :param: DNS name of ELB or None for env.aws_elb_name.
    """
    nodes = nodes or env.nodes
    elb = _find_elb_(elb_name)
    elb_name = elb_name or env.aws_elb_name
    if elb:
        info("Adding %s to ELB %s" % ([pretty_instance(node) for node in nodes], elb_name))
        elb.register_instances([node.id for node in nodes])
コード例 #6
0
    def __init__(self):
        ''' Where the magic begins '''
        self.parse_cli_args()

        #set inventory
        self.group = os.environ['GROUP']
        self.lbname = os.environ['LBNAME']
        self.get_servers()
        
        #its all the same to here.
        elb = boto.ec2.elb.connect_to_region('us-west-2')
        if self.args.dereg:
            for instance in self.hosts['ec2_instances']:
                elb.deregister_instances(self.lbname,instance)
                print('De-Registered '+ instance)
        else:
            for instance in self.hosts['ec2_instances']:
                elb.register_instances(self.lbname,instance)
                print('Registered '+ instance)
コード例 #7
0
def setup_elb():
    """ Setup the ELB and register the previously created EC2 instances """
    if dry_run:
        return

    # Create load balancer
    elb_conn = boto.ec2.elb.connect_to_region(region_name=region)
    # Returns: A ResultSet containing instances of boto.ec2.elb.loadbalancer.LoadBalancer
    #elb_res_set = elb_conn.get_all_load_balancers(load_balancer_names=None)
    elb_res_set = elb_conn.get_all_load_balancers()
    for elb in elb_res_set:
        print "ELB: name=%s, dns=%s, instances=%s" % (elb.name, elb.dns_name, elb.instances)

    elb = elb_conn.create_load_balancer("Mongodb-Internal-ELB", None, #['eu-west-1c'],
                                        listeners=[(27017, 27017, 'TCP')],
                                        subnets=['subnet-96aefbfd'],
                                        security_groups=['sg-2944784c'], # mongodb security group
                                        scheme='internal', #scheme='internet-facing',
                                        complex_listeners=None)
    elb.register_instances(env['instance_ids'])
    print "ELB: name=%s, dns=%s, instances=%s" % (elb.name, elb.dns_name, elb.instances)
コード例 #8
0
ファイル: ELB.py プロジェクト: ECE1779/A1
def add_this_instance_to_elb():
    elb = get_elb()

    # get the aws ec2 instance id for the current machine
    instance_id = boto.utils.get_instance_metadata()['instance-id']

    elb.register_instances(instance_id)

    start = time.time()
    print 'Adding %s to ELB %s' % (instance_id, elb.name)

    timeout = time.time() + 60*5 # 5 minutes
    while True:
        health = elb.get_instance_health([instance_id])[0]
        assert health.instance_id == instance_id
        assert time.time() < timeout
        if health.state == 'InService':
            break
        time.sleep(1)

    print 'Instance %s now successfully InService in ELB %s (took %d seconds)' % (instance_id, elb.name, time.time() - start)
コード例 #9
0
ファイル: loadbalancers.py プロジェクト: crschmidt/aws-tools
def add_instance(lbname, instance_id):
    "Given the name of a load balancer and an instance id, add the instance to the load balancer."
    elb = connect()
    return elb.register_instances(lbname, [instance_id])
コード例 #10
0
ファイル: awscli.py プロジェクト: mcsrainbow/python-demos
def create_instance(region, instance_name, image_id, instance_type, key_name,
                    security_group_ids, subnet_id, private_ip_address,
                    volume_size, volume_type, volume_zone, volume_iops,
                    volume_delete_on_termination, load_balancer_name):
    conn = boto.ec2.connect_to_region(
        region,
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    # check if instance already exists
    existing_reservations = conn.get_all_instances(
        filters={"tag:Name": "{0}".format(instance_name)})
    for existing_reservation in existing_reservations:
        existing_instance = existing_reservation.instances[0]
        if existing_instance:
            print "instance_name: {0} already exists".format(instance_name)
            return False

    # launch instance
    print "1. Launching instance: {0}".format(instance_name)
    reservation = conn.run_instances(
        image_id,
        instance_type=instance_type,
        key_name=key_name,
        security_group_ids=security_group_ids,
        subnet_id=subnet_id,
        private_ip_address=private_ip_address,
        instance_initiated_shutdown_behavior="stop")

    # get instance info
    instance = reservation.instances[0]

    # set instance name
    print """2. Creating tag as instance name: {"Name": %s}""" % (
        instance_name)
    conn.create_tags(instance.id, {"Name": instance_name})
    while instance.state == u'pending':
        time.sleep(10)
        instance.update()
        print "Instance state: {1}".format(instance_name, instance.state)

    if volume_delete_on_termination:
        root_device = instance.root_device_name
        instance.modify_attribute('blockDeviceMapping', {root_device: True})

    # create volume
    if volume_size and volume_type and volume_zone:
        print "3. Creating secondary volume for instance: {0} as {1} {2}G".format(
            instance_name, volume_type, volume_size)
        volume = conn.create_volume(volume_size,
                                    zone=volume_zone,
                                    volume_type=volume_type,
                                    iops=volume_iops)
        while volume.status == u'creating':
            time.sleep(10)
            volume.update()
            print "Volume status: {0}".format(volume.status)

        # attache volume
        volume_device = SECONDARY_VOLUME_DEVICE
        print "4. Attaching volume: {0} to instance: {1} as device: {2}".format(
            volume.id, instance_name, volume_device)
        conn.attach_volume(volume.id, instance.id, volume_device)
        if volume_delete_on_termination:
            instance.modify_attribute('blockDeviceMapping', {volume_device:
                                                             True})

    # register load balancer
    if load_balancer_name:
        elb_conn = boto.ec2.elb.connect_to_region(
            region,
            aws_access_key_id=AWS_ACCESS_KEY_ID,
            aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

        elb = elb_conn.get_all_load_balancers(load_balancer_name)[0]
        print '5. Adding instance: {0} to ELB: {1}'.format(instance_name,
                                                           elb.name)
        elb.register_instances(instance.id)

    return True
コード例 #11
0
ファイル: awscli.py プロジェクト: huynhvanphuc/python-demo
def create_instance(region, instance_name, image_id, instance_type, key_name,
                    security_group_ids, subnet_id, private_ip_address,
                    volume_size, volume_type, volume_zone, volume_iops,
                    volume_delete_on_termination, load_balancer_name):
    conn = boto.ec2.connect_to_region(
        region,
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    # check if instance already exists
    existing_reservations = conn.get_all_instances(
        filters={"tag:Name": "{0}".format(instance_name)})
    for existing_reservation in existing_reservations:
        existing_instance = existing_reservation.instances[0]
        if existing_instance:
            print "instance_name: {0} already exists".format(instance_name)
            return False

    # launch instance
    print "1. Launching instance: {0}".format(instance_name)
    reservation = conn.run_instances(
        image_id,
        instance_type=instance_type,
        key_name=key_name,
        security_group_ids=security_group_ids,
        subnet_id=subnet_id,
        private_ip_address=private_ip_address,
        instance_initiated_shutdown_behavior="stop")

    # get instance info
    instance = reservation.instances[0]

    # set instance name
    print """2. Creating tag as instance name: {"Name": %s}""" % (
        instance_name)
    conn.create_tags(instance.id, {"Name": instance_name})
    while instance.state == u'pending':
        time.sleep(10)
        instance.update()
        print "Instance state: {1}".format(instance_name, instance.state)

    if volume_delete_on_termination:
        root_device = instance.root_device_name
        instance.modify_attribute('blockDeviceMapping', {root_device: True})

    # create volume
    if volume_size and volume_type and volume_zone:
        print "3. Creating secondary volume for instance: {0} as {1} {2}G".format(
            instance_name, volume_type, volume_size)
        volume = conn.create_volume(volume_size,
                                    zone=volume_zone,
                                    volume_type=volume_type,
                                    iops=volume_iops)
        while volume.status == u'creating':
            time.sleep(10)
            volume.update()
            print "Volume status: {0}".format(volume.status)

        # attache volume
        volume_device = SECONDARY_VOLUME_DEVICE
        print "4. Attaching volume: {0} to instance: {1} as device: {2}".format(
            volume.id, instance_name, volume_device)
        conn.attach_volume(volume.id, instance.id, volume_device)
        if volume_delete_on_termination:
            instance.modify_attribute('blockDeviceMapping',
                                      {volume_device: True})

    # register load balancer
    if load_balancer_name:
        elb_conn = boto.ec2.elb.connect_to_region(
            region,
            aws_access_key_id=AWS_ACCESS_KEY_ID,
            aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

        elb = elb_conn.get_all_load_balancers(load_balancer_name)[0]
        print '5. Adding instance: {0} to ELB: {1}'.format(
            instance_name, elb.name)
        elb.register_instances(instance.id)

    return True
コード例 #12
0
ファイル: loadbalancers.py プロジェクト: crschmidt/aws-tools
def add_instance(lbname, instance_id):
    "Given the name of a load balancer and an instance id, add the instance to the load balancer."
    elb = connect()
    return elb.register_instances(lbname, [instance_id])
コード例 #13
0
ファイル: aws_ami.py プロジェクト: naik1985/repos
 def register_instances_to_elb(self, elb, instance_list):
    return elb.register_instances(instance_list)
コード例 #14
0
ファイル: elbadd.py プロジェクト: Jayaradha/ansible-scripts
def add_this_instance_to_elb():
    elb = get_elb()
    elb.register_instances(sys.argv[1])
    print 'adding %s from ELB %s' % (sys.argv[1], elb.name)
コード例 #15
0
def modify(name,
           region,
           zones,
           listeners=None,
           subnets=None,
           security_groups=None,
           health_check=None,
           policies=None,
           instances=None,
           test=False,
           aws_key=None,
           aws_secret=None,
           elbconn=None,
           elb=None):
    """
    Launch an ELB

    Most arguments are the same as :meth:`.launch_or_modify`

    Parameters
    ----------
    elbconn : :class:`boto.ec2.elb.ELBConnection`, optional
        If present this function will not open a new elb connection
    elb : :class:`boto.ec2.elb.LoadBalancer`, optional
        If present this function will not make a call to fetch the load
        balancer

    """
    if elbconn is None:
        elbconn = __salt__['aws_util.elbconn'](region, aws_key, aws_secret)

    if elb is None:
        elbs = elbconn.get_all_load_balancers(load_balancer_names=[name])
        elb = elbs[0]

    changes = {}

    # Availability Zones
    if zones != elb.availability_zones:
        to_remove = set(elb.availability_zones) - set(zones)
        if to_remove:
            if not test:
                elb.disable_zones(list(to_remove))
            for zone in to_remove:
                changes['Zone {0}'.format(zone)] = "Disabled"
        to_add = set(zones) - set(elb.availability_zones)
        if to_add:
            if not test:
                elb.enable_zones(list(to_add))
            for zone in to_add:
                changes['Zone {0}'.format(zone)] = "Enabled"

    # Listeners
    elb_listeners = []
    # Convert the boto listeners to tuples
    for listener in elb.listeners:
        tup_listener = (
            listener.load_balancer_port,
            listener.instance_port,
            listener.protocol,
        )
        if listener.instance_protocol is not None:
            tup_listener += (listener.instance_protocol, )
        if listener.ssl_certificate_id is not None:
            tup_listener += (listener.ssl_certificate_id, )
        elb_listeners.append(tup_listener)
    listeners = listeners or []

    # Translate all protocols to uppercase
    for listener in listeners:
        listener[2] = listener[2].upper()
        listener[3] = listener[3].upper()

    listeners = [tuple(l) for l in listeners]
    if listeners != elb_listeners:
        to_delete = set(elb_listeners) - set(listeners)
        if to_delete:
            if not test:
                for listener in to_delete:
                    elb.delete_listener(listener[0])
            changes['Listeners Deleted'] = str(list(to_delete))
        to_create = set(listeners) - set(elb_listeners)
        if to_create:
            if not test:
                elbconn.create_load_balancer_listeners(
                    elb.name, complex_listeners=list(to_create))
            changes['Listeners Created'] = str(list(to_create))

    # Subnets
    # TODO: This is untested because my account doesn't have VPC
    elb_subnets = list(elb.subnets)
    subnets = subnets or []
    if subnets != elb_subnets:
        to_detach = set(elb_subnets) - set(subnets)
        if to_detach:
            if not test:
                elb.detach_subnets(list(to_detach))
            changes['Subnets Detached'] = str(list(to_detach))
        to_attach = set(subnets) - set(elb_subnets)
        if to_attach:
            if not test:
                elb.attach_subnets(list(to_attach))
            changes['Subnets Attached'] = str(list(to_attach))

    # Security Groups
    # TODO: This is untested because my account doesn't have VPC
    elb_security_groups = list(elb.security_groups)
    security_groups = security_groups or []
    if security_groups != elb_security_groups:
        to_remove = set(elb_security_groups) - set(security_groups)
        if to_remove:
            changes['Security Groups Removed'] = str(list(to_remove))
        to_add = set(security_groups) - set(elb_security_groups)
        if to_add:
            changes['Security Groups Added'] = str(list(to_add))
        if not test:
            elb.apply_security_groups(security_groups)

    # Health check
    if health_check is None:
        health_check = {}
    check = elb.health_check
    new_check = boto.ec2.elb.healthcheck.HealthCheck(
        access_point=name,
        interval=health_check.get('interval', 30),
        target=health_check.get('target'),
        healthy_threshold=health_check.get('healthy_threshold', 3),
        timeout=health_check.get('timeout', 5),
        unhealthy_threshold=health_check.get('unhealthy_threshold', 5))
    modified = False
    if check is None:
        modified = True
    if not modified:
        for attr in ('interval', 'target', 'healthy_threshold', 'timeout',
                     'unhealthy_threshold'):
            if getattr(new_check, attr) != getattr(check, attr):
                modified = True
                break
    if modified:
        changes['Health check'] = "Modified"
        if not test:
            elb.configure_health_check(new_check)

    # Instances
    if instances is not None:
        _convert_server_names(instances, region, aws_key, aws_secret)

        elb_instances = [i.id for i in elb.instances]
        if instances != elb_instances:
            to_remove = set(elb_instances) - set(instances)
            if to_remove:
                changes['Instances Removed'] = list(to_remove)
                if not test:
                    elb.deregister_instances(list(to_remove))
            to_add = set(instances) - set(elb_instances)
            if to_add:
                changes['Instances Added'] = list(to_add)
                if not test:
                    elb.register_instances(list(to_add))

    # Policies
    policies = policies or {}
    elb_policies = []
    for attr in ('app_cookie_stickiness_policies',
                 'lb_cookie_stickiness_policies'):
        for policy in getattr(elb.policies, attr):
            elb_policies.append(policy.policy_name)

    # Create any policies that don't exist yet
    for policy in policies.itervalues():
        if policy['type'] == 'lb':
            policy['name'] = 'lb' + str(policy['cookie_expire'])
        elif policy['type'] == 'app':
            policy['name'] = 'app' + str(hash(policy['cookie_name']))
        else:
            raise ValueError("Policy type '{0}' must be 'lb' or 'app'".format(
                policy['name']))

        if policy['name'] not in elb_policies:
            if not test:
                if policy['type'] == 'lb':
                    elb.create_cookie_stickiness_policy(
                        policy['cookie_expire'], policy['name'])
                elif policy['type'] == 'app':
                    elb.create_app_cookie_stickiness_policy(
                        policy['cookie_name'], policy['name'])

    for listener in elb.listeners:
        if listener.load_balancer_port in policies:
            port = listener.load_balancer_port
            policy = policies[port]
            if len(listener.policy_names) == 0:
                if not test:
                    elb.set_policies_of_listener(port, [policy['name']])
                changes['Port {0:d}'.format(port)] = "Added policy"
            else:
                if policy['name'] != listener.policy_names[0]:
                    if not test:
                        elb.set_policies_of_listener(port, [policy['name']])
                    changes['Port {0:d}'.format(port)] = "Changed policy"
        else:
            if len(listener.policy_names) > 0:
                if not test:
                    elb.set_policies_of_listener(port, [])
            changes['Port {0:d}'.format(port)] = "Removed policy"

    return changes
コード例 #16
0
ファイル: elb.py プロジェクト: JJediny/aws-formula
def modify(
    name,
    region,
    zones,
    listeners=None,
    subnets=None,
    security_groups=None,
    health_check=None,
    policies=None,
    instances=None,
    test=False,
    aws_key=None,
    aws_secret=None,
    elbconn=None,
        elb=None):
    """
    Launch an ELB

    Most arguments are the same as :meth:`.launch_or_modify`

    Parameters
    ----------
    elbconn : :class:`boto.ec2.elb.ELBConnection`, optional
        If present this function will not open a new elb connection
    elb : :class:`boto.ec2.elb.LoadBalancer`, optional
        If present this function will not make a call to fetch the load
        balancer

    """
    if elbconn is None:
        elbconn = __salt__['aws_util.elbconn'](region, aws_key, aws_secret)

    if elb is None:
        elbs = elbconn.get_all_load_balancers(load_balancer_names=[name])
        elb = elbs[0]

    changes = {}

    # Availability Zones
    if zones != elb.availability_zones:
        to_remove = set(elb.availability_zones) - set(zones)
        if to_remove:
            if not test:
                elb.disable_zones(list(to_remove))
            for zone in to_remove:
                changes['Zone {0}'.format(zone)] = "Disabled"
        to_add = set(zones) - set(elb.availability_zones)
        if to_add:
            if not test:
                elb.enable_zones(list(to_add))
            for zone in to_add:
                changes['Zone {0}'.format(zone)] = "Enabled"

    # Listeners
    elb_listeners = []
    # Convert the boto listeners to tuples
    for listener in elb.listeners:
        tup_listener = (
            listener.load_balancer_port,
            listener.instance_port,
            listener.protocol,
        )
        if listener.instance_protocol is not None:
            tup_listener += (listener.instance_protocol,)
        if listener.ssl_certificate_id is not None:
            tup_listener += (listener.ssl_certificate_id,)
        elb_listeners.append(tup_listener)
    listeners = listeners or []

    # Translate all protocols to uppercase
    for listener in listeners:
        listener[2] = listener[2].upper()
        listener[3] = listener[3].upper()

    listeners = [tuple(l) for l in listeners]
    if listeners != elb_listeners:
        to_delete = set(elb_listeners) - set(listeners)
        if to_delete:
            if not test:
                for listener in to_delete:
                    elb.delete_listener(listener[0])
            changes['Listeners Deleted'] = str(list(to_delete))
        to_create = set(listeners) - set(elb_listeners)
        if to_create:
            if not test:
                elbconn.create_load_balancer_listeners(
                    elb.name, complex_listeners=list(to_create))
            changes['Listeners Created'] = str(list(to_create))

    # Subnets
    # TODO: This is untested because my account doesn't have VPC
    elb_subnets = list(elb.subnets)
    subnets = subnets or []
    if subnets != elb_subnets:
        to_detach = set(elb_subnets) - set(subnets)
        if to_detach:
            if not test:
                elb.detach_subnets(list(to_detach))
            changes['Subnets Detached'] = str(list(to_detach))
        to_attach = set(subnets) - set(elb_subnets)
        if to_attach:
            if not test:
                elb.attach_subnets(list(to_attach))
            changes['Subnets Attached'] = str(list(to_attach))

    # Security Groups
    # TODO: This is untested because my account doesn't have VPC
    elb_security_groups = list(elb.security_groups)
    security_groups = security_groups or []
    if security_groups != elb_security_groups:
        to_remove = set(elb_security_groups) - set(security_groups)
        if to_remove:
            changes['Security Groups Removed'] = str(list(to_remove))
        to_add = set(security_groups) - set(elb_security_groups)
        if to_add:
            changes['Security Groups Added'] = str(list(to_add))
        if not test:
            elb.apply_security_groups(security_groups)

    # Health check
    if health_check is None:
        health_check = {}
    check = elb.health_check
    new_check = boto.ec2.elb.healthcheck.HealthCheck(
        access_point=name,
        interval=health_check.get('interval', 30),
        target=health_check.get('target'),
        healthy_threshold=health_check.get('healthy_threshold', 3),
        timeout=health_check.get('timeout', 5),
        unhealthy_threshold=health_check.get('unhealthy_threshold', 5))
    modified = False
    if check is None:
        modified = True
    if not modified:
        for attr in ('interval', 'target', 'healthy_threshold', 'timeout',
                     'unhealthy_threshold'):
            if getattr(new_check, attr) != getattr(check, attr):
                modified = True
                break
    if modified:
        changes['Health check'] = "Modified"
        if not test:
            elb.configure_health_check(new_check)

    # Instances
    if instances is not None:
        _convert_server_names(instances, region, aws_key, aws_secret)

        elb_instances = [i.id for i in elb.instances]
        if instances != elb_instances:
            to_remove = set(elb_instances) - set(instances)
            if to_remove:
                changes['Instances Removed'] = list(to_remove)
                if not test:
                    elb.deregister_instances(list(to_remove))
            to_add = set(instances) - set(elb_instances)
            if to_add:
                changes['Instances Added'] = list(to_add)
                if not test:
                    elb.register_instances(list(to_add))

    # Policies
    policies = policies or {}
    elb_policies = []
    for attr in ('app_cookie_stickiness_policies',
                 'lb_cookie_stickiness_policies'):
        for policy in getattr(elb.policies, attr):
            elb_policies.append(policy.policy_name)

    # Create any policies that don't exist yet
    for policy in policies.itervalues():
        if policy['type'] == 'lb':
            policy['name'] = 'lb' + str(policy['cookie_expire'])
        elif policy['type'] == 'app':
            policy['name'] = 'app' + str(hash(policy['cookie_name']))
        else:
            raise ValueError("Policy type '{0}' must be 'lb' or 'app'"
                             .format(policy['name']))

        if policy['name'] not in elb_policies:
            if not test:
                if policy['type'] == 'lb':
                    elb.create_cookie_stickiness_policy(
                        policy['cookie_expire'],
                        policy['name'])
                elif policy['type'] == 'app':
                    elb.create_app_cookie_stickiness_policy(
                        policy['cookie_name'], policy['name'])

    for listener in elb.listeners:
        if listener.load_balancer_port in policies:
            port = listener.load_balancer_port
            policy = policies[port]
            if len(listener.policy_names) == 0:
                if not test:
                    elb.set_policies_of_listener(port, [policy['name']])
                changes['Port {0:d}'.format(port)] = "Added policy"
            else:
                if policy['name'] != listener.policy_names[0]:
                    if not test:
                        elb.set_policies_of_listener(port,
                                                     [policy['name']])
                    changes['Port {0:d}'.format(port)] = "Changed policy"
        else:
            if len(listener.policy_names) > 0:
                if not test:
                    elb.set_policies_of_listener(port, [])
            changes['Port {0:d}'.format(port)] = "Removed policy"

    return changes