Prerequisites
- Azure subscription
- Azure CLI installed
- .NET Core SDK (or Python/Node) installed
Getting Started
These steps show how to create a Batch account, pool, and a simple job that runs a task.
az batch account create --name myBatchAccount --resource-group MyResourceGroup --location eastus
az batch account login --name myBatchAccount --resource-group MyResourceGroup
az batch pool create --id myPool --vm-size Standard_A1_v2 --target-dedicated-nodes 2 --image canonical:ubuntuserver:18.04-LTS:latest
az batch job create --id myJob --pool-id myPool
az batch task create --job-id myJob --task-id myTask --command-line "/bin/bash -c 'echo Hello Batch! > output.txt'"
az batch task file download --job-id myJob --task-id myTask --file-path output.txt --download-path .
Sample .NET Core Application
Below is a minimal C# program that submits a job to Azure Batch.
using System;
using System.Threading.Tasks;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Auth;
class Program
{
static async Task Main()
{
string batchUrl = "https://mybatchaccount.region.batch.azure.com";
string batchKey = "";
string poolId = "myPool";
BatchSharedKeyCredentials cred = new BatchSharedKeyCredentials(batchUrl, "myBatchAccount", batchKey);
using BatchClient client = BatchClient.Open(cred);
CloudJob job = client.JobOperations.CreateJob();
job.Id = "myJob";
job.PoolInformation = new PoolInformation { PoolId = poolId };
await job.CommitAsync();
CloudTask task = new CloudTask("myTask", "cmd /c echo Hello from Azure Batch!");
await client.TaskOperations.AddTaskAsync(job.Id, task);
Console.WriteLine("Task submitted.");
}
}