Deploy an Azure Batch account and two pools with a start task - Terraform
Article
In this quickstart, you create an Azure Batch account, an Azure Storage account, and two Batch pools using Terraform. Batch is a cloud-based job scheduling service that parallelizes and distributes the processing of large volumes of data across many computers. It's typically used for tasks like rendering 3D graphics, analyzing large datasets, or processing video. In this case, the resources created include a Batch account (which is the central organizing entity for distributed processing tasks), a Storage account for holding the data to be processed, and two Batch pools, which are groups of virtual machines that execute the tasks.
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.
Specify the required version of Terraform and the required providers.
Define the Azure provider with no additional features.
Define variables for the resource group location and name prefix.
Generate a random name for the Azure resource group.
Create a resource group with the generated name at a specified location.
Generate a random string for the Storage account name.
Create a Storage account with the generated name in the created resource group.
Generate a random string for the Batch account name.
Create a Batch account with the generated name in the created resource group and linked to the created Storage account.
Generate a random name for the Batch pool.
Create a Batch pool with a fixed scale in the created resource group and linked to the created Batch account.
Create a Batch pool with autoscale in the created resource group and linked to the created Batch account.
Output the names of the created resource group, Storage account, Batch account, and both Batch pools.
Create a file named outputs.tf, and insert the following code:
Terraform
output"resource_group_name" {
value = azurerm_resource_group.rg.name
}
output"storage_account_name" {
value = azurerm_storage_account.example.name
}
output"batch_account_name" {
value = azurerm_batch_account.example.name
}
output"batch_pool_fixed_name" {
value = azurerm_batch_pool.fixed.name
}
output"batch_pool_autopool_name" {
value = azurerm_batch_pool.autopool.name
}
Create a file named providers.tf, and insert the following code:
Terraform
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider"azurerm" {
features {}
}
Create a file named variables.tf, and insert the following code:
Terraform
variable"resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable"resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
Console
terraform init -upgrade
Key points:
The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
Console
terraform apply main.tfplan
Key points:
The example terraform apply command assumes you previously ran terraform plan -out main.tfplan.
If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
If you didn't use the -out parameter, call terraform apply without any parameters.
The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
Azure Batch is a service that enables you to run large-scale parallel and high-performance computing (HPC) applications efficiently in the cloud. There's no need to manage or configure infrastructure. Just schedule the job, allocate the resources you need, and let Batch take care of the rest.
Azure Batch runs large-scale applications efficiently in the cloud. Schedule compute-intensive tasks and dynamically adjust resources for your solution without managing infrastructure.