Listing all of your EC2 Instances using boto

One of the most common problems that lot of people (who are learning AWS) face is that they can’t figure out which service in AWS they forgot to turn off. This shows up in month end on their credit card bills.

I have started with a long term project to use boto (python SDK for AWS) to list & take action on all of your components in AWS in all the regions. As first part of the series, here is the python code that can be used to list all EC2 Instances & EBS volumes.


import argparse
import boto.ec2
access_key = ''
secret_key = ''
def get_ec2_instances(region):
ec2_conn = boto.ec2.connect_to_region(region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
reservations = ec2_conn.get_all_reservations()
for reservation in reservations:
print region+':',reservation.instances
for vol in ec2_conn.get_all_volumes():
print region+':',vol.id
def main():
regions = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1',
'ap-southeast-1','ap-southeast-2','ap-northeast-1']
parser = argparse.ArgumentParser()
parser.add_argument('access_key', help='Access Key');
parser.add_argument('secret_key', help='Secret Key');
args = parser.parse_args()
global access_key
global secret_key
access_key = args.access_key
secret_key = args.secret_key
for region in regions: get_ec2_instances(region)
if __name__ =='__main__':main()

view raw

list.py

hosted with ❤ by GitHub

You can run this code as

python list.py <aws_access_key> <aws_secret_key>

Suggest what else would like to be covered in this long-term project. Let me know if you are good in Python/Boto & would like to contribute to it.

3 Responses to Listing all of your EC2 Instances using boto

  1. Alex says:

    Good script considering two new regions predefined in Boto but not available in real life:
    “cn-north-1” and “us-gov-west-1”

  2. john says:

    Get error as follows , is there any reason for that 😦

    File “./list.py.”, line 36, in
    if __name__ ==’__main__’:main()
    File “./list.py”, line 34, in main
    for region in regions: get_ec2_instances(region)
    File “./list.py”, line 13, in get_ec2_instances
    reservations = ec2_conn.get_all_reservations()
    AttributeError: EC2Connection instance has no attribute ‘get_all_reservations’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: