快速入門:在 Azure 容器執行個體(預覽版)中建立您的第一個無伺服器沙盒

在此快速入門中,您會在 Azure 容器執行個體 中建立無伺服器沙盒,並使用下列三個沙箱 API:

  • AI 代理程式群組 API(Azure Resource Manager REST),用於建立 AI 代理程式群組並連線到該群組。
  • 沙 箱管理 API (REST),用於在群組中建立沙箱。
  • 沙箱閘道 API(gRPC),用於執行指令、複製檔案並在沙箱中檢視日誌。

接著刪除這些資源。

Prerequisites

  • Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。

  • Azure CLI,(版本 2.60 或更新版本) 已使用 az login 登入,或使用其他工具向 Azure Resource Manager 傳送經過驗證的 REST 要求。

  • 在您的訂用帳戶中註冊的 Microsoft.ContainerInstance 資源提供者。

    az provider register --namespace Microsoft.ContainerInstance --wait
    
  • 將 參與者、Azure Deployment Stack Owner 和 Storage Blob Data Contributor 角色指派給您訂閱中的 Compute Sandbox Groups 服務主體,以便該服務能在您的訂閱中佈建受控資源群組和快照儲存體帳戶。 要指派角色,您需要有權限在訂閱上建立角色指派,例如 擁有者 或 使用者存取管理員 角色:

    subscriptionId=$(az account show --query id --output tsv)
    spObjectId=$(az ad sp show --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv 2>/dev/null || \
      az ad sp create --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv)
    
    for role in "Contributor" "Azure Deployment Stack Owner" "Storage Blob Data Contributor"; do
      az role assignment create --assignee-object-id $spObjectId --assignee-principal-type ServicePrincipal \
        --role "$role" --scope /subscriptions/$subscriptionId
    done
    

    角色指派可能需要最多 15 分鐘才能生效。

  • 在您的訂閱中為 Azure 容器執行個體 中無伺服器沙盒配置的配額:

    1. 在 Azure 入口網站開啟新的支援請求(問題類型設為服務與訂閱限制(配額),配額類型設為 容器執行個體),選擇您的訂閱,然後前往「附加詳細資料」標籤。

    2. 選取 「輸入詳細資料」 以開啟 配額詳細資料 窗格,選擇您的 區域,並在 配額類型 中選取 AIAgentsGroups - StandardCores (Preview),然後填寫 網路類型、容器類型、AIAgentsGroups 群組總限制 和 AIAgentsGroups 核心總限制:

      Azure 入口網站中 [新增支援要求] 頁面的螢幕擷取畫面,顯示 [配額詳細資料] 窗格,其中已選取 AIAgentsGroups - StandardCores (Preview)。

    3. 提交支援申請。 在您的配額申請獲核准後,請重新註冊 Microsoft.ContainerInstance 資源提供者(az provider register --namespace Microsoft.ContainerInstance --wait),以便您的訂閱能接收新啟用的功能。

  • 在建立 AI 代理群組和沙箱後(請參閱 快速入門:建立您的第一個無伺服器沙箱)後,若要連接正在執行中的沙箱,請使用由沙箱閘道 protobuf 檔案 sandboxgateway.proto產生的 gRPC 用戶端。 需要 gRPC 用戶端,因為沙箱閘道會公開一個 gRPC 服務(aci.sandboxgateway.SandboxGatewayService),用於沙盒內的操作,例如等待準備就緒、執行指令、傳輸檔案及讀取日誌。 若要使用本文件中的跨平台範例 C# CLI(支援 Linux、macOS 和 Windows),請安裝 .NET 8 SDK,並編譯 CLI。

    Note

    如果你透過 Azure Kubernetes Service (AKS) 排程和管理無伺服器沙盒,則不需要 gRPC 用戶端。

發送請求

本快速入門範例顯示每個 HTTP 請求的方法、URL、標頭及內容。 你可以用任何 HTTP 用戶端傳送它們。 例如,在 Bash shell 中:

  • 對於 AI 代理程式群組 API,請使用 az rest,該 以 Azure CLI 登入方式驗證 Azure Resource Manager。 例如,此指令會傳送 建立 AI 代理群組的請求:

    subscriptionId=$(az account show --query id --output tsv)
    groupUrl="https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myAgentsGroup"
    apiVersion="api-version=2026-08-01-preview"
    
    az rest --method put --url "$groupUrl?$apiVersion" --body '{ "location": "centralus", "properties": {} }'
    
  • 對於沙箱管理 API,請儲存 動作傳回的和,並使用。 例如,這些命令會連線至群組,並傳送用來建立沙箱的要求:

    { read -r endpoint; read -r accessToken; } < <(az rest --method post --url "$groupUrl/connect?$apiVersion" --query "[endpoint, accessToken]" --output tsv)
    
    curl -X PUT "$endpoint/sandboxes/sandbox1?$apiVersion" \
      -H "Authorization: Bearer $accessToken" -H "Content-Type: application/json" \
      -d '{ "location": "centralus", "properties": { "image": "mcr.microsoft.com/azurelinux/base/core:3.0", "resources": { "cpu": 1, "memory": 2 } } }'
    

建立 AI 代理群組

AI 代理程式群組是類型為 Microsoft.ContainerInstance/AIAgentsGroups 的 Azure Resource Manager 資源。 它保存你的沙盒以及它們共用的設定。 當您建立 AI 代理程式群組時,服務也會在您的訂閱中建立一個受管理的資源群組,其中包含用於儲存與還原快照的儲存帳戶。

  1. 建立資源群組:

    az group create --name myResourceGroup --location centralus
    
  2. 建立 AI 代理群組:

    PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myAgentsGroup?api-version=2026-08-01-preview
    
    {
      "location": "centralus",
      "properties": {}
    }
    
  3. 建立 AI 代理群組是一項長時間執行的作業。 檢查群組直到 properties.provisioningState 為 Succeeded:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myAgentsGroup?api-version=2026-08-01-preview
    

連線至 AI 代理群組

若要管理 AI 代理程式群組中的沙盒,請呼叫該群組的 connect 動作:

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myAgentsGroup/connect?api-version=2026-08-01-preview

回應內容包括群組的沙盒管理 endpoint 和一個短期有效的 accessToken:

{
  "endpoint": "https://<group-endpoint-host>/subscriptions/{subscriptionId}/resourcegroups/myResourceGroup/providers/Microsoft.ContainerInstance/<group-path>",
  "accessToken": "<access-token>",
  "notAfter": "2026-11-04T10:30:00Z"
}

該 endpoint 值已包含該群組的資源路徑。 直接將回傳的 {endpoint} URL 作為沙盒管理請求的基礎 URL(例如 {endpoint}/sandboxes/sandbox1),並傳入{accessToken}Authorization: Bearer {accessToken}標頭。 當存取權杖在 notAfter 該時到期時,請再次呼叫 connect 。

建立一個沙盒

  1. 在 AI 代理程式群組中建立沙盒:

    PUT {endpoint}/sandboxes/sandbox1?api-version=2026-08-01-preview
    Authorization: Bearer {accessToken}
    Content-Type: application/json
    
    {
      "location": "centralus",
      "properties": {
        "image": "mcr.microsoft.com/azurelinux/base/core:3.0",
        "guestOs": "linux",
        "architecture": "amd64",
        "resources": {
          "cpu": 1,
          "memory": 2
        }
      }
    }
    
  2. 沙盒建立是非同步的。 取得沙箱資訊,直到 properties.provisioningState 為 Succeeded 為止,這通常只需幾秒鐘:

    GET {endpoint}/sandboxes/sandbox1?api-version=2026-08-01-preview
    Authorization: Bearer {accessToken}
    

    回應包含 properties.connectionString,其中包含沙盒的閘道 address 與 Bearer token:

    {
      "id": "/subscriptions/{subscriptionId}/resourcegroups/myResourceGroup/providers/Microsoft.ContainerInstance/<group-path>/sandboxes/sandbox1",
      "name": "<group-name>/sandbox1",
      "type": "Microsoft.ContainerInstance/sandboxes",
      "location": "centralus",
      "tags": {},
      "properties": {
        "resources": {
          "cpu": 1.0,
          "memory": 2.0,
          "disk": 0.0
        },
        "guestOs": "linux",
        "architecture": "amd64",
        "provisioningState": "Succeeded",
        "targetState": "Active",
        "connectionString": "address=<gateway-host>;token=<gateway-token>;token_type=bearer",
        "attachToAgentFabric": false
      }
    }
    

    把 連接字串 當作秘密來處理。 任何持有令牌的人都可以在沙盒中執行指令,直到令牌到期為止。

  3. 列出群組中的沙盒:

    GET {endpoint}/sandboxes?api-version=2026-08-01-preview
    Authorization: Bearer {accessToken}
    

連接至沙箱閘道

建立沙箱後,使用 gRPC 用戶端連接沙箱閘道(aci.sandboxgateway.SandboxGatewayService),並在沙箱內執行操作。 從 sandboxgateway.proto 產生 gRPC 用戶端,並使用 properties.connectionString 中的值進行連線:

  • 連線至閘道端點:使用 address (取自properties.connectionString) 開啟 TLS gRPC 連線至 https://<gateway-host>:443。
  • 使用令牌驗證並路由請求: 在 gRPC 請求標頭(中繼資料)中傳遞 authorization: Bearer <gateway-token>。 閘道會同時使用此權杖來授權請求並識別目標沙盒。

若要建置在下列步驟中使用的範例 C# CLI 工具(SandboxGatewayCli),請安裝 .NET 8 SDK,並依照 從 sandboxgateway.proto 建立 C# CLI。

呼叫 aci.sandboxgateway.SandboxGatewayService/WaitForReady,等待沙箱內的客體開始執行。

透過使用 範例 C# CLI:

dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" wait

gRPC 請求酬載:

{
  "timeoutSeconds": 120
}

回應:

{
  "ready": true,
  "currentState": "running"
}

在沙盒中執行指令

呼叫 aci.sandboxgateway.SandboxGatewayService/ExecSandbox 以在沙盒中執行指令。 command 是要執行的執行檔,且 args 是其參數。

請使用範例 C# CLI:

dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" exec /bin/sh -c "cat /etc/os-release; nproc"

gRPC 請求酬載:

{
  "command": "/bin/sh",
  "args": ["-c", "cat /etc/os-release; nproc"],
  "timeoutSeconds": 60
}

回應包含stdout映像的作業系統(Azure Linux 3.0)以及沙盒的 CPU 數量(1)、、stderr以及 exitCode。

將檔案複製進出沙箱

  1. 將檔案使用 aci.sandboxgateway.SandboxGatewayService/WriteFile 寫入沙盒(content 是原始位元組,或在 JSON 中以 Base64 編碼)。

    透過使用範例 C# CLI:

    dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" write-file /tmp/hello.txt $'hello from my computer\n'
    

    gRPC 請求酬載:

    {
      "path": "/tmp/hello.txt",
      "content": "aGVsbG8gZnJvbSBteSBjb21wdXRlcgo="
    }
    
  2. 在沙箱內使用aci.sandboxgateway.SandboxGatewayService/ExecSandbox 在檔案中附加一行。

    透過使用範例 C# CLI:

    dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" exec /bin/sh -c "echo 'hello from the sandbox' >> /tmp/hello.txt"
    

    gRPC 請求酬載:

    {
      "command": "/bin/sh",
      "args": ["-c", "echo 'hello from the sandbox' >> /tmp/hello.txt"]
    }
    
  3. 使用 aci.sandboxgateway.SandboxGatewayService/ReadFile 讀取檔案。

    透過使用範例 C# CLI:

    dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" read-file /tmp/hello.txt
    

    gRPC 請求酬載:

    {
      "path": "/tmp/hello.txt"
    }
    

    串流回應會在 content 和 "eof": true 中傳回檔案位元組:

    hello from my computer
    hello from the sandbox
    

檢視沙盒日誌

用 aci.sandboxgateway.SandboxGatewayService/GetLogs來取沙盒日誌。

透過使用範例 C# CLI:

dotnet run -- -cs "address=<gateway-host>;token=<gateway-token>;token_type=bearer" logs 20

gRPC 請求酬載:

{
  "tail": 20,
  "timestamps": true
}

關於所有閘道方法,請參閱 沙盒閘道 API。

清理資源

  1. 刪除沙盒:

    DELETE {endpoint}/sandboxes/sandbox1?api-version=2026-08-01-preview
    Authorization: Bearer {accessToken}
    
  2. 如果你不打算做教學,請刪除 AI 代理群組和資源群組。 刪除 AI 代理群組是一項長期操作,會刪除該群組中的所有沙盒。

    DELETE https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myAgentsGroup?api-version=2026-08-01-preview
    
    az group delete --name myResourceGroup
    

教學中使用了這個快速入門的 AI 代理群組。 要做到這些,就保留 AI 代理群組。

下一步