python - Boto issues listing AWS instances with no-tags -


i use custom tags aws instances. trying list instances (running , stopped) in csv file. not being programmer, searched , copied/pasted codes , came script runs well. have noticed if 1 tag missing script throws error , stops right there. if tag created empty, script prints blank space, if tag not created @ script stops. example if tag owner missing throws error keyerror: 'owner' , stops there. script pasted below. can let me know changes need make if tag not exist, script prints out n/a instead of stopping.

#!/usr/bin/env python import boto.ec2  boto.ec2 import ec2connection csv_file = open('/home/sbasnet/scripts/instances/instances_east.csv','w+')  def process_instance_list(connection):     map(build_instance_list,connection.get_all_instances())  def build_instance_list(reservation):     map(write_instances,reservation.instances)  def write_instances(instance):     if (instance.platform == 'windows'):         platform = 'windows'     else:         platform = 'linux'     csv_file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(instance.id,instance.private_ip_address,instance.tags['classification'],instance.tags['fqdn'],instance.tags['owner'],instance.tags['runtype'],instance.instance_type,instance.subnet_id,instance.key_name,platform,instance.placement))     csv_file.flush()  if __name__=="__main__":     connection = ec2connection(aws_access_key_id='access',aws_secret_access_key='secret')     process_instance_list(connection)     csv_file.close() 

tia sbasnet

maybe do?

warning: untested

#!/usr/bin/env python import boto.ec2  boto.ec2 import ec2connection  csv_file = open('/home/sbasnet/scripts/instances/instances_east.csv','w+')  def process_instance_list(connection):     map(build_instance_list,connection.get_all_instances())  def build_instance_list(reservation):     map(write_instances,reservation.instances)  def get_tag(tags, tag_name):     if tag_name in tags.keys():         return tags[tag_name]     else:         return "n/a"  def write_instances(instance):     if instance.platform == 'windows':         platform = 'windows'     else:         platform = 'linux'     csv_file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(instance.id,                                                          instance.private_ip_address,                                                          get_tag(instance.tags, "classification"),                                                          get_tag(instance.tags, "fqdn"),                                                          get_tag(instance.tags, "owner"),                                                          get_tag(instance.tags, "runtype"),                                                          instance.instance_type,                                                          instance.subnet_id,                                                          instance.key_name,                                                          platform,                                                          instance.placement))     csv_file.flush()   if __name__=="__main__":     connection = ec2connection(aws_access_key_id='access',aws_secret_access_key='secret')     process_instance_list(connection)     csv_file.close() 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -