|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a lab in Azure DevTest Labs using Terraform' |
| 3 | +description: 'In this article, you create a Windows virtual machine in a lab within Azure DevTest Labs using Terraform' |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 2/27/2023 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +author: TomArcherMsft |
| 8 | +ms.author: tarcher |
| 9 | +--- |
| 10 | + |
| 11 | +# Quickstart: Create a lab in Azure DevTest Labs using Terraform |
| 12 | + |
| 13 | +This article shows how to use Terraform to create a Windows Server 2019 Datacenter virtual machine in a lab within [Azure DevTest Labs](/azure/devtest-labs/devtest-lab-overview) using [Terraform](/azure/developer/terraform). |
| 14 | + |
| 15 | + |
| 16 | +This quickstart uses Bicep to create a lab in Azure DevTest Labs that has one Windows Server 2019 Datacenter virtual machine (VM) in it. |
| 17 | + |
| 18 | + |
| 19 | +In this article, you learn how to: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Create a random pet name for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
| 23 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 24 | +> * Create a random password using [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) |
| 25 | +> * Create a lab within Azure DevTest Labs using [azurerm_dev_test_lab](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dev_test_lab) |
| 26 | +> * Create a virtual network within Azure DevTest Labs using [azurerm_dev_test_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dev_test_virtual_network) |
| 27 | +> * Create a Windows virtual machine within Azure DevTest Labs using [azurerm_dev_test_windows_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dev_test_windows_virtual_machine) |
| 28 | +
|
| 29 | +[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)] |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 34 | + |
| 35 | +## Implement the Terraform code |
| 36 | + |
| 37 | +> [!NOTE] |
| 38 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-devtest-labs). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-devtest-labs/TestRecord.md). |
| 39 | +> |
| 40 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 41 | +
|
| 42 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 43 | + |
| 44 | +1. Create a file named `main.tf` and insert the following code: |
| 45 | + |
| 46 | + [!code-terraform[master](~/terraform_samples/quickstart/101-devtest-labs/main.tf)] |
| 47 | + |
| 48 | +1. Create a file named `outputs.tf` and insert the following code: |
| 49 | + |
| 50 | + [!code-terraform[master](~/terraform_samples/quickstart/101-devtest-labs/outputs.tf)] |
| 51 | + |
| 52 | +1. Create a file named `providers.tf` and insert the following code: |
| 53 | + |
| 54 | + [!code-terraform[master](~/terraform_samples/quickstart/101-devtest-labs/providers.tf)] |
| 55 | + |
| 56 | +1. Create a file named `variables.tf` and insert the following code: |
| 57 | + |
| 58 | + [!code-terraform[master](~/terraform_samples/quickstart/101-devtest-labs/variables.tf)] |
| 59 | + |
| 60 | +## Initialize Terraform |
| 61 | + |
| 62 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 63 | + |
| 64 | +## Create a Terraform execution plan |
| 65 | + |
| 66 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 67 | + |
| 68 | +## Apply a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 71 | + |
| 72 | +## Verify the results |
| 73 | + |
| 74 | +1. Get the Azure resource name in which the lab was created. |
| 75 | + |
| 76 | + ```azurecli |
| 77 | + echo "$(terraform output resource_group_name)" |
| 78 | + ``` |
| 79 | +
|
| 80 | +1. Get the lab name. |
| 81 | +
|
| 82 | + ```azurecli |
| 83 | + echo "$(terraform output lab_name)" |
| 84 | + ``` |
| 85 | +
|
| 86 | +1. Run [az lab vm list](/cli/azure/lab/vm#az-lab-vm-list) to list the virtual machines for the lab you created in this article. |
| 87 | +
|
| 88 | + ```azurecli |
| 89 | + az lab vm list --resource-group <resource_group_name> --lab-name <lab_name> |
| 90 | + ``` |
| 91 | +
|
| 92 | +## Clean up resources |
| 93 | +
|
| 94 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 95 | +
|
| 96 | +## Troubleshoot Terraform on Azure |
| 97 | +
|
| 98 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 99 | +
|
| 100 | +## Next steps |
| 101 | +
|
| 102 | +> [!div class="nextstepaction"] |
| 103 | +> [Tutorial: Work with lab VMs](../tutorial-use-custom-lab.md) |
0 commit comments