Skip to main content

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.

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}")

Get Instance Details

To get detailed information about a specific instance, use its ID.

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}")

List Instances

You can retrieve a list of all instances in your Morph Cloud account.

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}")

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.

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}")

Stop an Instance

Stopping an instance terminates the virtual machine and releases the resources.

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.")