def get_all_instances(regions, filters=None): all_ips = [] connections = [ec2.startConnection(re) for re in regions] for c in connections: try: ips = ec2.getEc2InstancesPublicIp(c, name_tag, filters) all_ips.extend(ips) except Exception as e: print "Failed to get Ips in " + c.region.name + " " + str(e) return all_ips
def get_instances_per_region(regions): ip_to_region = {} for re in regions: conn = ec2.startConnection(re) try: ips = ec2.getEc2InstancesPublicIp(conn, name_tag) for (name, ip) in ips: ip_to_region[ip] = re except Exception as e: print "Failed to get Ips in " + c.region.name + " " + str(e) return ip_to_region
def cancel_instances(regions): connections = [ec2.startConnection(re) for re in regions] for c in connections: try: ids = ec2.getEc2InstancesId(c, name_tag, filters={"tag:Name": "ping"}) print "IDs to terminate for region " + c.region.name + " are" print ids if (len(ids) > 0): ec2.terminateEc2Instances(c, ids) except Exception as e: print e print "failed to terminate instance"
def create_instances(regions): print "Creating instances" connections = [ec2.startConnection(re) for re in regions] for c in connections: try: key = keys[c.region.name] reservation = c.run_instances(ami[c.region.name], key_name=key, instance_type=instance, security_groups=[security_group]) reservation.instances[0].add_tag('Name', 'ping') reservation = c.run_instances(ami[c.region.name], key_name=key, instance_type=instance, security_groups=[security_group]) reservation.instances[0].add_tag('Name', 'ping') except Exception as e: print e print "Failed to create instance in " + c.region.name
def load_keys(keyname, regions): for r in regions: conn = ec2.startConnection(r) key = keyname + "-" + r keys[r] = ec2.getOrCreateKey(conn, key)