What's an Instance?
Every virtual machine (VM) running in Morph Cloud is an instance. Each instance runs from a saved snapshot. This guide covers the basic lifecycle operations for instances, including starting, stopping, and saving them.
Start an Instance
To launch a new instance, you need a Snapshot ID. The instance will be created based on the specified snapshot.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
snapshot_id = "snapshot_abc123" # Replace with a valid snapshot ID
instance = client.instances.start(snapshot_id=snapshot_id)
print(f"Instance started with ID: {instance.id}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function startInstance() {
const snapshotId = "snapshot_abc123"; // Replace with a valid snapshot ID
const instance = await client.instances.start({ snapshotId: snapshotId });
console.log(`Instance started with ID: ${instance.id}`);
}
startInstance();
# Start an instance from a snapshot
morphcloud instance start snapshot_abc123
Get Instance Details
To get detailed information about a specific instance, use its ID.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
print(f"Instance ID: {instance.id}, Status: {instance.status}, Networking: {instance.networking}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function getInstanceDetails() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
console.log(`Instance ID: ${instance.id}, Status: ${instance.status}, Networking: ${JSON.stringify(instance.networking)}`);
}
getInstanceDetails();
# Get instance details
morphcloud instance get morphvm_abc123
List Instances
You can retrieve a list of all instances in your Morph Cloud account.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
# List all instances
instances = client.instances.list()
for instance in instances:
print(f"ID: {instance.id}, Status: {instance.status}, Snapshot ID: {instance.refs.snapshot_id}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function listInstances() {
// List all instances
const instances = await client.instances.list();
instances.forEach(instance => {
console.log(`ID: ${instance.id}, Status: ${instance.status}, Snapshot ID: ${instance.refs.snapshotId}`);
});
}
listInstances();
# List all instances
morphcloud instance list
Save an Instance (Create Snapshot)
You can create a snapshot from a running instance, capturing its current state for future use or as a backup.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
new_snapshot = instance.snapshot()
print(f"Snapshot created from instance '{instance_id}' with ID: {new_snapshot.id}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function createSnapshotFromInstance() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
const newSnapshot = await instance.snapshot();
console.log(`Snapshot created from instance '${instanceId}' with ID: ${newSnapshot.id}`);
}
createSnapshotFromInstance();
# Create a snapshot from an instance
morphcloud instance snapshot morphvm_abc123
Stop an Instance
Stopping an instance terminates the virtual machine and releases the resources.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
instance.stop()
print(f"Instance '{instance_id}' stopped.")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function stopInstance() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
await instance.stop();
console.log(`Instance '${instanceId}' stopped.`);
}
stopInstance();
# Stop an instance
morphcloud instance stop morphvm_abc123