| title | Quickstart: Create a lab and VM using Terraform | |
|---|---|---|
| description | Learn to use Terraform to create a lab and a Windows virtual machine (VM) in Azure DevTest Labs. | |
| ms.topic | quickstart | |
| ms.date | 04/02/2025 | |
| ms.custom | devx-track-terraform, UpdateFrequency2 | |
| author | TomArcherMsft | |
| ms.author | tarcher | |
| content_well_notification |
|
|
| ai-usage | ai-assisted |
Terraform is an infrastructure as code tool that helps you build and manage cloud resources. This article shows how to use Terraform to create a lab containing a Windows Server 2019 Datacenter virtual machine (VM) in Azure DevTest Labs.
- Owner or Contributor-level permissions in the Azure subscription where you want to create the lab.
- Terraform installed and configured, locally or in Azure Cloud Shell.
The sample code this article references is located in the Azure Terraform GitHub repository. The Terraform code takes the following actions:
- Creates a random pet name for the Azure resource group using random_pet
- Creates an Azure resource group using azurerm_resource_group
- Creates a random password using random_password
- Creates a lab using azurerm_dev_test_lab
- Creates a virtual network for the lab using azurerm_dev_test_virtual_network
- Creates a Windows VM in the lab using azurerm_dev_test_windows_virtual_machine
Create the following files in your Terraform directory. Make sure the directory is added to your PATH.
-
A file named main.tf that contains the following code. You can change the
gallery_image_referenceto create different types of VMs. [!code-terraformmaster] -
A file named outputs.tf that contains the following code: [!code-terraformmaster]
-
A file named providers.tf that contains the following code: [!code-terraformmaster]
-
A file named variables.tf that contains the following code. You can change the
defaultvalues for some variables likeresource_group_locationorvm_sizeif you need to use different values. [!code-terraformmaster]
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources. The -upgrade parameter upgrades the provider plugins to the newest supported version.
terraform init -upgradeRun terraform plan to create an execution plan. 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. Use the optional -out parameter to specify an output file named main.tfplan for the plan. You can review the output file to ensure that the plan is exactly what you want to apply.
terraform plan -out main.tfplanRun terraform apply to apply the execution plan to your cloud infrastructure. The following terraform apply command assumes you previously ran terraform plan -out main.tfplan.
terraform apply main.tfplanIf you specify a different filename for the -out parameter in terraform_plan, use that filename in the call to terraform apply. If you don't use the -out parameter in terraform_plan, call terraform apply without any parameters.
There are several ways to verify the results of the Terraform deployment. If you have Azure CLI available, you can use az lab vm list to get the names of the resource group and lab that Terraform created.
resource_group_name=$(terraform output -raw resource_group_name)
lab_name=$(terraform output -raw lab_name)
az lab vm list --resource-group $resource_group_name --lab-name $lab_name
When you no longer need the resources Terraform created, take the following steps to remove them:
-
Run terraform plan with the
destroyflag. Theterraform plancommand creates the execution plan but doesn't execute it. The-outparameter specifies an output file for the plan namedmain.destroy.tfplan.terraform plan -destroy -out main.destroy.tfplan -
Run terraform apply to apply the execution plan specified in the
main.destroy.tfplanfile.terraform apply main.destroy.tfplan
[!div class="nextstepaction"] Access and connect to lab VMs