Using azure batch with subnet
TypeScriptIn order to work with Azure Batch and create a compute environment for running parallel and high-performance computing applications, you need to define a Batch account, a Batch pool, and associate it with a subnet. The latter is often necessary for network security and isolation purposes. Below is a detailed breakdown of how we will achieve this with Pulumi in TypeScript.
Firstly, you will need to create a Batch account which is the top-level resource within the Batch service, and is associated with your subscription. This account gives you access to the Batch service, and contains a link to your storage account.
Secondly, the Batch Pool will be defined. A Batch pool is a collection of compute nodes (virtual machines). Applications will run on these compute nodes.
Finally, you will define a subnet using the
azure-native.network.Subnet
resource. This subnet will be linked to the virtual network and has several properties that can be set according to your needs, such as the address prefix and network security group. Importantly, the Batch pool needs to be associated with a subnet for network isolation.The following Pulumi program creates a subnet and then sets up an Azure Batch account and pool using this subnet:
import * as pulumi from "@pulumi/pulumi"; import * as azureNative from "@pulumi/azure-native"; // Create a Resource Group if it doesn't exist const resourceGroup = new azureNative.resources.ResourceGroup("batchResourceGroup", { resourceGroupName: "myResourceGroup", }); // Create a Virtual Network for the Subnet const virtualNetwork = new azureNative.network.VirtualNetwork("batchVnet", { resourceGroupName: resourceGroup.name, addressSpace: { addressPrefixes: ["10.2.0.0/16"], }, }); // Create a Subnet in our Virtual Network to be used by the Batch service const batchSubnet = new azureNative.network.Subnet("batchSubnet", { resourceGroupName: resourceGroup.name, virtualNetworkName: virtualNetwork.name, addressPrefix: "10.2.0.0/24", // Subnet properties can be customized as required }); // Create an Azure Batch Account const batchAccount = new azureNative.batch.Account("batchAccount", { resourceGroupName: resourceGroup.name, location: resourceGroup.location, poolAllocationMode: azureNative.batch.PoolAllocationMode.UserSubscription, }); // Create a Batch Pool within the Batch Account const batchPool = new azureNative.batch.Pool("batchPool", { resourceGroupName: resourceGroup.name, accountName: batchAccount.name, vmSize: "STANDARD_A1_v2", // Example VM size networkConfiguration: { subnetId: batchSubnet.id, }, scaleSettings: { fixedScale: { targetDedicatedNodes: 1, targetLowPriorityNodes: 1, } }, // Other properties such as VM Image, start tasks, etc. can be added as needed }); // Export the Batch account name and pool name export const batchAccountName = batchAccount.name; export const batchPoolName = batchPool.name;
To understand what's happening in this program:
- We import the necessary modules from Pulumi's Azure-Native provider.
- A new Azure Resource Group is created to hold all the resources.
- We then define a Virtual Network, which is required to create a subnet in Azure.
- A subnet is created in the Virtual Network, and we allocate an address range for this subnet.
- Next, we set up an Azure Batch Account specifying its location and allocation mode.
- A Batch Pool is created within the Batch Account, specifying the VM size and the subnet it should be associated with. Here we use a simple scale setting, but this can be customized to fit your scaling requirements.
- Finally, we export the names of the created Batch account and pool as stack outputs, which can be useful for integration with other systems or for reference.
Once you've written this code in a
index.ts
file, you can deploy it using the Pulumi CLI with the following commands:pulumi up # Preview and deploy the changes pulumi stack output # View the stack outputs pulumi destroy # Destroy the resources
This program assumes you have the Azure CLI installed and configured with appropriate credentials and a selected subscription. If this is not the case, you'll need to set up the Azure provider with Pulumi.
The VM size, the number of nodes, and other settings can be customized based on your specific workload requirements. This code provides a starting point that you can extend and adapt for your scenarios.