Quickstart: Create a lab in Azure DevTest Labs using Terraform
This article shows how to use Terraform to create a Windows Server 2019 Datacenter virtual machine in a lab within Azure DevTest Labs using Terraform.
In this article, you learn how to:
- Create a random pet name for the Azure resource group name using random_pet
- Create an Azure resource group using azurerm_resource_group
- Create a random password using random_password
- Create a lab within Azure DevTest Labs using azurerm_dev_test_lab
- Create a virtual network within Azure DevTest Labs using azurerm_dev_test_virtual_network
- Create a Windows virtual machine within Azure DevTest Labs using azurerm_dev_test_windows_virtual_machine
Prerequisites
Implement the Terraform code
Note
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.
See more articles and sample code showing how to use Terraform to manage Azure resources
Create a directory in which to test and run the sample Terraform code and make it the current directory.
Create a file named
main.tf
and insert the following code:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "random_string" "vm_suffix" { length = 5 upper = false special = false numeric = false } resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id location = var.resource_group_location } resource "random_password" "password" { count = var.password == null ? 1 : 0 length = 20 special = true min_numeric = 1 min_upper = 1 min_lower = 1 min_special = 1 } locals { password = try(random_password.password[0].result, var.password) } resource "azurerm_dev_test_lab" "lab" { name = var.lab_name location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_dev_test_virtual_network" "vnet" { name = "Dtl${var.lab_name}" lab_name = azurerm_dev_test_lab.lab.name resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_dev_test_windows_virtual_machine" "vm" { name = "ExampleVM-${random_string.vm_suffix.result}" lab_name = azurerm_dev_test_lab.lab.name lab_subnet_name = "Dtl${var.lab_name}Subnet" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location storage_type = "Standard" size = var.vm_size username = var.user_name password = local.password allow_claim = false lab_virtual_network_id = azurerm_dev_test_virtual_network.vnet.id gallery_image_reference { offer = "WindowsServer" publisher = "MicrosoftWindowsServer" sku = "2019-Datacenter" version = "latest" } }
Create a file named
outputs.tf
and insert the following code:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "lab_name" { value = azurerm_dev_test_lab.lab.name } output "vm_name" { value = azurerm_dev_test_windows_virtual_machine.vm.name } output "password" { sensitive = true value = local.password }
Create a file named
providers.tf
and insert the following code:terraform { required_version = ">=0.12" 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:variable "resource_group_location" { type = string default = "eastus" description = "Location for all resources." } 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." } variable "lab_name" { type = string description = "The name of the new lab instance to be created" default = "ExampleLab" } variable "vm_size" { type = string description = "The size of the vm to be created." default = "Standard_D4_v3" } variable "user_name" { type = string description = "The username for the local account that will be created on the new vm." default = "exampleuser" } variable "password" { type = string description = "The password for the local account that will be created on the new vm." sensitive = true default = null }
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
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.
Create a Terraform execution plan
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Key points:
- 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.
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
Verify the results
Get the Azure resource name in which the lab was created.
resource_group_name=$(terraform output -raw resource_group_name)
Get the lab name.
lab_name=$(terraform output -raw lab_name)
Run az lab vm list to list the virtual machines for the lab you created in this article.
az lab vm list --resource-group $resource_group_name \ --lab-name $lab_name
Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.terraform plan -destroy -out main.destroy.tfplan
Key points:
- 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.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure