|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create an Azure Windows virtual machine using Terraform' |
| 3 | +description: In this quickstart, you create an Azure Windows virtual machine (VM), virtual network, subnet, public IP, network security group, network interface, storage account, recovery services vault, backup policy, and a protected VM for backup. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 11/27/2024 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +ms.service: windows |
| 8 | +author: v-abhmallick |
| 9 | +ms.author: AbhishekMallick-MS |
| 10 | +#customer intent: As a Terraform user, I want to see how to create and configure an Azure virtual network, subnet, public IP, network security group, network interface, storage account, virtual machine, recovery services vault, and backup policy. |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Create an Azure windows virtual machine using Terraform |
| 16 | + |
| 17 | +In this quickstart, you create an Azure Windows virtual machine (VM) and associated resources using Terraform. An Azure Windows VM is a scalable computing resource that Azure provides. It's an on-demand, virtualized Windows server in the Azure cloud. You can use it to deploy, test, and run applications, among other things. In addition to the VM, this code also creates a virtual network, subnet, public IP, network security group, network interface, storage account, recovery services vault, and backup policy. |
| 18 | + |
| 19 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Specify the required version of Terraform and the required providers. |
| 23 | +> * Configure the Azure provider with recovery service features. |
| 24 | +> * Declare variables for the resource group location, resource group name prefix, and an enabled soft delete status. |
| 25 | +> * Generate a random pet name for the resource group. |
| 26 | +> * Create an Azure resource group with the generated pet name. |
| 27 | +> * Generate a random string of 12 lowercase characters. |
| 28 | +> * Create a virtual network with a unique name and a specified address space. |
| 29 | +> * Create a subnet within the virtual network with a unique name and a specified address prefix. |
| 30 | +> * Create a public IP address with a unique name. |
| 31 | +> * Create a network security group with two security rules for remote desk protocol and web traffic. |
| 32 | +> * Create a network interface with a unique name and attach it to the subnet and public IP address. |
| 33 | +> * Associate the network security group with the network interface. |
| 34 | +> * Create a storage account for boot diagnostics. |
| 35 | +> * Create a Windows VM with a unique name, specified size, and attached to the network interface. |
| 36 | +> * Generate a random ID for a unique storage account name. |
| 37 | +> * Generate a random password for the VM. |
| 38 | +> * Create a recovery services vault with a unique name. |
| 39 | +> * Create a backup policy for the VM with daily frequency and a retention period of 7 days. |
| 40 | +> * Protect the VM with the created backup policy. |
| 41 | +> * Output the names of the resource group, recovery services vault, backup policy, and VM. |
| 42 | +> * Output the public IP address and admin password of the VM. |
| 43 | +
|
| 44 | +## Prerequisites |
| 45 | + |
| 46 | +- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 47 | + |
| 48 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 49 | + |
| 50 | +## Implement the Terraform code |
| 51 | + |
| 52 | +> [!NOTE] |
| 53 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-backup-vm). 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-backup-vm/TestRecord.md). |
| 54 | +> |
| 55 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform). |
| 56 | +
|
| 57 | +1. Create a directory in which to test and run the sample Terraform code, and make it the current directory. |
| 58 | + |
| 59 | +1. Create a file named `main.tf`, and insert the following code: |
| 60 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-backup-vm/main.tf"::: |
| 61 | + |
| 62 | +1. Create a file named `outputs.tf`, and insert the following code: |
| 63 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-backup-vm/outputs.tf"::: |
| 64 | + |
| 65 | +1. Create a file named `providers.tf`, and insert the following code: |
| 66 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-backup-vm/providers.tf"::: |
| 67 | + |
| 68 | +1. Create a file named `variables.tf`, and insert the following code: |
| 69 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-backup-vm/variables.tf"::: |
| 70 | + |
| 71 | +## Initialize Terraform |
| 72 | + |
| 73 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 74 | + |
| 75 | +## Create a Terraform execution plan |
| 76 | + |
| 77 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 78 | + |
| 79 | +## Apply a Terraform execution plan |
| 80 | + |
| 81 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 82 | + |
| 83 | +## Verify the results |
| 84 | + |
| 85 | +### [Azure CLI](#tab/azure-cli) |
| 86 | + |
| 87 | +1. Get the Azure resource group name. |
| 88 | + |
| 89 | +```Terraform |
| 90 | +resource_group_name = $(terraform outout -raw azurerm_resource_group_name) |
| 91 | +``` |
| 92 | + |
| 93 | +1. Get the Recovery Services vault name. |
| 94 | + |
| 95 | +```Terraform |
| 96 | +recovery_services_vault_name = $(terraform output -raw azurerm_recovery_services_vault_name) |
| 97 | +``` |
| 98 | + |
| 99 | +1. Get the Windows VM name. |
| 100 | + |
| 101 | +```Terraform |
| 102 | +windows_virtual_machine_name = $(terraform output -raw azurerm_windows_virtual_machine_name) |
| 103 | +``` |
| 104 | + |
| 105 | +1. Run [az backup protection backup-now]( /cli/azure/backup/protection #az-backup-protection-backup-now) to start a backup job. |
| 106 | + |
| 107 | +```Terraform |
| 108 | +az backup protection backup-now --resource-group $resource_group_name \ |
| 109 | + --vault-name $recovery_services_vault_name \ |
| 110 | + --container-name $windows_virtual_machine_name \ |
| 111 | + --item-name $windows_virtual_machine_name \ |
| 112 | + --backup-management-type AzureIaaSVM |
| 113 | +``` |
| 114 | + |
| 115 | +1. Run [az backup job list]( /cli/azure/backup/job #az-backup-job-list) to monitor the backup job. |
| 116 | + |
| 117 | +```Terraform |
| 118 | +az backup job list --resource-group $resource_group_name \ |
| 119 | + --vault-name $recovery_services_vault_name \ |
| 120 | + --output table |
| 121 | +``` |
| 122 | + |
| 123 | +The output is similar to the following example, which shows the backup job is *InProgress*: |
| 124 | + |
| 125 | +```output |
| 126 | +Name Operation Status Item Name Start Time UTC Duration |
| 127 | +-------- --------------- ---------- ----------- ------------------- -------------- |
| 128 | +a0a8e5e6 Backup InProgress myvm 2017-09-19T03:09:21 0:00:48.718366 |
| 129 | +fe5d0414 ConfigureBackup Completed myvm 2017-09-19T03:03:57 0:00:31.191807 |
| 130 | +``` |
| 131 | + |
| 132 | +When the *Status* of the backup job reports *Completed*, your VM is protected with Recovery Services and has a full recovery point stored. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Clean up resources |
| 137 | + |
| 138 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 139 | + |
| 140 | +## Troubleshoot Terraform on Azure |
| 141 | + |
| 142 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot). |
| 143 | + |
| 144 | +## Next steps |
| 145 | + |
| 146 | +> [!div class="nextstepaction"] |
| 147 | +> [See more articles about Azure windows VMs](/search/?terms=Azure%20windows%20virtual%20machine%20and%20terraform). |
0 commit comments