|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a Linux VM cluster in Azure using Terraform' |
| 3 | +description: In this article, you learn how to create a Linux VM cluster in Azure using Terraform |
| 4 | +author: tomarchermsft |
| 5 | +ms.service: virtual-machines |
| 6 | +ms.collection: linux |
| 7 | +ms.topic: quickstart |
| 8 | +ms.workload: infrastructure |
| 9 | +ms.date: 07/24/2023 |
| 10 | +ms.author: tarcher |
| 11 | +ms.custom: devx-track-terraform |
| 12 | +content_well_notification: |
| 13 | + - AI-contribution |
| 14 | +--- |
| 15 | + |
| 16 | +# Quickstart: Create a Linux VM cluster in Azure using Terraform |
| 17 | + |
| 18 | +**Applies to:** :heavy_check_mark: Linux VMs |
| 19 | + |
| 20 | +This article shows you how to create a Linux VM cluster (containing two Linux VM instances) in Azure using Terraform. |
| 21 | + |
| 22 | +In this article, you learn how to: |
| 23 | + |
| 24 | +> [!div class="checklist"] |
| 25 | +> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet). |
| 26 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group). |
| 27 | +> * Create a virtual network using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) |
| 28 | +> * Create a subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) |
| 29 | +> * Create a public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) |
| 30 | +> * Create a load balancer using [azurerm_lb](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb) |
| 31 | +> * Create a load balancer address pool using [azurerm_lb_backend_address_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb_backend_address_pool) |
| 32 | +> * Create a network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) |
| 33 | +> * Create a managed disk using [azurerm_managed_disk](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk) |
| 34 | +> * Create a availability set using [azurerm_availability_set](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/availability_set) |
| 35 | +> * Create a Linux virtual machine using [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) |
| 36 | +> * Create an AzAPI resource [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource). |
| 37 | +> * Create an AzAPI resource to generate an SSH key pair using [azapi_resource_action](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource_action). |
| 38 | +
|
| 39 | +## Prerequisites |
| 40 | + |
| 41 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 42 | + |
| 43 | +## Implement the Terraform code |
| 44 | + |
| 45 | +> [!NOTE] |
| 46 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-vm-cluster-linux). 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-vm-cluster-linux/TestRecord.md). |
| 47 | +> |
| 48 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 49 | +
|
| 50 | +1. Create a directory in which to test the sample Terraform code and make it the current directory. |
| 51 | + |
| 52 | +1. Create a file named `providers.tf` and insert the following code: |
| 53 | + |
| 54 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/providers.tf"::: |
| 55 | + |
| 56 | +1. Create a file named `ssh.tf` and insert the following code: |
| 57 | + |
| 58 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/ssh.tf"::: |
| 59 | + |
| 60 | +1. Create a file named `main.tf` and insert the following code: |
| 61 | + |
| 62 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/main.tf"::: |
| 63 | + |
| 64 | +1. Create a file named `variables.tf` and insert the following code: |
| 65 | + |
| 66 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/variables.tf"::: |
| 67 | + |
| 68 | +1. Create a file named `outputs.tf` and insert the following code: |
| 69 | + |
| 70 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/outputs.tf"::: |
| 71 | + |
| 72 | +## Initialize Terraform |
| 73 | + |
| 74 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 75 | + |
| 76 | +## Create a Terraform execution plan |
| 77 | + |
| 78 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 79 | + |
| 80 | +## Apply a Terraform execution plan |
| 81 | + |
| 82 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 83 | + |
| 84 | +## Verify the results |
| 85 | + |
| 86 | +#### [Azure CLI](#tab/azure-cli) |
| 87 | + |
| 88 | +1. Get the Azure resource group name. |
| 89 | + |
| 90 | + ```console |
| 91 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 92 | + ``` |
| 93 | + |
| 94 | +1. Run [az vm list](/cli/azure/vm#az-vm-list) with a [JMESPath](/cli/azure/query-azure-cli) query to display the names of the virtual machines created in the resource group. |
| 95 | + |
| 96 | + ```azurecli |
| 97 | + az vm list \ |
| 98 | + --resource-group $resource_group_name \ |
| 99 | + --query "[].{\"VM Name\":name}" -o table |
| 100 | + ``` |
| 101 | + |
| 102 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 103 | + |
| 104 | +1. Get the Azure resource group name. |
| 105 | + |
| 106 | + ```console |
| 107 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 108 | + ``` |
| 109 | + |
| 110 | +1. Run [Get-AzVm](/powershell/module/az.compute/get-azvm) to display the names of all the virtual machines in the resource group. |
| 111 | + |
| 112 | + ```azurepowershell |
| 113 | + Get-AzVm -ResourceGroupName $resource_group_name |
| 114 | + ``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## Clean up resources |
| 119 | + |
| 120 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 121 | + |
| 122 | +## Troubleshoot Terraform on Azure |
| 123 | + |
| 124 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 125 | + |
| 126 | +## Next steps |
| 127 | + |
| 128 | +> [!div class="nextstepaction"] |
| 129 | +> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md) |
0 commit comments