Skip to main content

SSH Key Management

The InstaVM SDK provides methods to manage SSH public keys for secure VM authentication.

Overview

SSH keys allow you to securely access your VMs without password authentication. You can add multiple public keys, list them, and remove keys you no longer need.

Python SDK

Installation

pip install instavm

Adding SSH Keys

Add an SSH public key to your account:

from instavm import InstaVM

client = InstaVM('your_api_key')

# Add a new SSH public key
result = client.ssh.add_key(
public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC..."
)

print(f"SSH key added")
print(f"Key ID: {result.get('key_id')}")
print(f"Fingerprint: {result.get('fingerprint')}")
print(f"Added at: {result.get('created_at')}")

Listing SSH Keys

Retrieve all SSH keys associated with your account:

from instavm import InstaVM

client = InstaVM('your_api_key')

# List all SSH keys
keys = client.ssh.list_keys()

for key in keys:
print(f"Key ID: {key.get('key_id')}")
print(f" Name: {key.get('name', 'unnamed')}")
print(f" Fingerprint: {key.get('fingerprint')}")
print(f" Added: {key.get('created_at')}")

Deleting SSH Keys

Remove an SSH key by its ID:

from instavm import InstaVM

client = InstaVM('your_api_key')

# Delete an SSH key by ID
result = client.ssh.delete_key(key_id=123)

# Check if deletion was successful
if 'success' in result and result['success']:
print("SSH key deleted successfully")
else:
print(f"Failed to delete SSH key: {result}")

Generating SSH Keys

If you don't have an SSH key pair, you can generate one:

# Generate a new SSH key pair (RSA 4096 bits)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# This creates:
# ~/.ssh/id_rsa (private key)
# ~/.ssh/id_rsa.pub (public key)

# Display the public key
cat ~/.ssh/id_rsa.pub

Example output:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0xl+6your+key+data+here user@example.com

Using SSH Keys with VMs

Once you've added an SSH public key, you can SSH into your VMs:

# Get SSH connection details when creating a VM
from instavm import InstaVM

client = InstaVM('your_api_key')

vm = client.vms.create()

# SSH connection details are returned
ssh_host = vm.get('ssh_host')
ssh_user = vm.get('ssh_user')
ssh_port = vm.get('ssh_port', 22)

print(f"SSH into: ssh {ssh_user}@{ssh_host} -p {ssh_port}")

Then connect from your terminal:

# Connect to your VM
ssh -i ~/.ssh/id_rsa user@vm-host.example.com -p 22

# Or with the connection string from SDK
ssh user@vm-host.example.com -p 22

Best Practices

  1. Key Security

    • Never share private keys (id_rsa)
    • Only add public keys (id_rsa.pub)
    • Use strong passphrases for private keys
    • Rotate keys periodically
  2. Key Management

    • Add descriptive names/emails when generating keys
    • Delete unused keys regularly
    • Use separate keys for different environments
  3. VM Access

    • Each VM can accept multiple SSH keys
    • Keys are bound to your account, not individual VMs
    • All keys work across all your VMs

API Reference

For complete API documentation, see:

Error Handling

from instavm import InstaVM, InstaVMError

client = InstaVM('your_api_key')

try:
# Add invalid SSH key
result = client.ssh.add_key(public_key="invalid_key")
except InstaVMError as e:
print(f"Failed to add SSH key: {e}")