title | description | author | ms.service | ms.collection | ms.topic | ms.date | ms.author | ms.custom | content_well_notification | ai-usage | |
---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart: Create a Windows VM cluster in Azure using Terraform |
In this article, you learn how to create a Windows VM cluster in Azure using Terraform |
tomarchermsft |
virtual-machines |
windows |
quickstart |
07/24/2023 |
tarcher |
devx-track-terraform |
|
ai-assisted |
Applies to: ✔️ Windows VMs
This article shows you how to create a Windows VM cluster (containing three Windows VM instances) in Azure using Terraform.
[!div class="checklist"]
- Create a random value for the Azure resource group name using random_pet.
- Create an Azure resource group using azurerm_resource_group.
- Create a random value for the Windows VM host name random_string.
- Create a random password for the Windows VMs using random_password.
- Create a Windows VM using the compute module.
- Create a virtual network along with subnet using the network module.
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 the sample Terraform code and make it the current directory.
-
Create a file named
providers.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-windows/providers.tf":::
-
Create a file named
main.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-windows/main.tf":::
-
Create a file named
variables.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-windows/variables.tf":::
-
Create a file named
outputs.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-windows/outputs.tf":::
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-plan.md]
[!INCLUDE terraform-apply-plan.md]
Cost information isn't presented during the virtual machine creation process for Terraform like it is for the Azure portal. If you want to learn more about how cost works for virtual machines, see the Cost optimization Overview page.
-
Get the Azure resource group name.
resource_group_name=$(terraform output -raw resource_group_name)
-
Run az vm list with a JMESPath query to display the names of the virtual machines created in the resource group.
az vm list \ --resource-group $resource_group_name \ --query "[].{\"VM Name\":name}" -o table
-
Get the Azure resource group name.
$resource_group_name=$(terraform output -raw resource_group_name)
-
Run Get-AzVm to display the names of all the virtual machines in the resource group.
Get-AzVm -ResourceGroupName $resource_group_name
[!INCLUDE terraform-plan-destroy.md]
Troubleshoot common problems when using Terraform on Azure
[!div class="nextstepaction"] Azure Linux virtual machine tutorials