Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Bu hızlı başlangıçta, Python SDK'yı kullanarak bir sandbox grubu oluşturuyor, sandbox başlatıyor, komut çalıştırıyor ve oluşturulan tüm kaynakları temizliyorsun.
Prerequisites
- Kaynak grupları oluşturma iznine sahip bir Azure aboneliği.
- Azure CLI (
az) kuruldu ve giriş yaptı. - Python 3.13+ ile uv.
Ortamınızı ayarlama
SDK'yı kurun, Azure'a giriş yapın ve bu hızlı başlangıçtaki betikler için bir .env dosya yazın.
uv venv
uv pip install azure-containerapps-sandbox azure-mgmt-resource azure-mgmt-authorization
# Sign in so DefaultAzureCredential picks up your identity
az login
cat > .env <<EOF
AZURE_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
AZURE_RESOURCE_GROUP=my-rg
AZURE_SANDBOX_GROUP=my-sandbox-group
AZURE_REGION=eastus2
AZURE_PRINCIPAL_ID=$(az ad signed-in-user show --query id -o tsv)
EOF
Kaynak grubu ve sandbox grubu oluşturun ve kendinize veri düzlemi erişimi verin. Farklı kaydet setup.py:
import os, uuid
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.authorization import AuthorizationManagementClient
from azure.containerapps.sandbox import SandboxGroupManagementClient
credential = DefaultAzureCredential()
sub = os.environ["AZURE_SUBSCRIPTION_ID"]
rg = os.environ["AZURE_RESOURCE_GROUP"]
group = os.environ["AZURE_SANDBOX_GROUP"]
region = os.environ["AZURE_REGION"]
ResourceManagementClient(credential, sub).resource_groups.create_or_update(
rg, {"location": region},
)
SandboxGroupManagementClient(
credential, subscription_id=sub, resource_group=rg,
).begin_create_group(group, location=region).result()
# Container Apps SandboxGroup Data Owner (built-in role)
ROLE_DEF_ID = "c24cf47c-5077-412d-a19c-45202126392c"
scope = f"/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.App/sandboxGroups/{group}"
AuthorizationManagementClient(credential, sub).role_assignments.create(
scope=scope,
role_assignment_name=str(uuid.uuid4()),
parameters={
"properties": {
"roleDefinitionId": f"/subscriptions/{sub}/providers/Microsoft.Authorization/roleDefinitions/{ROLE_DEF_ID}",
"principalId": os.environ["AZURE_PRINCIPAL_ID"],
"principalType": "User",
}
},
)
print(f"Setup complete: rg={rg}, sandbox_group={group}, role granted.")
uv run --env-file .env python setup.py
Rol atamalarının yayılması 30 ila 60 saniye sürüyor. 403 dönerse main.py , bir dakika bekle ve tekrar dene.
Bir sandbox oluşturun ve çalıştırın
Farklı kaydet main.py:
import os
from azure.identity import DefaultAzureCredential
from azure.containerapps.sandbox import SandboxGroupClient, endpoint_for_region
credential = DefaultAzureCredential()
client = SandboxGroupClient(
endpoint_for_region(os.environ["AZURE_REGION"]), credential,
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group=os.environ["AZURE_RESOURCE_GROUP"],
sandbox_group=os.environ["AZURE_SANDBOX_GROUP"],
)
# Create a sandbox
sandbox = client.begin_create_sandbox(disk="ubuntu").result()
# Run a command
result = sandbox.exec("echo 'Hello from Azure Container Apps Sandbox.'")
print(result.stdout)
# Clean up
sandbox.delete()
Betiği Çalıştırın:
uv run --env-file .env python main.py
Kaynakları temizle
Sandbox grubunu ve kaynak grubunu silmek için aşağıdaki kodu şu şekilde teardown.pykaydedin:
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.containerapps.sandbox import SandboxGroupManagementClient
credential = DefaultAzureCredential()
sub = os.environ["AZURE_SUBSCRIPTION_ID"]
rg = os.environ["AZURE_RESOURCE_GROUP"]
SandboxGroupManagementClient(
credential, subscription_id=sub, resource_group=rg,
).begin_delete_group(os.environ["AZURE_SANDBOX_GROUP"]).result()
ResourceManagementClient(credential, sub).resource_groups.begin_delete(rg).result()
print(f"Teardown complete: {rg} deleted.")
Temizlik senaryosunu çalıştır.
uv run --env-file .env python teardown.py