Search code examples
azureazure-batch

Upload Azure Batch Job Application Package programmatically


I have found how to upload/manage Azure Batch job Application Packages through the UI:

https://learn.microsoft.com/en-us/azure/batch/batch-application-packages

And how to upload and manage Resource Packages programmatically:

https://github.com/Azure/azure-batch-samples/tree/master/CSharp/GettingStarted/02_PoolsAndResourceFiles

But I can't quite seem to put 2 and 2 together on how to manage Application Packages programmatically. Is there an API endpoint we can call to upload/manage an Application Package when setting up a batch job?


Solution

  • Since this is not quite straightforward, I'll write down my findings. These are the steps to programmatically upload Application Packages via an application that is unattended - no user input (e.g. Azure credentials) is needed.

    In Azure Portal:

    • Create the Azure Batch application
    • Create a new Azure AD application (as Application Type use Web app / API)
    • Follow these steps to create the secret key and assign the role to the Azure Batch account
    • Note down the following credentials/ids:
      • Azure AD application id
      • Azure AD application secret key
      • Azure AD tenant id
      • Subscription id
      • Batch account name
      • Batch account resource group name

    In your code:

    Put together the whole code looks something like this:

    private const string ResourceUri = "https://management.core.windows.net/";
    private const string AuthUri = "https://login.microsoftonline.com/" + "{TenantId}";
    private const string ApplicationId = "{ApplicationId}";
    private const string ApplicationSecretKey = "{ApplicationSecretKey}";
    private const string SubscriptionId = "{SubscriptionId}";
    private const string ResourceGroupName = "{ResourceGroupName}";
    private const string BatchAccountName = "{BatchAccountName}";
    
    private async Task UploadApplicationPackageAsync() {
        // get the access token
        var authContext = new AuthenticationContext(AuthUri);
        var authResult = await authContext.AcquireTokenAsync(ResourceUri, new ClientCredential(ApplicationId, ApplicationSecretKey)).ConfigureAwait(false);
    
        // create the BatchManagementClient and set the subscription id
        var bmc = new BatchManagementClient(new TokenCredentials(authResult.AccessToken)) {
            SubscriptionId = SubscriptionId
        };
    
        // create the application package
        var createResult = await bmc.ApplicationPackage.CreateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0").ConfigureAwait(false);
    
        // upload the package to the blob storage
        var cloudBlockBlob = new CloudBlockBlob(new Uri(createResult.Body.StorageUrl));
        cloudBlockBlob.Properties.ContentType = "application/x-zip-compressed";
        await cloudBlockBlob.UploadFromFileAsync("myZip.zip").ConfigureAwait(false);
    
        // create the application package
        var activateResult = await bmc.ApplicationPackage.ActivateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0", "zip").ConfigureAwait(false);
    }