Thursday 27 March 2014

pulling aws instance details using python boto1


########### Example #########
import boto.ec2

# NOTE: if you don't put the key at conn then it will search for
# your profile file, if not /etc/boto.cfg if not your varible 'BOTO_CONFIG=/file/path'

conn=boto.ec2.connect_to_region('us-east-1')
#conn=boto.ec2.connect_to_region('us-east-1',aws_access_key_id='access_key' , aws_secret_access_key='secret_key')

# Following is the prod key: [ Full access ]
#AWS_ACCESS_KEY_ID = 'access_key'
#AWS_SECRET_ACCESS_KEY = 'secret_key'


reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print "%s " % (inst.tags['Name'])
            print "%s  %s " % ("publicDnsName:", inst.public_dns_name)
            print "%s  %s " % ("internalDnsName:", inst.private_dns_name)
            print "%s  %s " % ("publicIP:", inst.ip_address)
            print "%s  %s " % ("internalIP:", inst.private_ip_address)
            print "%s  %s " % ("architecture:", inst.architecture)
            print "%s  %s " % ("image_id:", inst.image_id)
            print "%s    %s " % ("instance_type", inst.instance_type)
            print ""

###################################################



import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print "%s " % (inst.tags['Name'])
            print "%s  %s " % ("publicDnsName:", inst.public_dns_name)
            print "%s  %s " % ("internalDnsName:", inst.private_dns_name)
            print "%s  %s " % ("publicIP:", inst.ip_address)
            print "%s  %s " % ("internalIP:", inst.private_ip_address)
            print "%s  %s " % ("architecture:", inst.architecture)
            print "%s  %s " % ("image_id:", inst.image_id)
            print "%s    %s " % ("instance_type", inst.instance_type)
            print ""



##### To find all the details #####

import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        print(inst.__dict__)
        break # remove this to list all instances


### Output: ###

{'_in_monitoring_element': False,
 'ami_launch_index': u'0',
 'architecture': u'x86_64',
 'block_device_mapping': {},
 'connection': EC2Connection:ec2.amazonaws.com,
 'dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'id': u'i-xxxxxxxx',
 'image_id': u'ami-xxxxxxxx',
 'instanceState': u'\n                    ',
 'instance_class': None,
 'instance_type': u'm1.large',
 'ip_address': u'xxx.xxx.xxx.xxx',
 'item': u'\n                ',
 'kernel': None,
 'key_name': u'FARM-xxxx',
 'launch_time': u'2009-10-27T17:10:22.000Z',
 'monitored': False,
 'monitoring': u'\n                    ',
 'persistent': False,
 'placement': u'us-east-1d',
 'previous_state': None,
 'private_dns_name': u'ip-10-xxx-xxx-xxx.ec2.internal',
 'private_ip_address': u'10.xxx.xxx.xxx',
 'product_codes': [],
 'public_dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'ramdisk': None,
 'reason': '',
 'region': RegionInfo:us-east-1,
 'requester_id': None,
 'rootDeviceType': u'instance-store',
 'root_device_name': None,
 'shutdown_state': None,
 'spot_instance_request_id': None,
 'state': u'running',
 'state_code': 16,
 'subnet_id': None,
 'vpc_id': None}

NOTE: You can use above any of this sub value to get only those information.

No comments:

Post a Comment