Hi there
Continuing our journey on how to automate Azure Resources deployment using Terraform today we’re demonstrating how to create (or add to our current plan) a VNET with subnets.
Have with you your previous main.tf file – download from here.
Now open your Visual Studio Code and open your Project Folder

Load your main.tf and lets add the following code for the VNET creation
- Creates a VNET named: example-network
- VNET IP address block: 10.0.0.0/16
- Creates a Subnet 10.0.1.0/24 named: subnet1
- Azure Resource Group Name: terraform Resource Group (created previously)
#Azure Resource Group
resource "azurerm_resource_group" "rg" {
name = "terraform"
location = "canadacentral"
}
# Create a virtual network within the resource group
resource "azurerm_virtual_network" "vnet" {
name = "example-network"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.10.0.0/16"]
tags = {
CostCenter = "IT"
Environment = "PROD"
}
}
# Create Subnets
resource "azurerm_subnet" "subnet" {
name = "subnet1"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefix = "10.10.1.0/24"
}
You should see the following

Open a PowerShell on your Project Folder and run the following
az login (to connect to your azure subscription)

enter your credentials on browser and wait for the next screen

terraform init

terraform plan (OR terraform plan -out myplan3 to save your plan and reuse it if needed – keep your code under control)

terraform apply

Check you result at Azure Portal
Before

After




Thanks,