Guide
Sandbox lifecycle
Stop a sandbox when work pauses, resume it when work returns, and tune idle policies so compute matches the time your workload is active.
Lifecycle settings control when sandbox compute runs. Stopped sandboxes keep disk state and stop billing for compute.
Prepare a sandbox
Create a sandbox and capture its ID into $ID. Every CLI snippet on this page acts on $ID.
- Bash
- PowerShell
- SDK
aca sandbox create \
--disk ubuntu \
--label name=lifecycle-demo
ID=$(aca sandbox list -l name=lifecycle-demo -o json | jq -r '.[0].id')
echo "$ID"
aca sandbox create `
--disk ubuntu `
--label name=lifecycle-demo
$ID = (aca sandbox list -l name=lifecycle-demo -o json | ConvertFrom-Json)[0].id
$ID
from azure.identity import DefaultAzureCredential
from azure.containerapps.sandbox import SandboxGroupClient, endpoint_for_region
client = SandboxGroupClient(
endpoint_for_region("eastus2"),
DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID,
resource_group=RG,
sandbox_group=GROUP,
)
sandbox = client.begin_create_sandbox(
disk="ubuntu",
labels={"name": "lifecycle-demo"},
).result()
print(sandbox.sandbox_id)
Set lifecycle policy
Set auto-suspend to pause an idle sandbox after a timeout. Auto-delete cleans up a sandbox after a longer interval.
- In the ACA Sandboxes portal, open your sandbox group.
- Select Sandboxes.
- Select the sandbox, then select Lifecycle.
- Set Auto-suspend to the idle timeout you want, such as
60seconds. - Select Save.
- Bash
- PowerShell
- SDK
aca sandbox lifecycle set --id "$ID" --auto-suspend enable --mode Memory --idle-timeout-seconds 60
aca sandbox lifecycle set --id $ID --auto-suspend enable --mode Memory --idle-timeout-seconds 60
from azure.containerapps.sandbox import (
AutoDeletePolicy,
AutoSuspendPolicy,
LifecyclePolicy,
)
sandbox.set_lifecycle_policy(LifecyclePolicy(
auto_suspend=AutoSuspendPolicy(enabled=True, interval=60, mode="Memory"),
auto_delete=AutoDeletePolicy(enabled=True, delete_interval_seconds=600),
))
Stop a sandbox
Stop a sandbox to pause compute while keeping its disk state.
- In the ACA Sandboxes portal, open your sandbox group.
- Select Sandboxes.
- Select the sandbox row.
- Select Stop.
- Wait for the state to change to Stopped.
- Bash
- PowerShell
- SDK
aca sandbox create --disk ubuntu --label name=lifecycle-demo
aca sandbox stop -l name=lifecycle-demo
aca sandbox create --disk ubuntu --label name=lifecycle-demo
aca sandbox stop -l name=lifecycle-demo
import time
sandbox.stop()
time.sleep(3)
print(client.get_sandbox_client(sandbox.sandbox_id).get().state)
Resume a sandbox
Resume a stopped sandbox before running commands in it.
- In the ACA Sandboxes portal, open your sandbox group.
- Select Sandboxes.
- Select the stopped sandbox.
- Select Resume.
- Wait for the state to return to Running.
- Bash
- PowerShell
- SDK
aca sandbox resume --id "$ID"
aca sandbox exec --id "$ID" -c "uptime"
aca sandbox resume --id $ID
aca sandbox exec --id $ID -c "uptime"
sandbox.resume()
sandbox.wait_for_running(timeout=120)
result = sandbox.exec("uptime")
print(result.stdout.strip())
Read lifecycle state
Use the sandbox state to decide whether to stop, resume, or run work.
- In the ACA Sandboxes portal, open your sandbox group.
- Select Sandboxes.
- Review the State column.
- Select a sandbox for detailed lifecycle state.
- Bash
- PowerShell
- SDK
aca sandbox get --id "$ID" -o json | jq -r '.state'
(aca sandbox get --id $ID -o json | ConvertFrom-Json).state
print(client.get_sandbox_client(sandbox.sandbox_id).get().state)
sandbox.ensure_running()
print("ok")