Azure Terraform: Part 4 – Azure Networking

Reading Time: 3 minutes

In the previous chapter in our Terraform journey we defined and created a magical vessel to hold our digital landscape, an Azure Resource Group. As we continue on this magical path we now learn how to deploy fundamental network components an Azure VNET and Subnet.

Azure Virtual Network (VNET)

As we continue our journey, VNET’s emerge as the architects of connectivity across your Azure digital landscape. A single VNET is the minimal requirement before resources that require network connectivity can be deployed.

In this chapter we will deploy a single VNET and Subnet using Terraform.

To learn more about Azure VNET click the following link.

VNET Configuration

The Terraform documentation for creating a Azure Virtual Network is located here, provides example code and details required and optional arguments.

We will be using the following code to create our VNET,

resource "azurerm_virtual_network" "ftvnet" {
  name                = "ftvnet01"
  address_space       = ["10.10.0.0/16"]
  location            = "uksouth"
  resource_group_name = azurerm_resource_group.ftrg001.name

  tags = {
    environment = "dev"
  }

}

Subnet Configuration

The Terraform documentation for creating a Azure subnet is located here, provides example code and details required and optional arguments.

We will be using the following code to create our subnet,

resource "azurerm_subnet" "ftsubnet" {
  name                 = "ftsubnet01"
  resource_group_name  = azurerm_resource_group.ftrg001.name
  virtual_network_name = azurerm_virtual_network.ftvnet.name
  address_prefixes     = ["10.10.0.0/24"]
}

Copy both code examples and add them to your main.tf file within Visual Studio Code.

As per the previous blog post in this series, having defined our configuration we will validate, plan and deploy our new resources.

Start by running terraform validate, followed by terraform plan.

Terraform Plan

The out from terraform plan confirms the new resources that will be created. You may have noticed it is only creating the new resources you defined into the existing resource group we created during the previous blog post.

This is known as Idempotence.

Idempotence

When running terraform plan, terraform will check if there are resources defined within the script that have already been created. If nothing has changed regarding those resources no changes will be made. Terraform will only apply the changes to your digital landscape that do not match your live environment.

In our example we have already created an Azure resource group which we have not changed. When running terraform apply terraform will only create the new VNET and Subnet that you have recently defined.

Terraform Apply

To apply the configuration run terraform apply and confirm (type yes) to apply the changes,

Confirm VNET Creation

To confirm your VNET was created successfully, run the following command,

  • az network vnet list –output table

Confirm Subnet Creation

To confirm your subnet created successfully run the following command,

  • az network vnet subnet list –resource-group “FT23-RG-001” –vnet-name ftvnet01 –output table

In the next chapter of our Festive Tech Terraform odyssey we will deploy Azure resources that connect to the virtual network infrastructure you have defined and created in this blog post. Until the next time, happy orchestrating.