SSH key pair management

Note

This functionality is only available as part of the base API in Libcloud 0.14.0 and above. Previously it was available on some drivers through the extension methods.

Key pair management functionality allows you to manage SSH key pairs on your account.

This includes the following functionality:

Creating a new key pair

This example shows how to create a new key pair using libcloud.compute.base.NodeDriver.create_key_pair() method.

To generate a new key pair, you only need to specify a name argument and the provider will automatically generate a new key pair for you. Private key which has been generated on your behalf is available in the value returned by the create_key_pair method.

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

EC2_ACCESS_ID = 'your access id'
EC2_SECRET_KEY = 'your secret key'

Driver = get_driver(Provider.EC2)
conn = Driver(EC2_ACCESS_ID, EC2_SECRET_KEY)

key_pair = conn.create_key_pair(name='my-key-pair-1')

# Private key which provided generated on your behalf should be available
# through key_pair.private_key attribute.

Importing an existing key pair

If you already have an existing key pair you would like to use, you can use libcloud.compute.base.NodeDriver.import_key_pair_from_file() and libcloud.compute.base.NodeDriver.import_key_pair_from_string() method to import a public component of this pair.

import os

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

EC2_ACCESS_ID = 'your access id'
EC2_SECRET_KEY = 'your secret key'

Driver = get_driver(Provider.EC2)
conn = Driver(EC2_ACCESS_ID, EC2_SECRET_KEY)

key_file_path = os.path.expanduser('~/.ssh/id_rsa_my_key_pair_1.pub')
key_pair = conn.import_key_pair_from_file(name='my_key',
                                          key_file_path=key_file_path)
from __future__ import with_statement

import os

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

EC2_ACCESS_ID = 'your access id'
EC2_SECRET_KEY = 'your secret key'

Driver = get_driver(Provider.EC2)
conn = Driver(EC2_ACCESS_ID, EC2_SECRET_KEY)

key_file_path = os.path.expanduser('~/.ssh/id_rsa_my_key_pair_1.pub')

with open(key_file_path, 'r') as fp:
    key_material = fp.read()

key_pair = conn.import_key_pair_from_string(name='my_key',
                                            key_material=key_material)