ScaleScaleScaleScale

Great Architectures, Stacks & DevOps at Webscale

By Chris Ueland


Using libcloud: an unified API for Cloud based Products

Apache libcloud is an unified open-source API to work with Cloud providers. It was written in Python and allows you to interact with popular Cloud service providers like Google, Rackspace, Amazon, DigitalOcean, Zerigo, Cloudflare and many others.

It was created to make your life easier while developing apps or building cloud products that work with several Cloud providers. Using libcloud is easy, and on this post we will explore practical examples about this new Cloud API.

Features

Apache Libcloud can manage the following type of resouces:

  • Cloud Servers / Block Storage like Rackspace CloudServers or Amazon EC2 solutions.
  • Load Balancing as Service (LBaaS) like Amazon Elastic, GoGrid, Rackspace Cloud and much more.
  • CDN and Cloud Object Storage like Amazon S3, Google Storage and Rackspace CloudFiles.
  • DNS as service (DNSaaS) like Cloud DNS, Rackspace DNS or Amazon Route 53.

It also allow System Adminstraros and Devops to:

  • Create servers and run deployment scripts inside
  • Easy management with Puppet, Chef or cfengine
  • Execute arbitrary Linux commands
  • Manage instances across multiple Cloud service providers (create, delete, copy, reboot, turn on/off, etc)

Installing libcloud

libcloud can be installed using stable or development versions.

Stable version

pip install apache-libcloud

Development version

pip install -e git+https://git-wip-us.apache.org/repos/asf/libcloud.git@trunk#egg=apache-libcloud

Using libcloud

From the libcloud documentation we found some useful examples on how to use libcloud.

This first one will set the provider driver (RACKSPACE in this case), then set your drive user and api key and finally start listing your nodes:

from pprint import pprint

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

cls = get_driver(Provider.RACKSPACE)
driver = cls('my username', 'my api key')

pprint(driver.list_sizes())
pprint(driver.list_nodes())

On this other example, you’ll find a script to create a node using Rackspace:

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

RACKSPACE_USER = 'your username'
RACKSPACE_KEY = 'your key'

Driver = get_driver(Provider.RACKSPACE)
conn = Driver(RACKSPACE_USER, RACKSPACE_KEY)

# retrieve available images and sizes
images = conn.list_images()
# [, ...]
sizes = conn.list_sizes()
# [, ...]

# create node with first image and first size
node = conn.create_node(name='test', image=images[0], size=sizes[0])
# 

On this one, you will be able to create a MX and SRV record with Zerigo, and specify the priority.

from libcloud.dns.providers import get_driver
from libcloud.dns.types import Provider, RecordType

CREDENTIALS_ZERIGO = ('email', 'api key')

cls = get_driver(Provider.ZERIGO)
driver = cls(*CREDENTIALS_ZERIGO)

zone = [z for z in driver.list_zones() if z.domain == 'example.com'][0]

extra = {'priority': 10}
record = zone.create_record(name=None, type=RecordType.MX,
                            data='aspmx.l.google.com', extra=extra)

There are many more examples on how you can use libcloud to interact with your service providers.

Conclusion

As you see, Libcloud is a a pretty cool solution to interact with popular Cloud providers like Amazon, Rackspace and Google, with a simple and unified API. This API allows developers to build Cloud-based commands, services and products from a single Python interface, this is ideal for fast development of solid modern Cloud apps.

Further reading:

Popular search terms:

  • libcloud use amazon init
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • bad_ip

    Really good article.