0
0
Azurecloud~5 mins

Subnets within a VNet in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a virtual network in Azure, you divide it into smaller parts called subnets. Subnets help organize and secure your network by grouping resources together and controlling traffic between them.
When you want to separate your web servers from your database servers within the same network.
When you need to apply different security rules to different groups of resources.
When you want to control traffic flow between parts of your application.
When you want to allocate IP address ranges efficiently inside your virtual network.
When you want to connect your network to other networks or services securely.
Config File - main.tf
main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "eastus"
}

resource "azurerm_virtual_network" "example_vnet" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "subnet1" {
  name                 = "subnet-frontend"
  virtual_network_name = azurerm_virtual_network.example_vnet.name
  resource_group_name  = azurerm_resource_group.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_subnet" "subnet2" {
  name                 = "subnet-backend"
  virtual_network_name = azurerm_virtual_network.example_vnet.name
  resource_group_name  = azurerm_resource_group.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

This Terraform file creates an Azure resource group, a virtual network with a 10.0.0.0/16 address space, and two subnets inside it.

The azurerm_virtual_network resource defines the main network.

The azurerm_subnet resources define two smaller networks inside the main one, each with its own IP range.

Commands
This command sets up Terraform in the current folder, downloading the Azure provider so you can create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will create in Azure without making any changes yet. It helps you check your setup.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_resource_group.example will be created + resource "azurerm_resource_group" "example" { + location = "eastus" + name = "example-resources" } # azurerm_virtual_network.example_vnet will be created + resource "azurerm_virtual_network" "example_vnet" { + address_space = ["10.0.0.0/16"] + location = "eastus" + name = "example-vnet" + resource_group_name = "example-resources" } # azurerm_subnet.subnet1 will be created + resource "azurerm_subnet" "subnet1" { + address_prefixes = ["10.0.1.0/24"] + name = "subnet-frontend" + resource_group_name = "example-resources" + virtual_network_name = "example-vnet" } # azurerm_subnet.subnet2 will be created + resource "azurerm_subnet" "subnet2" { + address_prefixes = ["10.0.2.0/24"] + name = "subnet-backend" + resource_group_name = "example-resources" + virtual_network_name = "example-vnet" } Plan: 4 to add, 0 to change, 0 to destroy.
This command creates the resource group, virtual network, and subnets in Azure as defined in the configuration file.
Terminal
terraform apply -auto-approve
Expected OutputExpected
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources] azurerm_virtual_network.example_vnet: Creating... azurerm_virtual_network.example_vnet: Creation complete after 3s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Network/virtualNetworks/example-vnet] azurerm_subnet.subnet1: Creating... azurerm_subnet.subnet1: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Network/virtualNetworks/example-vnet/subnets/subnet-frontend] azurerm_subnet.subnet2: Creating... azurerm_subnet.subnet2: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Network/virtualNetworks/example-vnet/subnets/subnet-backend] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to apply changes immediately
This Azure CLI command lists all subnets inside the virtual network to verify they were created correctly.
Terminal
az network vnet subnet list --resource-group example-resources --vnet-name example-vnet --output table
Expected OutputExpected
Name AddressPrefix --------------- -------------- subnet-frontend 10.0.1.0/24 subnet-backend 10.0.2.0/24
--output table - Formats output as a readable table
Key Concept

If you remember nothing else from this pattern, remember: subnets divide a virtual network into smaller parts to organize and control your cloud resources.

Common Mistakes
Using overlapping IP address ranges for subnets inside the same virtual network
Overlapping IP ranges cause routing conflicts and prevent resources from communicating properly.
Assign unique, non-overlapping IP address ranges to each subnet within the virtual network.
Not specifying the resource group or virtual network name correctly in subnet definitions
Terraform or Azure CLI cannot find the parent network, causing deployment failures.
Always reference the correct resource group and virtual network names exactly as created.
Trying to create subnets before the virtual network exists
Subnets depend on the virtual network; creating them first causes errors.
Create the virtual network first, then create subnets inside it.
Summary
Create a virtual network with a large IP address range to hold your subnets.
Define subnets with smaller, unique IP ranges inside the virtual network.
Use Terraform commands to initialize, plan, and apply your network configuration.
Verify subnet creation using Azure CLI to list subnets in the virtual network.