Friday, October 30, 2009

Using RDS in Boto

Initial support for RDS has just been added to boto. The code currently lives in the subversion trunk but a new boto release will be out very soon that will also include the new RDS module. To get things started, I'll give a short tutorial on using RDS.

The first thing we need to do is create a connection to the RDS service. This is done in the same way all other service connections are created in boto:

>>> import boto
>>> rds = boto.connect_rds()

Ultimately, we want to create a new DBInstance, basically an EC2 instance that has been pre-configured to run MySQL. Before we can do that, we need to create a couple of things that are required when creating a new DBInstance. First, we will need a DBSecurityGroup. This is very similar to the SecurityGroup used in EC2 but it's considerably more simple because it is focused on only one type of application, MySQL. Within a DBSecurityGroup I can authorize access either by a CIDR block or by specifying an existing EC2 SecurityGroup. Since I'm going to be accessing my DBInstance from an EC2 instance, I'm just going to authorize the EC2 SecurityGroup that my instance is running in. Let's assume it's the group "default":

Now that we have a DBSecurityGroup created, we now need a DBParameterGroup. The DBParameterGroup is what's used to manage all of the configuration settings you would normally have in your MySQL config file. Because you don't have direct access to your DBInstance (unlike a normal EC2 instance) you need to use the DBParameterGroup to retrieve and modify the configuration settings for your DBInstance. Let's create a new one:

The ParameterGroup object in boto subclasses dict, so it behaves just like a normal mapping type. Each key in the ParameterGroup is the name of a config entry and it's value is a Parameter object. Let's explore one of the Parameters in the ParameterGroup. Because the set of parameters is quite large, RDS doesn't send all of the default parameter settings to you when you create a new ParameterGroup. To fetch them from RDS, we need to call get_params:

At this point, RDS will start the process of bringing up a new MySQL instance based on my specifications. There are lots of other parameters available to tweak. In addition, you can do things like set the preferred maintenance window and when you would prefer to have snapshots run. To check on the status of our instance, we can do the following:

So, at this point our new DBInstance is up and running and we have the endpoint and port number we need to connect to it. One of the nice things about RDS is that once the instance is running, I can use RDS to perform a lot of the management tasks associated with the server. I can do snapshots of the server at any time, or I can automate that process. I can change any of the parameters associated with the server and decide whether I want those changes to take place immediately or to wait until the next maintenance window. I can also use the modify_dbinstance method to tell RDS to increase the allocated storage on my server or even move my instance up to a larger instance class.

The current RDS code is checked in. It's still beta quality but we will be releasing a 1.9 version of boto early next week which will include this code as well as support for VPC and a ton of bug fixes. So, if you get a chance, give the boto RDS module a try and let us know what you think.

I use the CIDR 9#.2##.2##.1##/32 to allow my ip to test out accces to the new rds database. It says that it is ready but I can't connect. Am I missing something? I used the master user I setup as the user and it's password but no luck. Also how do I use the ModifyDBInstance of the API in boto? I will want to add another security group to the db once I'm done testing from my machine here. Thanks again for a great python product to help do this.

Hi Mitch,It looks like it is set to default instead of the one I created. How do I modify this? I tried:inst2 = rds.modify_dbinstance(id='dbdev', security_group='linkage') but I get a global name 'backup_retention_period' is not defined error