Skip to content

Latest commit

 

History

History
115 lines (76 loc) · 6.23 KB

File metadata and controls

115 lines (76 loc) · 6.23 KB
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-contribution
ai-usage ai-assisted

Quickstart: Create a lab and VM using Terraform

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.

Prerequisites

  • 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.

Create the lab and VM

The sample code this article references is located in the Azure Terraform GitHub repository. The Terraform code takes the following actions:

Create the code files

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_reference to 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 default values for some variables like resource_group_location or vm_size if you need to use different values. [!code-terraformmaster]

Initialize Terraform

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 -upgrade

Create the Terraform execution plan

Run 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.tfplan

Apply the Terraform execution plan

Run 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.tfplan

If 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.

Verify the results

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

Clean up resources

When you no longer need the resources Terraform created, take the following steps to remove them:

  1. Run terraform plan with the destroy flag. The terraform plan command creates the execution plan but doesn't execute it. The -out parameter specifies an output file for the plan named main.destroy.tfplan.

    terraform plan -destroy -out main.destroy.tfplan
  2. Run terraform apply to apply the execution plan specified in the main.destroy.tfplan file.

    terraform apply main.destroy.tfplan

Next step

[!div class="nextstepaction"] Access and connect to lab VMs

Related content