LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Blogs > Spock's Brain
User Name
Password

Notices


Rate this Entry

Amazon query instances script using boto.ec2

Posted 06-05-2015 at 12:36 PM by Habitual
Updated 06-24-2015 at 10:57 AM by Habitual

My very first Python script hatched into existence today, My Birthday!

This script utilizes the boto.ec2 library.
It runs in 2 seconds.
All my past experience has been outright butchery of the bash environment, and if I did this in bash, it would take 45s'ish and be painful.
Be sure to read this wrt the boto config for profiles: eg: ~/.boto:
Code:
[Credentials]

[profile default]
aws_access_key_id = key 
aws_secret_access_key = secret
ec2_region_name = region

[profile name1]
aws_access_key_id = key 
aws_secret_access_key = secret
ec2_region_name = region

[profile name2]
aws_access_key_id = key 
aws_secret_access_key = secret
ec2_region_name = region
...
et al
Here's the goods: - Updated script on Jun 24th, 2015
Code:
#!/usr/bin/python
import boto.ec2
import os
from boto.s3.connection import S3Connection
# boto.set_stream_logger('boto')
os.system('clear')

conn = boto.ec2.connect_to_region('us-east-1', profile_name='default')
images = conn.get_all_images(owners='self', filters=None)
snapshots = conn.get_all_snapshots(owner="self")
volumes = conn.get_all_volumes()
reservations = conn.get_all_instances()
sec_groups = conn.get_all_security_groups()
connS3 = S3Connection(profile_name='default')
buckets = connS3.get_all_buckets()

lines = "*" * 12
lines80 = "-" * 80

instances = [i for r in reservations for i in r.instances]
instance_volumes = [v for v in volumes if v.attach_data.instance_id]
groups = [i for g in sec_groups for i in r.instances]

print str(len(instances)) + " Instances:"
print lines
for instance in instances:
    formatter = "%s \t%s\t%s\t%s\t%s"
    print formatter % (instance.tags['Name'], instance.placement, instance.id,
                       instance.ip_address, instance.key_name)

print ""
print str(len(images)) + " Images:"
print lines
for image in images:
    formatter = "%s\t%s"
    print formatter % (image.id, image.name)

print ""
print str(len(volumes)) + " Volumes:"
print lines
for volume in volumes:
        print volume.id

print ""
print str(len(snapshots)) + " Snapshots:"
print lines
for snapshot in snapshots:
        print snapshot.id

print ""
print str(len(groups)) + " SecGroups:"
print lines
for group in sec_groups:
    print group.name

print ""
print str(len(buckets)) + " S3 buckets:"
print lines
for bucket in sorted(buckets):
    print(bucket.name)
Posted in Uncategorized
Views 2421 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    You picked a great way to spend your day =)

    Learning something new keeps life interesting. Congrats on your script and if you ever want to check out Amazon's thoughts on interfacing with AWS (written in python and utilizes boto, too), check here:

    http://aws.amazon.com/cli/

    Happy coding, and happy birthday!
    Posted 06-05-2015 at 03:52 PM by rocket357 rocket357 is offline
  2. Old Comment
    python is the way to go for slick scripting, especially with boto for AWS.

    here is what i did a few years ago:
    https://skaperen.s3.amazonaws.com/list-instances
    https://skaperen.s3.amazonaws.com/list-volumes
    https://skaperen.s3.amazonaws.com/list-s3

    and recently:
    https://skaperen.s3.amazonaws.com/co...to_instance.py
    Posted 08-29-2015 at 07:57 AM by Skaperen Skaperen is offline
 

  



All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration