Reputation: 81
I have something in python 2.7 that works well when writing to CSV, how can I add an output to json in a separate file in the same s3 bucket?
#boto3 library ec2 API describe addresses page
#http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_addresses
addresses = ec2con.describe_addresses().get('Addresses',[] )
addresseslist = len(addresses)
if addresseslist > 0:
csv_file.write("%s,%s,%s,%s,%s\n"%('','','','',''))
csv_file.write("%s,%s\n"%('EIPS INSTANCE',regname))
csv_file.write("%s,%s,%s,%s\n"%('PublicIp','AllocationId','Domain','InstanceId'))
csv_file.flush()
for address in addresses:
PublicIp=address['PublicIp']
try:
AllocationId=address['AllocationId']
except:
AllocationId="empty"
Domain=address['Domain']
if 'InstanceId' in address:
instanceId=address['InstanceId']
else:
instanceId='empty'
csv_file.write("%s,%s,%s,%s\n"%(PublicIp,AllocationId,Domain,instanceId))
csv_file.flush()...
date_fmt = strftime("%Y_%m_%d", gmtime())
#Give your file path
filepath ='/tmp/AWS_Resources_' + date_fmt + '.csv'
#Save Inventory
s3.Object('s3BUCKETNAME', filename).put(Body=open(filepath, 'rb'))
Upvotes: 0
Views: 10584
Reputation: 81
This was helpful however, I managed to get the json IP list by adding
iplist = list()
for address in addresses:
PublicIp=address['PublicIp']
iplist.append(PublicIp)
s3.Object('S#BUCKETNAME', account_id + "_" + date_fmt).put(Body=json.dumps(iplist))
Upvotes: 2
Reputation: 2748
You could create a similar function based on the one you already have, but first build a Python list of dictionaries like the following. Read more about lists and dictionaries and json if needed. You can then create a string from the dictionary for writing to an S3 file.
Note that you have a lot of style issues with your Python code. Things like variable names, list length checks, etc. Please look through PEP-8 for style recommendations.
addresses_json = []
for address in addresses:
# extract values as you already do
# create dict
addresses_json.append(dict(
PublicIp=PublicIp,
AllocationId=AllocationId,
Domain=Domain,
instanceId=instanceId
))
body = json.dumps(addresses)
filepath = '/tmp/AWS_Resources_' + date_fmt + '.json'
# use similar s3 calls to create a file for the JSON
Upvotes: 1