Deploy Azure VNETs and Subnets with Terraform

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log into the Azure Portal and set up the Command Line Interface (CLI) for use.
  1. Open the CLI.
  2. Select Bash at the prompt.
  3. Click Show Advanced Settings.
  4. Set the cloudshell region to the same location as the resource group.
  5. Select the existing Resource Group and select Use Existing for the Storage Account.
  6. In the File share section, choose the Create new radio button and enter console.
  7. Click the Attach Storage button.
  8. Once the command prompt is initialized, proceed to Task 2.
Task 2 – Deploy a virtual network and two subnets.

To complete this exercise, please make sure you’ve completed Task 1 before completing the following:

  1. Use the code block found in the Additional Information and Resources section to create a lab.tf file and upload it to the CLI.
  2. In the (resource "azurerm_storage_account" "lab") declaration, you’ll need to edit the resource_group_name value with the name generated by the lab. Additionally, you’ll need to enter a unique name for the storage account that will be used for the file share. The "provider" statement has been added to the code, so you won’t need to create a main.tf file to deploy the storage account.
  3. Once the file has been uploaded, run terraform init.
  4. Run terraform plan and review the output to confirm that Terraform will create the desired resource. Green plus signs will indicate the resources that need to be added.
  5. Run terraform apply, answering yes to the prompt to continue.
  6. Once Terraform completes the deployment, check the Azure Portal to confirm.

You’re done! Go ahead and shut down the lab.

Additional Resources

In this lab, we will create a /16 Virtual Network with a /24 Subnet. You'll need to use the pre-generated resource group name. Consider tagging this deployment with an appropriate department or use-value.

Code for lab.tf file follows

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "= 2.99"
    }
  }
}

provider "azurerm" {
  features {}

  skip_provider_registration = true
}

# Create virtual network
resource "azurerm_virtual_network" "TFNet" {
    name                = "LabVnet"
    address_space       = ["10.0.0.0/16"]
    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 = azurerm_virtual_network.TFNet.name
    address_prefixes       = ["10.0.1.0/24"]
}
resource "azurerm_subnet" "tfsubnet2" {
    name                 = "LabSubnet2"
    resource_group_name = "Enter Resource Group Name"
    virtual_network_name = azurerm_virtual_network.TFNet.name
    address_prefixes       = ["10.0.2.0/24"]
}

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?