Tuesday, November 29, 2011

BitNami Django Stack Cloud-ready with boto

At BitNami we are big fans of the Amazon Web Services Cloud. As part of our commitment to make AWS more accessible to developers and end-users, we have been bundling popular developer tools and SDKs with our stacks. Currently, BitNami LAMP/WAMP and MAMP Stacks ship the PHP AWS SDK tools and RubyStack ships the latest stable version of the Ruby AWS SDK. You can also download BitNami Cloud Tools, an all-in-one installer that includes the most popular command line tools available for the Amazon API: EC2, Beanstalk, ELB, RDS, SES etc.

So, what does all of the above have to do with Django? We are happy to announce that the latest version of BitNami Django Stack ships the most popular Python interface to Amazon Web Services: the boto project. Boto is an integrated interface to current and future infrastructure services offered by Amazon, including S3, EC2, SQS, ELB, SNS among others.

Please see below for a quick intro on how to use boto to launch a BitNami Django Stack instance in the EC2 Cloud:
  • First, create a text file  (aws-credentials.txt) that includes your AWS credentials :
AWSAccessKeyId=YOUR_ACCESS_KEY
AWSSecretKey=YOUR_SECRET_KEY
  • Go to your BitNami Django Stack installation and open the "use_djangostack" console and set the AWS_AWS_CREDENTIAL_FILE environment variable.
On Windows: > set AWS_AWS_CREDENTIAL_FILE="C:\path\to\aws-credentials.txt"
On OS X or Linux: $ export AWS_AWS_CREDENTIAL_FILE=/path/to/aws-credentials.txt
  • Open a Python terminal and create a the connection
bash-3.2$ python
Python 2.6.5 (r265:79063, Nov 16 2011, 09:41:19) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. 
>>> from boto.ec2.connection import EC2Connection 
>>> connection = EC2Connection()

  • Check that your aws credentials are correct if you see the following error:
'Check your credentials' % (len(names), str(names))) boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['QuerySignatureV2AuthHandler'] Check your credentials
  • No launch the BitNami DjangoStack AMI using boto:
>>> images = connection.get_all_images(image_ids=['ami-2bbb7342']) 
>>> image = images[0] >>> image.location u'979382823631/bitnami-djangostack-1.3.1-1-linux-ubuntu-10.04-ebs' 
>>> reservation = image.run()

  • Now it is starting the instance, you can check the state
>>> reservation.instances [Instance:i-cf1334ac] 
>>> instance = reservation.instances[0] 
>>> instance.state u'pending' 
>>> instance.update() u'running'

  • Once instance is running, you will know the public domain name and you can check that it is working from your web browser.
>>> instance.dns_name u'ec2-XX-XXX-XX-XXX.compute-1.amazonaws.com'

  • As we have started an EBS volume, you can select to stop it or remove it once you are done with it.
>>> instance.stop() 
>>> instance.update() u'stopping' 
>>> instance.update() u'stopped' 
>>> instance.terminate() 
>>> instance.update() u'terminated'