Skip to content

Latest commit

 

History

History
136 lines (89 loc) · 6.79 KB

quick-create-terraform.md

File metadata and controls

136 lines (89 loc) · 6.79 KB
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-contribution
ai-assisted

Quickstart: Use Terraform to create a Linux VM

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"]

Prerequisites

Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

    :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/providers.tf":::

  3. Create a file named ssh.tf and insert the following code:

    :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/ssh.tf":::

  4. Create a file named main.tf and insert the following code:

    :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/main.tf":::

  5. Create a file named variables.tf and insert the following code:

    :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/variables.tf":::

  6. Create a file named outputs.tf and insert the following code:

    :::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/outputs.tf":::

Initialize Terraform

[!INCLUDE terraform-init.md]

Create a Terraform execution plan

[!INCLUDE terraform-plan.md]

Apply a Terraform execution plan

[!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.

Verify the results

  1. Get the Azure resource group name.

    resource_group_name=$(terraform output -raw resource_group_name)
  2. 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
    
  1. Get the Azure resource group name.

    $resource_group_name=$(terraform output -raw resource_group_name)
  2. Run Get-AzVm to display the names of all the virtual machines in the resource group.

    Get-AzVm -ResourceGroupName $resource_group_name
    

Clean up resources

[!INCLUDE terraform-plan-destroy.md]

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps

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