|
| 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: azure-virtual-machines |
| 6 | +ms.collection: linux |
| 7 | +ms.topic: quickstart |
| 8 | +ms.date: 07/24/2023 |
| 9 | +ms.author: tarcher |
| 10 | +ms.custom: devx-track-terraform, linux-related-content, innovation-engine |
| 11 | +ai-usage: ai-assisted |
| 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 | +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. |
| 21 | + |
| 22 | +[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 23 | + |
| 24 | +In this article, you learn how to: |
| 25 | +> [!div class="checklist"] |
| 26 | +> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet). |
| 27 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group). |
| 28 | +> * Create a virtual network (VNET) using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network). |
| 29 | +> * Create a subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet). |
| 30 | +> * Create a public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip). |
| 31 | +> * Create a network security group using [azurerm_network_security_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group). |
| 32 | +> * Create a network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface). |
| 33 | +> * Create an association between the network security group and the network interface using [azurerm_network_interface_security_group_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association). |
| 34 | +> * Generate a random value for a unique storage account name using [random_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id). |
| 35 | +> * Create a storage account for boot diagnostics using [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account). |
| 36 | +> * Create a Linux VM using [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine). |
| 37 | +> * Create an AzAPI resource using [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource). |
| 38 | +> * 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). |
| 39 | +
|
| 40 | +## Prerequisites |
| 41 | + |
| 42 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 43 | + |
| 44 | +## Implement the Terraform code |
| 45 | + |
| 46 | +> [!NOTE] |
| 47 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-vm-with-infrastructure). 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-with-infrastructure/TestRecord.md). |
| 48 | +> |
| 49 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 50 | +
|
| 51 | +1. Create a directory in which to test the sample Terraform code and make it the current directory. |
| 52 | + |
| 53 | +1. Create a file named providers.tf and insert the following code: |
| 54 | + |
| 55 | +```bash |
| 56 | +cat <<'EOF' > providers.tf |
| 57 | +terraform { |
| 58 | + required_version = ">=0.12" |
| 59 | +
|
| 60 | + required_providers { |
| 61 | + azapi = { |
| 62 | + source = "azure/azapi" |
| 63 | + version = "~>1.5" |
| 64 | + } |
| 65 | + azurerm = { |
| 66 | + source = "hashicorp/azurerm" |
| 67 | + version = "~>3.0" |
| 68 | + } |
| 69 | + random = { |
| 70 | + source = "hashicorp/random" |
| 71 | + version = "~>3.0" |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +provider "azurerm" { |
| 77 | + features {} |
| 78 | +} |
| 79 | +EOF |
| 80 | +``` |
| 81 | + |
| 82 | +1. Create a file named ssh.tf and insert the following code: |
| 83 | + |
| 84 | +```bash |
| 85 | +cat <<'EOF' > ssh.tf |
| 86 | +resource "random_pet" "ssh_key_name" { |
| 87 | + prefix = "ssh" |
| 88 | + separator = "" |
| 89 | +} |
| 90 | +
|
| 91 | +resource "azapi_resource_action" "ssh_public_key_gen" { |
| 92 | + type = "Microsoft.Compute/sshPublicKeys@2022-11-01" |
| 93 | + resource_id = azapi_resource.ssh_public_key.id |
| 94 | + action = "generateKeyPair" |
| 95 | + method = "POST" |
| 96 | +
|
| 97 | + response_export_values = ["publicKey", "privateKey"] |
| 98 | +} |
| 99 | +
|
| 100 | +resource "azapi_resource" "ssh_public_key" { |
| 101 | + type = "Microsoft.Compute/sshPublicKeys@2022-11-01" |
| 102 | + name = random_pet.ssh_key_name.id |
| 103 | + location = azurerm_resource_group.rg.location |
| 104 | + parent_id = azurerm_resource_group.rg.id |
| 105 | +} |
| 106 | +
|
| 107 | +output "key_data" { |
| 108 | + value = azapi_resource_action.ssh_public_key_gen.output.publicKey |
| 109 | +} |
| 110 | +EOF |
| 111 | +``` |
| 112 | + |
| 113 | +1. Create a file named main.tf and insert the following code: |
| 114 | + |
| 115 | +```bash |
| 116 | +cat <<'EOF' > main.tf |
| 117 | +resource "random_pet" "rg_name" { |
| 118 | + prefix = var.resource_group_name_prefix |
| 119 | +} |
| 120 | +
|
| 121 | +resource "azurerm_resource_group" "rg" { |
| 122 | + location = var.resource_group_location |
| 123 | + name = random_pet.rg_name.id |
| 124 | +} |
| 125 | +
|
| 126 | +# Create virtual network |
| 127 | +resource "azurerm_virtual_network" "my_terraform_network" { |
| 128 | + name = "myVnet" |
| 129 | + address_space = ["10.0.0.0/16"] |
| 130 | + location = azurerm_resource_group.rg.location |
| 131 | + resource_group_name = azurerm_resource_group.rg.name |
| 132 | +} |
| 133 | +
|
| 134 | +# Create subnet |
| 135 | +resource "azurerm_subnet" "my_terraform_subnet" { |
| 136 | + name = "mySubnet" |
| 137 | + resource_group_name = azurerm_resource_group.rg.name |
| 138 | + virtual_network_name = azurerm_virtual_network.my_terraform_network.name |
| 139 | + address_prefixes = ["10.0.1.0/24"] |
| 140 | +} |
| 141 | +
|
| 142 | +# Create public IPs |
| 143 | +resource "azurerm_public_ip" "my_terraform_public_ip" { |
| 144 | + name = "myPublicIP" |
| 145 | + location = azurerm_resource_group.rg.location |
| 146 | + resource_group_name = azurerm_resource_group.rg.name |
| 147 | + allocation_method = "Dynamic" |
| 148 | +} |
| 149 | +
|
| 150 | +# Create Network Security Group and rule |
| 151 | +resource "azurerm_network_security_group" "my_terraform_nsg" { |
| 152 | + name = "myNetworkSecurityGroup" |
| 153 | + location = azurerm_resource_group.rg.location |
| 154 | + resource_group_name = azurerm_resource_group.rg.name |
| 155 | +
|
| 156 | + security_rule { |
| 157 | + name = "SSH" |
| 158 | + priority = 1001 |
| 159 | + direction = "Inbound" |
| 160 | + access = "Allow" |
| 161 | + protocol = "Tcp" |
| 162 | + source_port_range = "*" |
| 163 | + destination_port_range = "22" |
| 164 | + source_address_prefix = "*" |
| 165 | + destination_address_prefix = "*" |
| 166 | + } |
| 167 | +} |
| 168 | +
|
| 169 | +# Create network interface |
| 170 | +resource "azurerm_network_interface" "my_terraform_nic" { |
| 171 | + name = "myNIC" |
| 172 | + location = azurerm_resource_group.rg.location |
| 173 | + resource_group_name = azurerm_resource_group.rg.name |
| 174 | +
|
| 175 | + ip_configuration { |
| 176 | + name = "my_nic_configuration" |
| 177 | + subnet_id = azurerm_subnet.my_terraform_subnet.id |
| 178 | + private_ip_address_allocation = "Dynamic" |
| 179 | + public_ip_address_id = azurerm_public_ip.my_terraform_public_ip.id |
| 180 | + } |
| 181 | +} |
| 182 | +
|
| 183 | +# Connect the security group to the network interface |
| 184 | +resource "azurerm_network_interface_security_group_association" "example" { |
| 185 | + network_interface_id = azurerm_network_interface.my_terraform_nic.id |
| 186 | + network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id |
| 187 | +} |
| 188 | +
|
| 189 | +# Generate random text for a unique storage account name |
| 190 | +resource "random_id" "random_id" { |
| 191 | + keepers = { |
| 192 | + # Generate a new ID only when a new resource group is defined |
| 193 | + resource_group = azurerm_resource_group.rg.name |
| 194 | + } |
| 195 | +
|
| 196 | + byte_length = 8 |
| 197 | +} |
| 198 | +
|
| 199 | +# Create storage account for boot diagnostics |
| 200 | +resource "azurerm_storage_account" "my_storage_account" { |
| 201 | + name = "diag${random_id.random_id.hex}" |
| 202 | + location = azurerm_resource_group.rg.location |
| 203 | + resource_group_name = azurerm_resource_group.rg.name |
| 204 | + account_tier = "Standard" |
| 205 | + account_replication_type = "LRS" |
| 206 | +} |
| 207 | +
|
| 208 | +# Create virtual machine |
| 209 | +resource "azurerm_linux_virtual_machine" "my_terraform_vm" { |
| 210 | + name = "myVM" |
| 211 | + location = azurerm_resource_group.rg.location |
| 212 | + resource_group_name = azurerm_resource_group.rg.name |
| 213 | + network_interface_ids = [azurerm_network_interface.my_terraform_nic.id] |
| 214 | + size = "Standard_DS1_v2" |
| 215 | +
|
| 216 | + os_disk { |
| 217 | + name = "myOsDisk" |
| 218 | + caching = "ReadWrite" |
| 219 | + storage_account_type = "Premium_LRS" |
| 220 | + } |
| 221 | +
|
| 222 | + source_image_reference { |
| 223 | + publisher = "Canonical" |
| 224 | + offer = "0001-com-ubuntu-server-jammy" |
| 225 | + sku = "22_04-lts-gen2" |
| 226 | + version = "latest" |
| 227 | + } |
| 228 | +
|
| 229 | + computer_name = "hostname" |
| 230 | + admin_username = var.username |
| 231 | +
|
| 232 | + admin_ssh_key { |
| 233 | + username = var.username |
| 234 | + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey |
| 235 | + } |
| 236 | +
|
| 237 | + boot_diagnostics { |
| 238 | + storage_account_uri = azurerm_storage_account.my_storage_account.primary_blob_endpoint |
| 239 | + } |
| 240 | +} |
| 241 | +EOF |
| 242 | +``` |
| 243 | + |
| 244 | +1. Create a file named variables.tf and insert the following code: |
| 245 | + |
| 246 | +```bash |
| 247 | +cat <<'EOF' > variables.tf |
| 248 | +variable "resource_group_location" { |
| 249 | + type = string |
| 250 | + default = "eastus" |
| 251 | + description = "Location of the resource group." |
| 252 | +} |
| 253 | +
|
| 254 | +variable "resource_group_name_prefix" { |
| 255 | + type = string |
| 256 | + default = "rg" |
| 257 | + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." |
| 258 | +} |
| 259 | +
|
| 260 | +variable "username" { |
| 261 | + type = string |
| 262 | + description = "The username for the local account that will be created on the new VM." |
| 263 | + default = "azureadmin" |
| 264 | +} |
| 265 | +EOF |
| 266 | +``` |
| 267 | + |
| 268 | +1. Create a file named outputs.tf and insert the following code: |
| 269 | + |
| 270 | +```bash |
| 271 | +cat <<'EOF' > outputs.tf |
| 272 | +output "resource_group_name" { |
| 273 | + value = azurerm_resource_group.rg.name |
| 274 | +} |
| 275 | +
|
| 276 | +output "public_ip_address" { |
| 277 | + value = azurerm_linux_virtual_machine.my_terraform_vm.public_ip_address |
| 278 | +} |
| 279 | +EOF |
| 280 | +``` |
| 281 | + |
| 282 | +## Initialize Terraform |
| 283 | + |
| 284 | +In this section, Terraform is initialized; this command downloads the Azure provider required to manage your Azure resources. Before running the command, ensure you are in the directory where you created the Terraform files. You can set any necessary environment variables here. |
| 285 | + |
| 286 | +```bash |
| 287 | +export TERRAFORM_DIR=$(pwd) |
| 288 | +terraform init -upgrade |
| 289 | +``` |
| 290 | + |
| 291 | +Key points: |
| 292 | + |
| 293 | +- The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints. |
| 294 | + |
| 295 | +## Create a Terraform execution plan |
| 296 | + |
| 297 | +This step creates an execution plan but does not execute it. It shows what actions are necessary to create the configuration specified in your files. |
| 298 | + |
| 299 | +```bash |
| 300 | +terraform plan -out main.tfplan |
| 301 | +``` |
| 302 | + |
| 303 | +Key points: |
| 304 | + |
| 305 | +- The terraform plan command creates an execution plan, allowing you to verify whether it matches your expectations before applying any changes. |
| 306 | +- The optional -out parameter writes the plan to a file so that the exact plan can be applied later. |
| 307 | + |
| 308 | +## Apply a Terraform execution plan |
| 309 | + |
| 310 | +Apply the previously created execution plan to deploy the infrastructure to your cloud. |
| 311 | + |
| 312 | +```bash |
| 313 | +terraform apply main.tfplan |
| 314 | +``` |
| 315 | + |
| 316 | +Key points: |
| 317 | + |
| 318 | +- This command applies the plan created with terraform plan -out main.tfplan. |
| 319 | +- If you used a different filename for the -out parameter, use that same filename with terraform apply. |
| 320 | +- If the -out parameter wasn’t used, run terraform apply without any parameters. |
| 321 | + |
| 322 | +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). |
| 323 | + |
| 324 | +## Verify the results |
| 325 | + |
| 326 | +#### [Azure CLI](#tab/azure-cli) |
| 327 | + |
| 328 | +1. Get the Azure resource group name. |
| 329 | + |
| 330 | +```bash |
| 331 | +export RESOURCE_GROUP_NAME=$(terraform output -raw resource_group_name) |
| 332 | +``` |
| 333 | + |
| 334 | +1. Run az vm list with a JMESPath query to display the names of the virtual machines created in the resource group. |
| 335 | + |
| 336 | +```azurecli |
| 337 | +az vm list \ |
| 338 | + --resource-group $RESOURCE_GROUP_NAME \ |
| 339 | + --query "[].{\"VM Name\":name}" -o table |
| 340 | +``` |
| 341 | + |
| 342 | +Results: |
| 343 | + |
| 344 | +<!-- expected_similarity=0.3 --> |
| 345 | + |
| 346 | +```console |
| 347 | +VM Name |
| 348 | +----------- |
| 349 | +myVM |
| 350 | +``` |
| 351 | + |
| 352 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 353 | + |
| 354 | +1. Get the Azure resource group name. |
| 355 | + |
| 356 | +```console |
| 357 | +$resource_group_name=$(terraform output -raw resource_group_name) |
| 358 | +``` |
| 359 | + |
| 360 | +1. Run Get-AzVm to display the names of all the virtual machines in the resource group. |
| 361 | + |
| 362 | +```azurepowershell |
| 363 | +Get-AzVm -ResourceGroupName $resource_group_name |
| 364 | +``` |
| 365 | + |
| 366 | +## Troubleshoot Terraform on Azure |
| 367 | + |
| 368 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 369 | + |
| 370 | +## Next steps |
| 371 | + |
| 372 | +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. |
| 373 | + |
| 374 | +> [!div class="nextstepaction"] |
| 375 | +> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md) |
0 commit comments