.NET SDK
Create a sandbox group and sandbox, run a command in sandbox with C#.
- An Azure subscription with Microsoft.App registered and an existing resource group.
- The Contributor role on the resource group, or equivalent permissions that include
Microsoft.App/sandboxGroups/write, for the identity you use to sign in. - The Container Apps SandboxGroup Data Owner role on the resource group so the new group inherits data-plane access, or on the group if it already exists. Contributor alone does not grant data-plane access. See Grant data-plane access.
- The .NET SDK, version 8.0 or later.
Use ArmClient with Azure.ResourceManager.AppContainers for the control-plane Azure Resource Manager (ARM) API, which handles all create, read, update, and delete (CRUD) operations on sandbox groups.
Use SandboxGroupClient with Microsoft.Azure.ContainerApps.Sandbox for the data-plane API, which manages sandboxes, disk images, snapshots, and volumes within a sandbox group.
Install and configure the SDK
Create an empty project directory and open a shell in it. Install the sandbox and management-plane packages, plus Azure.Identity for authentication.
Replace the placeholder values with your subscription ID and existing resource group name, then choose a sandbox group name and region. Set AZURE_REGION to a region code, such as eastus2, not its display name. If you use an existing group, use its name and region. Keep this shell open for the run step.
- Bash
- PowerShell
dotnet new console
dotnet add package Microsoft.Azure.ContainerApps.Sandbox --prerelease
dotnet add package Azure.ResourceManager.AppContainers --version 1.6.0
dotnet add package Azure.Identity
# Sign in so DefaultAzureCredential can use your Azure CLI identity.
az login
export AZURE_SUBSCRIPTION_ID="<subscription-id>"
export AZURE_RESOURCE_GROUP="<resource-group-name>"
export AZURE_SANDBOX_GROUP="<sandbox-group-name>"
export AZURE_REGION="<group-region>"
dotnet new console
dotnet add package Microsoft.Azure.ContainerApps.Sandbox --prerelease
dotnet add package Azure.ResourceManager.AppContainers --version 1.6.0
dotnet add package Azure.Identity
# Sign in so DefaultAzureCredential can use your Azure CLI identity.
az login
$env:AZURE_SUBSCRIPTION_ID = '<subscription-id>'
$env:AZURE_RESOURCE_GROUP = '<resource-group-name>'
$env:AZURE_SANDBOX_GROUP = '<sandbox-group-name>'
$env:AZURE_REGION = '<group-region>'
DefaultAzureCredential uses your Azure CLI sign-in when no earlier credential in its chain is configured. If you just received data-plane access and the next step returns a 403 response, wait a minute for the role assignment to propagate and retry.
Create a sandbox group
Create or update the named sandbox group in your existing resource group through the control plane and wait for provisioning to complete.
Replace the contents of Program.cs with:
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppContainers;
using Azure.ResourceManager.AppContainers.Models;
using Azure.ResourceManager.Resources;
var subscriptionId = RequiredEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var resourceGroupName = RequiredEnvironmentVariable("AZURE_RESOURCE_GROUP");
var sandboxGroupName = RequiredEnvironmentVariable("AZURE_SANDBOX_GROUP");
var region = RequiredEnvironmentVariable("AZURE_REGION");
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential, subscriptionId);
var resourceGroup = armClient.GetResourceGroupResource(
ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName));
var groupData = new SandboxGroupData(new AzureLocation(region))
{
Properties = new SandboxGroupProperties()
};
var groupOperation = await resourceGroup.GetSandboxGroups().CreateOrUpdateAsync(
WaitUntil.Completed, sandboxGroupName, groupData);
Console.WriteLine($"Sandbox group: {groupOperation.Value.Id}");
static string RequiredEnvironmentVariable(string name) =>
Environment.GetEnvironmentVariable(name)
?? throw new InvalidOperationException($"Set {name} before running the quickstart.");
Create a sandbox and run a command
Add these data-plane using directives at the top of Program.cs, alongside the existing directives:
using Microsoft.Azure.ContainerApps.Sandbox;
using Microsoft.Azure.ContainerApps.Sandbox.Models;
Then append the following code to Program.cs. It uses the same credential and group configuration to create a sandbox through the data plane, run a command, and delete the sandbox.
var groupClient = new SandboxGroupClient(
region: region,
subscriptionId: subscriptionId,
resourceGroupName: resourceGroupName,
sandboxGroupName: sandboxGroupName,
credential: credential);
var content = new CreateSandboxContent
{
SourcesRef = new SandboxSource
{
DiskImage = new SandboxSourceDiskImage
{
Name = "ubuntu",
IsPublic = true
}
},
Resources = new SandboxResources("1000m", "2048Mi")
};
SandboxResource sandbox = (await groupClient.CreateSandboxAsync(content)).Value;
try
{
var command = new ExecuteSandboxCommandContent("/bin/echo");
command.Arguments.Add("Hello from ACA Sandbox.");
SandboxExecuteCommandResult result =
(await sandbox.ExecuteCommandAsync(command)).Value;
if (result.ExitCode != 0)
{
throw new InvalidOperationException(
$"Command failed with exit code {result.ExitCode}: {result.StandardError}");
}
Console.WriteLine(result.StandardOutput);
}
finally
{
await sandbox.DeleteAsync();
}
The region-based constructor selects the regional data-plane endpoint. CreateSandboxAsync returns a SandboxResource; use that resource to execute commands and delete the sandbox.
Run the project in the same shell:
- Bash
- PowerShell
dotnet run
dotnet run
The program prints the sandbox group's resource ID, then Hello from ACA Sandbox., and deletes the sandbox, even if command execution fails.
Other operations
List sandboxes in the group with GetSandboxesAsync. Add this code after the SandboxGroupClient initialization in Program.cs:
await foreach (SandboxResource item in groupClient.GetSandboxesAsync())
{
Console.WriteLine($"{item.Id}: {item.Data?.State}");
}
Clean up
The finally block deletes only the sandbox created by this quickstart. The sandbox group and resource group remain, along with any other sandboxes.
If the program is interrupted before cleanup finishes, delete the leftover sandbox from the sandboxes portal.