Skip to content

Instantly share code, notes, and snippets.

@bwhaley
Last active February 22, 2019 00:33
Show Gist options
  • Save bwhaley/eee6a0f61636862515aa to your computer and use it in GitHub Desktop.
Save bwhaley/eee6a0f61636862515aa to your computer and use it in GitHub Desktop.
Determine if the instances in an autoscaling group are InService in its ELBs attached to that group. Used in conjunction with Ansible's ec2_asg module.
#!/usr/bin/env python
"""Determine if the instances in an autoscaling group are InService in its ELBs
attached to that group.
"""
import sys
import boto.ec2.elb
import boto.ec2.autoscale
import argparse
parser = argparse.ArgumentParser(description='Given an ASG, check if its instances are InService in an ELB')
parser.add_argument('-a', '--autoscale-group',
required=True,
help='Name of an autoscale group')
parser.add_argument('-r', '--region',
required=True,
help='AWS region')
parser.add_argument('-k', '--access-key',
required=True,
help='AWS Access Key')
parser.add_argument('-s', '--secret-key',
required=True,
help='AWS Secret Key')
args = parser.parse_args()
AWS_ACCESS_KEY = args.access_key
AWS_SECRET_KEY = args.secret_key
AWS_REGION = args.region
AWS_ASG = args.autoscale_group
conn_as = boto.ec2.autoscale.connect_to_region(AWS_REGION, aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
asg = conn_as.get_all_groups(names=[AWS_ASG])[0]
if asg.health_check_type != "ELB":
sys.exit("ASG does not use ELB health checks. Quitting.")
instances = [i.instance_id for i in asg.instances]
if len(instances) != asg.desired_capacity:
sys.exit("Number of instances do not yet match desired capacity")
conn_elb = boto.ec2.elb.connect_to_region(AWS_REGION, aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
for lb in asg.load_balancers:
for i in conn_elb.describe_instance_health(lb, instances=instances):
if i.state != "InService":
sys.exit("Instance {} is not InService".format(i.instance_id))
# Ansible play that uses this script
- name: wait for instances to become InService in the ELB
shell: 'python "{{health_check_script}}" -r "{{region}}" -k "{{aws_access_key}}" -s "{{aws_secret_key}}" -a "{{asg_name}}"'
register: result
until: result.rc == 0
delay: 10
retries: 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment