Setup Azure Virtual Network Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
  • For MacOs install the terraform using HomeBrew.

Step 2: Set Up Azure CLI

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.

  • For MacOs install the Azure CLI using below HomeBrew Command.
brew update && brew install azure-cli

Step 3: Configure Azure CLI

  • Open terminal and run below command.
az login
  • A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
  • Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
  • Add configuration for Virtual Network. For this article we will add only required arguments. You can specify other arguments if required.
resource "azurerm_virtual_network" "samplevnet" {
name = "samplevnet"
resource_group_name = "DeepsLab"
location = "eastus"
address_space = [ "10.0.0.0/16"]

subnet {
name = "subnet-A"
address_prefix = "10.0.1.0/24"
}
}
  • We have specified name for Virtual network. We have specified the location and resource group where virtual network should be created.
  • we have added a subnet with /24 prefix in the virtual network.
  • The complete code will look like below.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_virtual_network" "samplevnet" {
name = "samplevnet"
resource_group_name = "DeepsLab"
location = "eastus"
address_space = [ "10.0.0.0/16"]

subnet {
name = "subnet-A"
address_prefix = "10.0.1.0/24"
}
}

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

  • After successful output of terraform apply the changes using below command.
terrraform apply
  • After verifying type “yes” to confirm and apply.
  • Terraform will start creating network.

  • You can also verify deployment by visiting Virtual Networks page of Azure.

How to Create Vnet in Azure using Terraform ?

Azure Vnet also called Azure Virtual Network is a network that provides various network-related services in Azure. It connects groups of resources and isolates them from outside access in azure cloud. In this article let’s see how we can set up Azure Virtual Network using Terraform.

Similar Reads

Understanding Of Primary Terminologies

The following are the primary components of Azure Vnet related to Terraform:...

Setup Azure Virtual Network Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform...

Conclusion

We have successfully created Azure Virtual Network with the help of terraform in this article. the configuration described can be further modified to make changes to subnets and network in azure. This is how terraform allows reusable and modifiable configuration of infrastructure....

How to create vnet in azure using terraform – FAQ’s

What is a virtual network (VNet) in Azure?...