Skip to content

Instantly share code, notes, and snippets.

@hltbra
Created September 23, 2014 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hltbra/d14de243ad5e3883a55c to your computer and use it in GitHub Desktop.
Save hltbra/d14de243ad5e3883a55c to your computer and use it in GitHub Desktop.
import boto
def find_autoscaling_groups_by_instance(instance_id):
asg = boto.connect_autoscale()
groups = []
g = asg.get_all_groups()
groups.extend(g)
while g.next_token:
g = asg.get_all_groups(next_token=g.next_token)
groups.extend(g)
result = []
for group in groups:
instance_ids = [i.instance_id for i in group.instances]
if instance_id in instance_ids:
result.append(group.name)
return result
def find_route_tables_by_instance_id(instance_id):
vpc = boto.connect_vpc()
route_tables = vpc.get_all_route_tables()
result = []
for route_table in route_tables:
for route in route_table.routes:
if route.instance_id == instance_id:
result.append(route_table.id)
return result
def find_subnets_associated_to_route_tables(route_table_ids):
result = []
vpc = boto.connect_vpc()
route_tables = vpc.get_all_route_tables(route_table_ids)
for route_table in route_tables:
for association in route_table.associations:
if association.subnet_id:
result.append(association.subnet_id)
return result
def find_instances_that_belong_to_a_subnet(subnet_id):
ec2 = boto.connect_ec2()
all_instances = ec2.get_only_instances()
result = []
for instance in all_instances:
if instance.subnet_id == subnet_id:
result.append(instance.instance_id)
return result
INSTANCE_ID = 'i-XYZ'
asgs = find_autoscaling_groups_by_instance(INSTANCE_ID)
route_table_ids = find_route_tables_by_instance_id(INSTANCE_ID)
subnet_ids = find_subnets_associated_to_route_tables(route_table_ids)
print "instance: {}".format(INSTANCE_ID)
print "autoscaling groups: {}".format(repr(asgs))
print "route tables: {}".format(repr(route_table_ids))
print "subnets: {}".format(repr(subnet_ids))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment