|
| 1 | +--- |
| 2 | +title: 'Quickstart: Use Terraform to configure a virtual network in Azure' |
| 3 | +description: In this quickstart, you create a virtual network, subnets, private DNS zones, network interfaces, Windows virtual machines, a private DNS A record, network security groups, and a network security rule in Azure. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 12/09/2024 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +ms.service: virtual-network |
| 8 | +author: greg-lindsay |
| 9 | +ms.author: greglin |
| 10 | +#customer intent: As a Terraform user, I want to see how to create a virtual network with a subnet, a private DNS zone, and Windows virtual machines in Azure. |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Use Terraform to configure a virtual network in Azure |
| 16 | + |
| 17 | +In this quickstart, you use Terraform to create a virtual network, subnets, private DNS zones, network interfaces, Windows virtual machines, a private DNS A record, network security groups, and a network security rule in Azure. |
| 18 | +An Azure virtual network is a fundamental component of the Azure networking model, providing isolation and protection for your virtual machines. It's used to control and manage traffic between resources such as virtual machines within a network. |
| 19 | +In addition to the Azure virtual network, this code also creates: |
| 20 | + |
| 21 | +* Subnets within the network. |
| 22 | +* Private DNS zones for name resolution. |
| 23 | +* Network interfaces for the virtual machines. |
| 24 | +* Network security groups to control inbound and outbound traffic. |
| 25 | +* Windows virtual machines with random passwords for administration. |
| 26 | + |
| 27 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 28 | + |
| 29 | +> [!div class="checklist"] |
| 30 | +> * Specify the required version and providers for Terraform. |
| 31 | +> * Define variables for the resource group location, name prefix, address space, address prefixes, private DNS zone name, and admin username. |
| 32 | +> * Generate a random pet name for the resource group. |
| 33 | +> * Create an Azure resource group with a unique name. |
| 34 | +> * Generate a random string for unique naming. |
| 35 | +> * Create a virtual network with a unique name. |
| 36 | +> * Create a subnet within the virtual network. |
| 37 | +> * Create a private DNS zone. |
| 38 | +> * Link the private DNS zone to the virtual network. |
| 39 | +> * Generate random passwords for the virtual machines. |
| 40 | +> * Create two network interfaces. |
| 41 | +> * Create two Windows virtual machines and attach the network interfaces. |
| 42 | +> * Create a private DNS A record. |
| 43 | +> * Create a network security group. |
| 44 | +> * Create a network security rule to allow ICMP traffic. |
| 45 | +> * Output the names and admin credentials of the virtual machines. |
| 46 | +
|
| 47 | +## Prerequisites |
| 48 | + |
| 49 | +- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 50 | + |
| 51 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure). |
| 52 | + |
| 53 | +## Implement the Terraform code |
| 54 | + |
| 55 | +> [!NOTE] |
| 56 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-dns-private-zone). 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-dns-private-zone/TestRecord.md). |
| 57 | +> |
| 58 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform). |
| 59 | +
|
| 60 | +1. Create a directory in which to test and run the sample Terraform code, and make it the current directory. |
| 61 | + |
| 62 | +1. Create a file named `main.tf`, and insert the following code. |
| 63 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-dns-private-zone/main.tf"::: |
| 64 | + |
| 65 | +1. Create a file named `outputs.tf`, and insert the following code. |
| 66 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-dns-private-zone/outputs.tf"::: |
| 67 | + |
| 68 | +1. Create a file named `providers.tf`, and insert the following code. |
| 69 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-dns-private-zone/providers.tf"::: |
| 70 | + |
| 71 | +1. Create a file named `variables.tf`, and insert the following code. |
| 72 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-dns-private-zone/variables.tf"::: |
| 73 | + |
| 74 | +## Initialize Terraform |
| 75 | + |
| 76 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 77 | + |
| 78 | +## Create a Terraform execution plan |
| 79 | + |
| 80 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 81 | + |
| 82 | +## Apply a Terraform execution plan |
| 83 | + |
| 84 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 85 | + |
| 86 | +## Verify the results |
| 87 | + |
| 88 | +### [Azure CLI](#tab/azure-cli) |
| 89 | + |
| 90 | +1. Run `az network private-dns zone list` to view all DNS zones and find yours. |
| 91 | + |
| 92 | + ```azurecli |
| 93 | + az network private-dns zone list --output table |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Run `az network private-dns zone show` to view the resource group associate with your DNS zone. |
| 97 | + |
| 98 | + ```azurecli |
| 99 | + az network private-dns zone show --name <dnsZoneName> --resource-group <resourceGroupName> |
| 100 | + ``` |
| 101 | + |
| 102 | + Replace `<dnsZoneName>` with the name of your DNS zone and `<resourceGroupName>` with the name of your resource group. |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Clean up resources |
| 107 | + |
| 108 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 109 | + |
| 110 | +## Troubleshoot Terraform on Azure |
| 111 | + |
| 112 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot). |
| 113 | + |
| 114 | +## Next steps |
| 115 | + |
| 116 | +> [!div class="nextstepaction"] |
| 117 | +> [See more articles about Azure virtual network](/search/?terms=Azure%20virtual%20network%20and%20terraform). |
0 commit comments