Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created July 16, 2016 09:57
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save ableasdale/8cb7a61cad3202e09bab3e11c4639133 to your computer and use it in GitHub Desktop.
Save ableasdale/8cb7a61cad3202e09bab3e11c4639133 to your computer and use it in GitHub Desktop.
Using Boto 3 to list out AWS EC2 instance information
import boto3
from termcolor import colored
ec2 = boto3.resource('ec2')
for i in ec2.instances.all():
print("Id: {0}\tState: {1}\tLaunched: {2}\tRoot Device Name: {3}".format(
colored(i.id, 'cyan'),
colored(i.state['Name'], 'green'),
colored(i.launch_time, 'cyan'),
colored(i.root_device_name, 'cyan')
))
print("\tArch: {0}\tHypervisor: {1}".format(
colored(i.architecture, 'cyan'),
colored(i.hypervisor, 'cyan')
))
print("\tPriv. IP: {0}\tPub. IP: {1}".format(
colored(i.private_ip_address, 'red'),
colored(i.public_ip_address, 'green')
))
print("\tPriv. DNS: {0}\tPub. DNS: {1}".format(
colored(i.private_dns_name, 'red'),
colored(i.public_dns_name, 'green')
))
print("\tSubnet: {0}\tSubnet Id: {1}".format(
colored(i.subnet, 'cyan'),
colored(i.subnet_id, 'cyan')
))
print("\tKernel: {0}\tInstance Type: {1}".format(
colored(i.kernel_id, 'cyan'),
colored(i.instance_type, 'cyan')
))
print("\tRAM Disk Id: {0}\tAMI Id: {1}\tPlatform: {2}\t EBS Optimized: {3}".format(
colored(i.ramdisk_id, 'cyan'),
colored(i.image_id, 'cyan'),
colored(i.platform, 'cyan'),
colored(i.ebs_optimized, 'cyan')
))
print("\tBlock Device Mappings:")
for idx, dev in enumerate(i.block_device_mappings, start=1):
print("\t- [{0}] Device Name: {1}\tVol Id: {2}\tStatus: {3}\tDeleteOnTermination: {4}\tAttachTime: {5}".format(
idx,
colored(dev['DeviceName'], 'cyan'),
colored(dev['Ebs']['VolumeId'], 'cyan'),
colored(dev['Ebs']['Status'], 'green'),
colored(dev['Ebs']['DeleteOnTermination'], 'cyan'),
colored(dev['Ebs']['AttachTime'], 'cyan')
))
print("\tTags:")
for idx, tag in enumerate(i.tags, start=1):
print("\t- [{0}] Key: {1}\tValue: {2}".format(
idx,
colored(tag['Key'], 'cyan'),
colored(tag['Value'], 'cyan')
))
print("\tProduct codes:")
for idx, details in enumerate(i.product_codes, start=1):
print("\t- [{0}] Id: {1}\tType: {2}".format(
idx,
colored(details['ProductCodeId'], 'cyan'),
colored(details['ProductCodeType'], 'cyan')
))
print("Console Output:")
# Commented out because this creates a lot of clutter..
# print(i.console_output()['Output'])
print("--------------------")
@viniciusntch
Copy link

Very useful

@sharathsamala
Copy link

Superb... !

@akhildevelops
Copy link

I'm unable to find any details on ec2.instance object type in boto3 docs. Any pointer Thankyou.

@defenestratexp
Copy link

This is pretty amazing. I'm forking the hell out of this one.

@anisf
Copy link

anisf commented Nov 23, 2018

Thank you !

@jasonobert
Copy link

Thanks for this. Very helpful!

@Muddasser123
Copy link

its really amazing .... however its giving only one region data ..... if required for all region then .......please help .

@WingsLikeEagles
Copy link

Description is a little misleading... This list information for ALL instances. Should read:
Using Boto 3 to list out AWS EC2 instances information

I only mention this because I'm trying to find how to list the MAC Address of a given Instance with an instance-id and this result showed up in search.

@manugarri
Copy link

manugarri commented Jul 11, 2019

simple fix on line 59 (to avoid crashing on untagged instances):

    if i.tags:
        print("\tTags:")
        for idx, tag in enumerate(i.tags, start=1):
            print("\t- [{0}] Key: {1}\tValue: {2}".format(
                idx,
                colored(tag['Key'], 'cyan'),
                colored(tag['Value'], 'cyan')
            ))

@terminator891
Copy link

Great Work. Thanks

@frenzydf
Copy link

awesome, is mandatory to fix line 59 error mentioned before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment