# Create a Virtual NET in Azure

In 
Published 2022-12-03

This tutorial explains how we can create a Virtual NET in Azure using Terraform.

An Azure Virtual Network (VNet) is a representation of your own network in the cloud. It is a logical isolation of the Azure cloud dedicated to your subscription. You can use VNets to provision and manage virtual private networks (VPNs) in Azure

Here are the steps to follow in order to deploy a VNet in Azure using Terraform:

1) Create a folder where we keep our project

To be well organized we need to create a folder where we keep our project. In my case this is D:\terraform\azure\vnet.

2) Create the configuration file

Now is the time to tell Terraform what to do.

We create main.tf file with the following content:

#Set the Azure Provider source and version being used
#----------------------------------------------------
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.1"
    }
  }
}

# Configure the Microsoft Azure Provider
#---------------------------------------
provider "azurerm" {
  features {}
}

###############################
#   Create a resource group   #
##############################################
resource "azurerm_resource_group" "my-new-resource-group" {
  name     = "my-new-resource-group"
  location = "West Europe"
}

###################
#   Virtual NET   #
################################################
resource "azurerm_virtual_network" "my-vnet" {
  name                = "my-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.my-new-resource-group.location
  resource_group_name = azurerm_resource_group.my-new-resource-group.name
}

So, we configure the provider, the resource group and the Virtual NET.

3) Initialize the project

Run the terraform init command to initialize a working directory that contains a Terraform configuration. This command is run under the working/project directory.

terraform init

You will see the following in your console:

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "3.0.1"...
- Installing hashicorp/azurerm v3.0.1...
- Installed hashicorp/azurerm v3.0.1 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

4) Login to the Azure account we work with using Azure CLI

az login

You will be prompted to login from the browser. Once the username/password are verified, you are connected to Azure in the console.

Run the following command and change the subscription name if you are using multiple subscriptions:

az account set --subscription "MyAzureSubscription1"

5) Create the Virtual NET in Azure

Now it is the time to create a Resource Group and a Virtual NET in Azure and this is done very simple using the following command:

terraform apply

You can see the result on the screen:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.my-new-resource-group will be created
  + resource "azurerm_resource_group" "my-new-resource-group" {
      + id       = (known after apply)
      + location = "westeurope"
      + name     = "my-new-resource-group"
    }

  # azurerm_virtual_network.my-vnet will be created
  + resource "azurerm_virtual_network" "my-vnet" {
      + address_space       = [
          + "10.0.0.0/16",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "westeurope"
      + name                = "my-vnet"
      + resource_group_name = "my-new-resource-group"
      + subnet              = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.my-new-resource-group: Creating...
azurerm_resource_group.my-new-resource-group: Creation complete after 1s [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group]
azurerm_virtual_network.my-vnet: Creating...
azurerm_virtual_network.my-vnet: Creation complete after 5s [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group/providers/Microsoft.Network/virtualNetworks/my-vnet]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

If you run terraform apply again you will see the following :

azurerm_resource_group.my-new-resource-group: Refreshing state... [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group]
azurerm_virtual_network.my-vnet: Refreshing state... [id=/subscriptions/69045ea7-6500-45f7-adc6-b4c86ce7233b/resourceGroups/my-new-resource-group/providers/Microsoft.Network/virtualNetworks/my-vnet]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are
needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

That means terraform apply command is idempotent.