Home » 2013 » May

Archive for May, 2013

Using boto for Amazon EC2

Posted May 20, 2013 By weetabix

Sorry for the month delay. I had things in “the real world” that I needed to deal with. I am back.


Today, I will go over Amazon a bit, and Boto, the Amazon Python SDK.

If you are unfamiliar with either Boto, or Amazon EC2, heres the tl; dr; version:

Boto is a Python2 API for Amazons AWS. I use it specifically to talk to the Elastic Cloud. It also happens to be the normal Amazon supported SDK. It can be ported to Python3.


For a longer version:

For Python2, the library can be installed as normal, from pip, etc. You can do it manually, but this would be the best method.

For Python3, you need to grab the py3kport branch, and run 2to3 after following the README and adjusting to suit your needs.

The documentation is the same for both.


After 2to3, there need to be a few more changes made:

Do vi /usr/local/lib/python3.2/dist-packages/boto/connection.py

Find uses of sock.sendall, such as sock.sendall("CONNECT %s HTTP/1.0\r\n" % host),

and change it to read sock.sendall(bytes("CONNECT %s HTTP/1.0\r\n" % host,'utf-8')).

i.e : sock.sendall("payload") becomes sock.sendall(bytes("payload",'utf-8'))

This corrects the last of the issues with 2to3, which happens to be coding strings for transmission over a socket.


Then, you can do things like:


#!/usr/bin/env python3
import boto.ec2
conn = boto.ec2.connect_to_region("us-east-1",
                                  aws_access_key_id='<FILL ME>',
                                  aws_secret_access_key='<FILL ME>')
reservations = conn.get_all_instances()
for x in reservations:
    print(x, x.instances)
    for y in x.instances:
        for key in y.tags:
            print(key, y.tags.get(key))

will give you a printout of all your instances, and the name you gave them, as well as any other tags.

The access keys can be retreived from your AWS account.

Until next week,


Be the first to comment