Skip to content

Instantly share code, notes, and snippets.

@hermes-pimentel
Last active May 11, 2017 19:45
Show Gist options
  • Save hermes-pimentel/ee48be58d96a6e1f9237 to your computer and use it in GitHub Desktop.
Save hermes-pimentel/ee48be58d96a6e1f9237 to your computer and use it in GitHub Desktop.
Export ec2 to csv
#!/usr/bin/env python
import boto.ec2
from boto.ec2 import EC2Connection
import sys
reload(sys)
sys.setdefaultencoding('utf8')
csv_file = open('instances.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):
csv_file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(get_tag(instance.tags, "Project"),
get_tag(instance.tags, "Name"),
instance.private_ip_address,
get_tag(instance.tags, "EnvType"),
instance.instance_type,
instance.key_name,
get_tag(instance.tags, "OS"),
instance.placement,
instance.subnet_id,
instance.vpc_id))
csv_file.flush()
if __name__=="__main__":
connection = boto.ec2.connect_to_region("sa-east-1", aws_access_key_id='XXXXX', aws_secret_access_key='XXXXX')
process_instance_list(connection)
csv_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment