How to Create Vnet in Azure using Terraform ?
Last Updated :
26 Apr, 2024
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.
Understanding Of Primary Terminologies
The following are the primary components of Azure Vnet related to Terraform:
- Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
- Azure Virtual Network: In Microsoft Azure, a Virtual network is an isolated network that protects a group of resources.
- IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.
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.
.png)
- 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.
.jpg)
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.

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.
Similar Reads
How to Create Windows VM in Azure Using Terraform In this article, we will cover the whole process of creating Windows VM in Azure using Terraform. When you have to create multiple VMs, perhaps even identical ones, or machines with nearly identical configurations, it can be repetitive and time-consuming to go through the manual setup process each t
10 min read
How To Create AWS VPN Using Terraform? As associations embrace cloud computing for their Infrastructure needs, the secure and effective association between on-premises organizations and cloud assets becomes pivotal. In the domain of cloud-based DevOps, a Virtual Private Network (VPN) fills in as a fundamental component for laying out sec
6 min read
How to Create App Service in Azure using Terraform Azure App Service is a service that provides a managed platform for deploying applications in the Azure cloud. It supports multiple language applications. App service allows building, deploying and scaling of applications in Azure. Setting up an app service is a complicated process. let's see how we
5 min read
How To Create AKS Cluster In Azure Using Terraform ? Azure AKS also called as Azure Kubernetes Service is service that provides Kubernetes implementation in the Azure cloud. Azure Kubernetes service is a managed service that allows deployment of containerized applications in azure cloud. Setting up a Kubernetes cluster in azure is tedious task so lets
4 min read
How To Create AWS VPC Using Terraform ? Terraform is an IAAC tool used to automate programmatic infrastructure provisioning. Here in this guide, I will first discuss what is AWS VPC. Then I will discuss terraform. After this, I will walk you through the different steps to write a code using Terraform to create a custom AWS VPC using subne
6 min read
How To Create VPC In GCP Using Terraform ? When we are building an application to be hosted on a Cloud Platform we must configure networks and take security measures. If you are building an application that is open to users over the internet, you might want to control who gets access or not, and how users interact with each other. This is es
9 min read