|
| 1 | +--- |
| 2 | +title: 'Quickstart: Use Terraform to create a Linux VM' |
| 3 | +description: In this quickstart, you learn how to use Terraform to create a Linux virtual machine |
| 4 | +author: tomarchermsft |
| 5 | +ms.service: virtual-machines |
| 6 | +ms.collection: linux |
| 7 | +ms.topic: quickstart |
| 8 | +ms.workload: infrastructure |
| 9 | +ms.date: 08/31/2022 |
| 10 | +ms.author: tarcher |
| 11 | +ms.custom: devx-track-terraform |
| 12 | +--- |
| 13 | + |
| 14 | +# Quickstart: Use Terraform to create a Linux VM |
| 15 | + |
| 16 | +**Applies to:** :heavy_check_mark: Linux VMs |
| 17 | + |
| 18 | +Article tested with the following Terraform and Terraform provider versions: |
| 19 | + |
| 20 | +- [Terraform v1.2.7](https://releases.hashicorp.com/terraform/) |
| 21 | +- [AzureRM Provider v.3.20.0](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs) |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 26 | + |
| 27 | +In this article, you learn how to: |
| 28 | +> [!div class="checklist"] |
| 29 | +
|
| 30 | +> * Create a virtual network |
| 31 | +> * Create a subnet |
| 32 | +> * Create a public IP address |
| 33 | +> * Create a network security group and SSH inbound rule |
| 34 | +> * Create a virtual network interface card |
| 35 | +> * Connect the network security group to the network interface |
| 36 | +> * Create a storage account for boot diagnostics |
| 37 | +> * Create SSH key |
| 38 | +> * Create a virtual machine |
| 39 | +> * Use SSH to connect to virtual machine |
| 40 | +
|
| 41 | +> [!NOTE] |
| 42 | +> The example code in this article is located in the [Microsoft Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-vm-with-infrastructure). See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 43 | +
|
| 44 | +## Prerequisites |
| 45 | + |
| 46 | +[!INCLUDE [open-source-devops-prereqs-azure-subscription.md](~/azure-dev-docs-pr/articles/includes/open-source-devops-prereqs-azure-subscription.md)] |
| 47 | + |
| 48 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 49 | + |
| 50 | +## Implement the Terraform code |
| 51 | + |
| 52 | +1. Create a directory in which to test the sample Terraform code and make it the current directory. |
| 53 | + |
| 54 | +1. Create a file named `providers.tf` and insert the following code: |
| 55 | + |
| 56 | + [!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/providers.tf)] |
| 57 | + |
| 58 | +1. Create a file named `main.tf` and insert the following code: |
| 59 | + |
| 60 | + [!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/main.tf)] |
| 61 | + |
| 62 | +1. Create a file named `variables.tf` and insert the following code: |
| 63 | + |
| 64 | + [!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/variables.tf)] |
| 65 | + |
| 66 | +1. Create a file named `outputs.tf` and insert the following code: |
| 67 | + |
| 68 | + [!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/outputs.tf)] |
| 69 | + |
| 70 | +## Initialize Terraform |
| 71 | + |
| 72 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 73 | + |
| 74 | +## Create a Terraform execution plan |
| 75 | + |
| 76 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 77 | + |
| 78 | +## Apply a Terraform execution plan |
| 79 | + |
| 80 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 81 | + |
| 82 | +## Verify the results |
| 83 | + |
| 84 | +To use SSH to connect to the virtual machine, do the following steps: |
| 85 | + |
| 86 | +1. Run [terraform output](https://www.terraform.io/cli/commands/output) to get the SSH private key and save it to a file. |
| 87 | + |
| 88 | + ```console |
| 89 | + terraform output -raw tls_private_key > id_rsa |
| 90 | + ``` |
| 91 | + |
| 92 | +1. Run [terraform output](https://www.terraform.io/cli/commands/output) to get the virtual machine public IP address. |
| 93 | + |
| 94 | + ```console |
| 95 | + terraform output public_ip_address |
| 96 | + ``` |
| 97 | + |
| 98 | +1. Use SSH to connect to the virtual machine. |
| 99 | + |
| 100 | + ```console |
| 101 | + ssh -i id_rsa azureuser@<public_ip_address> |
| 102 | + ``` |
| 103 | + |
| 104 | + **Key points:** |
| 105 | + - Depending on the permissions of your environment, you might get an error when trying to ssh into the virtual machine using the `id_rsa` key file. If you get an error stating that the private key file is unprotected and can't be used, try running the following command: `chmod 600 id_rsa`, which will restrict read and write access to the owner of the file. |
| 106 | + |
| 107 | +## Clean up resources |
| 108 | + |
| 109 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 110 | + |
| 111 | +## Troubleshoot Terraform on Azure |
| 112 | + |
| 113 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 114 | + |
| 115 | +## Next steps |
| 116 | + |
| 117 | +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. |
| 118 | + |
| 119 | +> [!div class="nextstepaction"] |
| 120 | +> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md) |
0 commit comments