Skip to main content

Deploy Azure VNETs and Subnets with Terraform

Hands-On Lab


Photo of Russ Croft

Russ Croft

Azure Training Architect II





In this lab, we will deploy a virtual network and two subnets.

Networking is core to all datacenters, virtual or physical, and Azure is no exception. By staging network deployments with Terraform for things such as Dev/Test or a Lab environment where you, the Admin, have preset network sizes and IP ranges, you're able to ensure that valuable IP space is used in the most appropriate manner.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploy Azure VNETs and Subnets with Terraform

In this lab, we will deploy a virtual network and two subnets. To do so, we will be setting up the Azure CLI to be able to create the virtual network and subnets using the provided code.

Before We Begin

Before we get started, we need to log into the Azure portal using the provided credentials.

Set Up the Azure CLI

In the Azure portal, select the Command Line button at the top of the screen. Open the CLI. Here, select Bash when prompted. We then want to choose Show Advanced Settings. Leave both the Resource Group and Storage Account as the provided defaults. In the File share section, enter a name for the account (for this example, we are using Console) and click the Attach Storage button. Once the command prompt completes, we can continue.

Deploy a Virtual Network and Two Subnets

The first thing we need to do now that we have a storage account is to create a file using the code we've been provided. In this code, replace the "unique name" with a unique name (we'll use"LabVnet" for our example). For each instance of "Enter Resource Group Name", use the name of the resource group provided by the lab, which should be preceded by three numbers on the Azure portal's main page. For our video example, this title starts with 155. We will be naming this

There are two main sections to this code. The first section sets up our virtual network, while the second sets up both of our subnets.

provider "azurerm" {
    version = 1.38

# Create virtual network
resource "azurerm_virtual_network" "TFNet" {
    name                = "unique name"
    address_space       = [""]
    location            = "eastus"
    resource_group_name = "Enter Resource Group Name"

    tags = {
        environment = "Terraform Networking"

# Create subnet
resource "azurerm_subnet" "tfsubnet" {
    name                 = "LabSubnet"
    resource_group_name = "Enter Resource Group Name"
    virtual_network_name =
    address_prefix       = ""
resource "azurerm_subnet" "tfsubnet2" {
    name                 = "LabSubnet2"
    resource_group_name = "Enter Resource Group Name"
    virtual_network_name =
    address_prefix       = ""

Once we've completed the file and saved it, go to the Upload/Download button above the Azure CLI and upload the file.

With that uploaded, we need to run the command terraform init. Next, run terraform plan and review the output to confirm that our changes are reflected. The green plus signs indicate the resources that will be added.

Lastly, run terraform apply to confirm these changes, answering yes to the prompt to continue.

Once the command finishes, we need to make sure that our virtual network is working. To do so, on the Azure portal, select the Refresh button. The network that we named appears. Select it, then select Subnets from the sidebar. The two subnets we created appear here.


After completing this lab, we are now able to launch a virtual network from the Azure CLI, and have seen how the code is used can create subnets. Congratulations on finishing the lab!