Create your first PAI-Instant Service instance through the shortest path: create an instant service, launch an instance from a public template, run commands with the E2B SDK, and destroy the instance.
Prerequisites
Before you use PAI-Instant Service, make sure that you have completed the following preparations:
An Alibaba Cloud account is activated and real-name verification is complete.
PAI is activated and a PAI workspace is created.
Instant Service compute resources are purchased on the EAS Dedicated Machine Pay-As-You-Go purchase page.
(Optional) To create a template from a custom image, activate Container Registry (ACR) and create a Standard or Premium ACR Enterprise Edition instance. If you only use public templates, ACR is not required.
(Optional) When using the E2B Python SDK, only versions earlier than v2.25.0 are supported.
Step 1: Create an instant service
Log in to the PAI console. In the left-side navigation pane, select Elastic Algorithm Service (EAS) to go to the service list page.
Switch to the Instant Service tab at the top.
Click Create Instant Service and configure the parameters as follows. Keep other parameters at their default values:
Name: Enter a custom name, for example,
quickstart.Resource Type: Select EAS Resource Group.
Resource Group:Select your purchased instant service resource group.
Network Egress Mode: Select Shared Public Network.
Click Confirm to complete the creation.
For detailed parameters and advanced configuration, see Manage instant services and templates.
Step 2: Select a public template
PAI-Instant Service provides ready-to-use public templates such as code-interpreter, browser, and desktop. This guide uses code-interpreter — no custom template is required.
Go to the instant service workbench.
On the Instant Service tab, find the instant service you created in Step 1. In the Invoke column, click the Instant Service Workbench icon.
In the Go to Instant Service Workbench dialog box, copy the Manage Links or Read-only Link URL and open it in your browser.
In the public template list, select
code-interpreterand click the template ID to go to the template details page.In the upper-right corner of the template details page, click SDK Access to get the SDK connection configuration and Python sample code.
Step 3: Create an instance and run commands with the SDK
Install the E2B SDK
pip install "e2b<2.25.0" Full example
The following example shows a complete instance workflow: create an instance, retrieve instance info, run commands, read and write files, and destroy the instance.
Replace <YOUR-API-TOKEN> and <YOUR-DOMAIN> with the values you obtained in Step 2.
import os
from e2b import Sandbox
import time
# 1. Set authentication credentials
os.environ["E2B_API_KEY"] = "<YOUR-API-TOKEN>"
os.environ["E2B_DOMAIN"] = "<YOUR-DOMAIN>"
# 2. Create an instance
sandbox = Sandbox.create(
template="<YOUR-TEMPLATE-ID>",
timeout=300000, # Timeout (milliseconds)
)
print(f"Sandbox ID: {sandbox.sandbox_id}")
# Wait for the instance to enter the Running state
print("Waiting for sandbox to be ready...")
max_attempts = 30
for attempt in range(1, max_attempts + 1):
try:
is_running = sandbox.is_running()
status = "Running" if is_running else "Not Running"
print(f" [{attempt}/{max_attempts}] Sandbox status: {status}")
if is_running:
print(" ✓ Sandbox is now running!")
break
except Exception as e:
print(f" [{attempt}/{max_attempts}] Failed to check status: {e}")
if attempt < max_attempts:
time.sleep(2)
# 3. Get instance details (including metadata and dashboard URL)
info = sandbox.get_info()
print(f"State: {info.state}")
print(f"CPU: {info.cpu_count}, Memory: {info.memory_mb}MB")
# Print metadata
if info.metadata:
print("\nMetadata:")
for key, value in info.metadata.items():
print(f" {key}: {value}")
# Get dashboard URL
dashboard_url = info.metadata.get('sandbox.alicloud.com/dashboard-url') or \
info.metadata.get('dashboard_url')
if dashboard_url:
print(f"\nDashboard URL: {dashboard_url}")
# 4. Run a command
result = sandbox.commands.run("echo 'Hello, World!'")
print(result.stdout)
# 5. File operations
sandbox.files.write("/tmp/test.txt", "Hello from sandbox!")
content = sandbox.files.read("/tmp/test.txt")
print(content)
# 6. Clean up
sandbox.kill() Step 4: Clean up resources
After you call
sandbox.kill()to destroy the instance, billing stops for both compute resources and the system disk.You can also find the instance in the Instance list in the instant service workbench and click Delete to destroy it.
If you no longer need the instant service, you can delete it from the instant service list.
Instances that have not called kill() or reached the timeout continue to incur charges. Paused instances are also billed at the running rate. For details, see Billing.
What's next
Use the SDK for more lifecycle, command, and file operations: E2B SDK integration
Use public templates for browser, desktop, coding, and other environments: Use public templates
Debug instances using the console terminal, VNC, file management, and other panels: Manage instances
Create a custom image template: Manage instant services and templates