Global Constant
Steve Nay's ramblings

Using a configuration file for Boto

In Lab 3, we’re doing a lot more with Boto. It’s useful to have a configuration file and put everything there so that AWS keys, SimpleDB domains, and SQS queue names don’t have to be embedded in the source code. In this post, I’ll talk about using a system-wide config file for Boto.

The config file The config file itself is very simple. It has several sections, each with a heading. Under the headings are key=value pairs. Here is a simple example:



You could make other sections for S3 and SQS if desired (those will be used more in Lab 4). Finally, put that file in somewhere it can be accessed by the whole system. Mine will go in /etc/boto.cfg.

Using the config file in Python You can use the ConfigParser module to get the key/value pairs out of the config file and use them. That code might look something like this:

import ConfigParser
config = ConfigParser.ConfigParser()["/etc/boto.cfg"])

Now, whenever you need to get a value from the config file, simply call get() on the ConfigParser object, passing it the section and key you want. For example, if I want to get the imagedomain key out of the simpledb section from the example config file above, I would write this:

imagedomain = config.get('simpledb', 'imagedomain')

Conclusion The advantage of doing it this way is that all your Boto configuration is stored in one location out of the web root where all your scripts can access it. You don’t have to hard-code all that information into the website code itself, making maintenance easier.