|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create virtual network and subnets using Terraform' |
| 3 | +titleSuffix: Azure Virtual Network |
| 4 | +description: In this quickstart, you create an Azure Virtual Network and Subnets using Terraform. You use Azure CLI to verify the resources. |
| 5 | +ms.topic: quickstart |
| 6 | +ms.date: 1/19/2024 |
| 7 | +ms.custom: devx-track-terraform |
| 8 | +ms.service: virtual-network |
| 9 | +author: asudbring |
| 10 | +ms.author: allensu |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +# Customer intent: As a Network Administrator, I want to create a virtual network and subnets using Terraform. |
| 14 | +--- |
| 15 | + |
| 16 | +# Quickstart: Create an Azure Virtual Network and subnets using Terraform |
| 17 | + |
| 18 | +In this quickstart, you learn about a Terraform script that creates an Azure resource group and a virtual network with two subnets. The names of the resource group and the virtual network are generated using a random pet name with a prefix. The script also outputs the names of the created resources. |
| 19 | + |
| 20 | +The script uses the Azure Resource Manager (azurerm) and Random (random) providers. The azurerm provider is used to interact with Azure resources, while the random provider is used to generate random pet names for the resources. |
| 21 | + |
| 22 | +The script creates the following resources: |
| 23 | + |
| 24 | +- A resource group: A container that holds related resources for an Azure solution. |
| 25 | + |
| 26 | +- A virtual network: A fundamental building block for your private network in Azure. |
| 27 | + |
| 28 | +- Two subnets: Segments of a virtual network's IP address range where you can place groups of isolated resources. |
| 29 | + |
| 30 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 31 | + |
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 35 | + |
| 36 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 37 | + |
| 38 | +## Implement the Terraform code |
| 39 | + |
| 40 | +> [!NOTE] |
| 41 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-virtual-network-create-two-subnets). 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-virtual-network-create-two-subnets/TestRecord.md). |
| 42 | +> |
| 43 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 44 | +
|
| 45 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 46 | + |
| 47 | +1. Create a file named `main.tf` and insert the following code: |
| 48 | + |
| 49 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-virtual-network-create-two-subnets/main.tf"::: |
| 50 | + |
| 51 | +1. Create a file named `outputs.tf` and insert the following code: |
| 52 | + |
| 53 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-virtual-network-create-two-subnets/outputs.tf"::: |
| 54 | + |
| 55 | +1. Create a file named `providers.tf` and insert the following code: |
| 56 | + |
| 57 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-virtual-network-create-two-subnets/providers.tf"::: |
| 58 | + |
| 59 | +1. Create a file named `variables.tf` and insert the following code: |
| 60 | + |
| 61 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-virtual-network-create-two-subnets/variables.tf"::: |
| 62 | + |
| 63 | + |
| 64 | +## Initialize Terraform |
| 65 | + |
| 66 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 67 | + |
| 68 | +## Create a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 71 | + |
| 72 | +## Apply a Terraform execution plan |
| 73 | + |
| 74 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 75 | + |
| 76 | +## Verify the results |
| 77 | + |
| 78 | +#### [Azure CLI](#tab/azure-cli) |
| 79 | + |
| 80 | +1. Get the Azure resource group name. |
| 81 | + |
| 82 | + ```console |
| 83 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 84 | + ``` |
| 85 | + |
| 86 | +1. Get the virtual network name. |
| 87 | + |
| 88 | + ```console |
| 89 | + virtual_network_name=$(terraform output -raw virtual_network_name) |
| 90 | + ``` |
| 91 | + |
| 92 | +1. Use [`az network vnet show`](/cli/azure/network/vnet#az-network-vnet-show) to display the details of your newly created virtual network. |
| 93 | + |
| 94 | + ```azurecli |
| 95 | + az network vnet show \ |
| 96 | + --resource-group $resource_group_name \ |
| 97 | + --name $virtual_network_name |
| 98 | + ``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Clean up resources |
| 103 | + |
| 104 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 105 | + |
| 106 | +## Troubleshoot Terraform on Azure |
| 107 | + |
| 108 | + For more information about troubleshooting Terraform, see [Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot). |
| 109 | + |
| 110 | +## Next steps |
| 111 | + |
| 112 | +> [!div class="nextstepaction"] |
| 113 | +> [Learn more about using Terraform in Azure](/azure/terraform) |
0 commit comments