Raw New Markdown
Generating updated version of doc...
Rendered New Markdown
Generating updated version of doc...
---
title: 'Quickstart: Create a Linux VM cluster in Azure using Terraform'
description: In this article, you learn how to create a Linux VM cluster in Azure using Terraform
author: tomarchermsft
ms.service: azure-virtual-machines
ms.collection: linux
ms.topic: quickstart
ms.date: 07/24/2023
ms.author: tarcher
ms.custom: devx-track-terraform, linux-related-content
content_well_notification:
- AI-contribution
ai-usage: ai-assisted
# Customer intent: "As a cloud administrator, I want to create a Linux VM cluster using Terraform, so that I can efficiently manage and automate the deployment of infrastructure in Azure."
---
# Quickstart: Create a Linux VM cluster in Azure using Terraform
**Applies to:** :heavy_check_mark: Linux VMs
This article shows you how to create a Linux VM cluster (containing two Linux VM instances) in Azure using Terraform.
In this article, you learn how to:
> [!div class="checklist"]
> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet).
> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group).
> * Create a virtual network using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network)
> * Create a subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet)
> * Create a public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip)
> * Create a load balancer using [azurerm_lb](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb)
> * 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)
> * Create a network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface)
> * Create a managed disk using [azurerm_managed_disk](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk)
> * Create a availability set using [azurerm_availability_set](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/availability_set)
> * Create a Linux virtual machine using [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine)
> * Create an AzAPI resource [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource).
> * 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).
## Prerequisites
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
## Implement the Terraform code
> [!NOTE]
> 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).
>
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
1. Create a directory in which to test the sample Terraform code and make it the current directory.
1. Create a file named `providers.tf` and insert the following code:
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/providers.tf":::
1. Create a file named `ssh.tf` and insert the following code:
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/ssh.tf":::
1. Create a file named `main.tf` and insert the following code:
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/main.tf":::
1. Create a file named `variables.tf` and insert the following code:
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/variables.tf":::
1. Create a file named `outputs.tf` and insert the following code:
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-cluster-linux/outputs.tf":::
## Initialize Terraform
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
## Create a Terraform execution plan
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
## Apply a Terraform execution plan
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
Cost information isn't presented during the virtual machine creation process for Terraform like it is for the [Azure portal](quick-create-portal.md). If you want to learn more about how cost works for virtual machines, see the [Cost optimization Overview page](../plan-to-manage-costs.md).
## Verify the results
#### [Azure CLI](#tab/azure-cli)
1. Get the Azure resource group name.
```console
resource_group_name=$(terraform output -raw resource_group_name)
```
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.
```azurecli
az vm list \
--resource-group $resource_group_name \
--query "[].{\"VM Name\":name}" -o table
```
#### [Azure PowerShell](#tab/azure-powershell)
1. Get the Azure resource group name.
```console
$resource_group_name=$(terraform output -raw resource_group_name)
```
1. Run [Get-AzVm](/powershell/module/az.compute/get-azvm) to display the names of all the virtual machines in the resource group.
```azurepowershell
Get-AzVm -ResourceGroupName $resource_group_name
```
---
## Clean up resources
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
## Troubleshoot Terraform on Azure
[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot)
## Next steps
> [!div class="nextstepaction"]
> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md)