Open In App

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.

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

Step 2: Set Up Azure CLI

brew update && brew install azure-cli

Step 3: Configure Azure CLI

az login



Step 4: Create Terraform Code

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"
}
}
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

terraform init

terrraform apply

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?

A virtual network is a logically isolated network in Azure that allows resources to communicate with each other, the internet, and on-premises networks securely. It acts as a private network in the cloud.

What tools do I need to create a virtual network in Azure using Terraform?

You need Terraform installed on your local machine or a CI/CD pipeline, and access to an Azure subscription with the necessary permissions to create resources.

Can I customize the virtual network further?

Yes, you can customize various aspects of the virtual network such as subnets, security rules, DNS settings, and peering configurations according to your requirements.

How do I manage changes to the virtual network over time?

You can version control your Terraform configuration files and use Terraform’s plan and apply workflow to manage changes. When you need to update the virtual network, you modify the Terraform configuration and apply the changes.

Are there any best practices I should follow when creating a virtual network in Azure with Terraform?

Yes, follow best practices such as using variables for parameters, organizing your Terraform code into modules for reusability, and using remote state management for collaboration and concurrency.


Article Tags :