title | description | author | ms.service | ms.collection | ms.topic | ms.date | ms.author | ms.custom | content_well_notification | ai-usage | |
---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart: Use Terraform to create a Linux VM |
In this quickstart, you learn how to use Terraform to create a Linux virtual machine |
tomarchermsft |
virtual-machines |
linux |
quickstart |
07/24/2023 |
tarcher |
devx-track-terraform, linux-related-content |
|
ai-assisted |
Applies to: ✔️ Linux VMs
Article tested with the following Terraform and Terraform provider versions:
This article shows you how to create a complete Linux environment and supporting resources with Terraform. Those resources include a virtual network, subnet, public IP address, and more.
[!INCLUDE Terraform abstract]
In this article, you learn how to:
[!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 virtual network (VNET) using azurerm_virtual_network.
- Create a subnet using azurerm_subnet.
- Create a public IP using azurerm_public_ip.
- Create a network security group using azurerm_network_security_group.
- Create a network interface using azurerm_network_interface.
- Create an association between the network security group and the network interface using azurerm_network_interface_security_group_association.
- Generate a random value for a unique storage account name using random_id.
- Create a storage account for boot diagnostics using azurerm_storage_account.
- Create a Linux VM using azurerm_linux_virtual_machine
- Create an AzAPI resource azapi_resource.
- Create an AzAPI resource to generate an SSH key pair using azapi_resource_action.
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-with-infrastructure/providers.tf":::
-
Create a file named
ssh.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/ssh.tf":::
-
Create a file named
main.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/main.tf":::
-
Create a file named
variables.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/variables.tf":::
-
Create a file named
outputs.tf
and insert the following code::::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/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
In this quickstart, you deployed a simple virtual machine using Terraform. To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.
[!div class="nextstepaction"] Azure Linux virtual machine tutorials