Amazon Relational Database Service and Django

Amazon has recently released a long-awaited feature addition to it’s Web Services infrastructure: Relational Databases. Having for a long time only had SimpleDB on offer (non-relational and weird to implement), it’s always been something of a struggle to get MySQL running on EC2.

Traditionally one would have to set up a block storage device, mount it, hack the mySQL  configuration to use the mounted EBS volume for storage (not to mention messing around with XFS locking and snapshotting the DB when the instance goes down). All in all a pain to implement and maintain.

Now with Amazon’s Relational Database Service, you get a MySQL database, hosted in the cloud – auto backed-up, patched and running on a scalable infrastructure (you can re-size it to your hearts content) and at the same time independent of your instances – meaning no more messing around with block storage for your database.

Now RDS is actually completely compatible with all mySQL tools, so interfacing it with django is stupid simple – but, for those that want a quick walkthrough, I’ve prepared one below, from getting the service running to getting django to use it:

1. Sign up for the RDS account

I’m going to assume you’re already using amazon AWS, or at least have an account – if not, sign up now. Once done, go tot he rds page and sign up for the RDS service.

2. Get the tools

You’ll need the command line tools, if you’re familiar with EC2 this should be easy, but if not, you will need to download Java and install it.

Next, download the command line tools and unzip them to a friendly directory.

3. Set up your paths

I’ve put together a simple batch script (for windows) that will set all the paths up for you, it is helpfully included below:

setuprds.bat

@echo off
set AWS_RDS_HOME=C:RDSCli-1.0.001
set PATH=%PATH%;%AWS_RDS_HOME%bin
set AWS_CREDENTIAL_FILE=%AWS_RDS_HOME%aws_credentials
set JAVA_HOME=C:Program FilesJavajre6

make sure to set the JAVA_HOME path to your java installation

4. Set up your credentials template

If you look at the above path, I’ve put it into the command line tools  home, name it aws_credentials and it should look something like this:

AWSAccessKeyId=YOUR_ACCESS_ID
AWSSecretKey=YOUR_SECRET_KEY

For those that don’t know where to get these ID’s, you can get them from your AWS account page under ‘Your Account’ -> Security Credentials

5.  Set up your paths

Run the batch file you’ve set up – that should get all your paths sorted out – if you want to make the changes permanent, you will need to set the paths up in your control panel to make them permanent.

To test if everything is running type in:

c:> rds --help

6. Let’s get started – setting up your first instance

I’ve based these instructions on the Amazon quick start guide, so if you want to see the originals just head over there, here’s how you would start up your first instance:

c:> rds-create-db-instance --db-instance-identifier TestInstanceName --allocated-storage 20 --db-instance-class db.m1.xlarge --engine MySQL5.1  --master-username mymasteruser --master-user-password mymasterpass  --db-name TESTDB --headers

Change the names of the instance, username, password, DB Size and DB name in the command line before running it, you should get output showing that the instance is starting.

7. checking your instances

Make sure that your instance is started – and get the address of your new DB, simply type in:

c:> rds-describe-db-instances --headers

If your instance has started, you’ll see something along the lines of:

available  xxxx.xxxx.us-east-1.rds.amazonaws.com  3306  us-east-1a  1

In the output, that’s your db host name – write it down.

8. Set up the security settings.

For simplicity (and testing) you can grant access to the instance using an IP range, it’s easy – just enter the range with the following command

c: > rds-authorize-db-security-group-ingress default --cidr-ip xx.xx.xx.xx/10 --headers

If you want to grant access to an EC2 security group, you will need your account ID (this is different from your access ID) – you will find it under ‘Your Account’ on the top right, it will say something along the lines of:

Account Number XXXX-XXXX-XXXX

remove the dashes and place the account number into the following command:

c:> rds-authorize-db-security-group-ingress default --ec2-security-group-name SECURITY_GROUP_NAME --ec2-security-group-owner-id XXXXXXXXXXXX

9. Try it out with django

I set up a standard app and ran it from localhost, the most important thing is to set up your settings.py file:

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'DB_NAME'
DATABASE_USER = 'mymasteruser'
DATABASE_PASSWORD = 'mymasterpass'
DATABASE_HOST = 'xxxxx.xxxxx.us-east-1.rds.amazonaws.com'
DATABASE_PORT = ''

In your app models.py create your model the same way you would normally, go to the command prompt and run syncdb. Your model will be created and you can go on ahead and use it to your hearts content the same way you would any other MySQL back-end.

I’d recommend reading up on the RDS command line tools – the documentation can be found here

Hopefully Amazon will add RDS to the AWS management console like they did EBS, which would circumvent this whole set up malarkey.

What are your thoughts on this new service? Is it too pricey?