Getting started with Boto


Boto is a python library for interfacing with Amazon Web Services (AWS). It supports nearly the entire AWS stack and is under active development so you can expect support for any upcoming AWS functionality as well. Boto is hosted on GitHub, and is available under the MIT license.

This is a quick post on the basics of setting up Boto, connecting to AWS, and managing EC2 instances. This covers a very small slice of Boto’s available functionality; refer to Boto’s online documentation for information on the rest of the API.

If you’re curious about the name, like I was, my best guess is that it was named after a species of dolphins found in the Amazon river, which are called Boto in portuguese. Amazon, get it? Moving on…

Installing Boto

The best way to get Boto is via the “pip” package manager:

You can also manually download it from GitHub and install:

Configuring access keys

You need your AWS access key ID and secret access key to make requests to the AWS API. If you don’t know how to get them, read the instructions here. If you don’t have an AWS account yet, sign up for one at the AWS home page.

You can provide the keys in your code as parameters to the Boto API, but it’s better to set them up ahead of time. You have do this in one of many ways:

Option 1: Using a credentials file

I prefer this method. Put the following into a file, say .aws-creds in your home directory.

Now set the AWS_CREDENTIAL_FILE environment variable to point to it. If you’re using Bash, add the following to ~/.bash_profile:

Option 2: As environment variables

You can also set the keys themselves as environment variables. If you’re using Bash, add the following to ~/.bash_profile:

Option 3: In the Boto config file

Create a file called .boto in your home directory, with the following contents:

Using the Boto API

Establishing a connection

Pretty much all API operations require an EC2Connection object, which you can obtain as follows:

Creating an instance

Waiting for an instance to come up

If you want to wait for an instance to come up before exiting your script, us the following code:

Once the instance is up, here are some other things you can do:

Assigning a static IP

Registering IP with DNS (Route53)

Adding tags

Stopping an instance

Starting an instance

The instance will take some time to come up; if you want to wait, use the code from the earlier section on waiting for an instance to come up.

Note that if your instance had a static IP assigned to it before it was stopped, you need to re-associate the IP to the instance. A trick you can use to know which IP to re-associate is to store the IP as a tag of the instance.

Getting a list of instances

Deleting an instance

Releasing an IP address

Deregistering an IP from DNS