Deploy an Azure Batch account and two pools - Terraform
Artikkeli
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 parametric sweeps, Monte Carlo simulations, financial risk modeling, and other high-performance computing applications. A Batch account is the top-level resource in the Batch service that provides access to pools, jobs, and tasks. The Storage account is used to store and manage all the files that are used and generated by the Batch service, while the two Batch pools are collections of compute nodes 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 location of the resource group and the prefix of the resource group name.
Generate a random name for the resource group using the provided prefix.
Create an Azure resource group with the generated name at the specified location.
Generate a random string to be used as the name for the Storage account.
Create a Storage account with the generated name in the created resource group, at the same location, and with a standard account tier and locally redundant Storage replication type.
Generate another random string to be used as the name for the Batch account.
Create a Batch account with the generated name in the created resource group, at the same location, and link it to the created Storage account with Storage keys authentication mode.
Generate a random name for the Batch pool with a "pool" prefix.
Create a Batch pool with a fixed scale using the generated name in the created resource group, linked to the created Batch account, with a standard A1 virtual machine (VM) size, Ubuntu 22.04 node agent SKU, and a start task that echoes 'Hello World from $env' with a maximum of one retry and waits for success.
Create another Batch pool with auto scale, using the same generated name, in the created resource group, linked to the created Batch account, with a standard A1 VM size, Ubuntu 22.04 node agent SKU, and an autoscale formula.
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.
Konsoli
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.
Konsoli
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 on palvelu, jonka avulla voit suorittaa suuren mittakaavan rinnakkaisia ja suorituskykyisiä tietojenkäsittelysovelluksia tehokkaasti pilvipalvelussa. Sinun ei tarvitse hallita tai määrittää infrastruktuuria. Voit vain ajoittaa työn, varata tarvitsemasi resurssit ja antaa erän hoitaa loput.