Unleashing the Cloud: A Guide to Crafting a Virtual Network in Azure with Terraform

Unleashing the Cloud: A Guide to Crafting a Virtual Network in Azure with Terraform

Introduction :-

Embarking on the #cloud journey is a thrilling yet intricate experience. In the realm of #Azure, the prowess of #Terraform becomes a beacon, guiding developers to seamlessly weave a #virtual network that aligns with their architectural vision. This guide is your compass, navigating through the steps of creating a #robust #virtual network, unlocking the true potential of #Azure through the efficiency of #Terraform.

Requirements :-

  • #Terraform should be installed on your system

  • A #Microsoft Azure Portal with full permissions

Steps for deploying a virtual Network using terraform - -

  1. First create a folder with name virtual_network on your Desktop. Within the folder create #terraform configuration files such as main.tf, variable.tf and provider.tf

  2. Copy the below code into your main.tf file.

#main.tf 
resource "azurerm_resource_group" "corp_network"{
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_network" "corp_firewalvnet" {
  name                = var.virtual_network_name
  location            = azurerm_resource_group.corp_network.location
  resource_group_name = azurerm_resource_group.corp_network.name
  address_space       = [var.address_space] 
}

resource "azurerm_subnet" "jumbbox_subnet" {
  name                 = var.subnet_jumbbox_name
  resource_group_name  = azurerm_resource_group.corp_network.name
  virtual_network_name = azurerm_virtual_network.corp_firewalvnet.name
  address_prefixes     = [var.jumbbox_subnet_cidr]
  depends_on = [
    azurerm_virtual_network.corp_firewalvnet
  ]
}

resource "azurerm_subnet" "gateway_subnet" {
  name                 = var.subnet_gateway_name
  resource_group_name  = azurerm_resource_group.corp_network.name
  virtual_network_name = azurerm_virtual_network.corp_firewalvnet.name
  address_prefixes     = [var.gateway_subnet_cidr]
   depends_on = [
    azurerm_virtual_network.corp_firewalvnet
  ]
}

resource "azurerm_subnet" "function_subnet" {
  name                 = var.subnet_function_name
  resource_group_name  = azurerm_resource_group.corp_network.name
  virtual_network_name = azurerm_virtual_network.corp_firewalvnet.name
  address_prefixes     = [var.functions_subnet_cidr]
   depends_on = [
    azurerm_virtual_network.corp_firewalvnet
  ]
}

3. Paste the below code into your variable.tf file.

variable "resource_group_name" {
  description = "The name of the resource group in which the resources will be created."
  type        = string
  default     = "corp-network"
}

variable "location" {
  description = "(Optional) The location in which the resources will be created."
  type        = string
  default     = "East US 2"
}

variable "virtual_network_name" {
description = "Name of virtual_network "
type        = string
default     = "corp_firewalvnet"
}

variable "subnet_jumbbox_name" {
  type = string
  default = "jumbbox-subnet"
}

variable "subnet_gateway_name" {
  type = string
  default = "gateway-subnet"
}

variable "subnet_function_name" {
  type = string
  default = "functions-subnet"
}

variable "address_space" {
description = "VN address space"
type        = string
default     = "10.0.0.0/22"
}

variable "jumbbox_subnet_cidr" {
description = "CIDR for jumbox sunbet"
type        = string
default     = "10.0.1.0/24"
}

variable "gateway_subnet_cidr" {
description = "CIDR for gateway sunbet"
type        = string
default     = "10.0.2.0/24"
}

variable "functions_subnet_cidr" {
description = "CIDR for functions sunbet"
type        = string
default     = "10.0.0.0/24"
}

3. Now u need to get your subscription-id, client-id, client-secret & tenant-id from your #azure portal and paste them into the below provider.tf file

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.42.0"
    }
  }
}

provider "azurerm" {
  subscription_id = "YOUR SUBSCRIPTION-ID"
  client_id       = "YOUR CLIENT-ID"
  client_secret   = "YOUR CLIENT-SECRET"
  tenant_id       = "YOUR TENANT-ID"
  features {}
}

4. Next open a terminal or command propmt window and configure your #azure credentials. Then locate to the folder in which your #terraform files exists.

5. First run the #terraform init cmd and then run the #terraform plan cmd to view the list of resources that should be created when the #terraform configuration is deployed. Last and finally run the #terraform apply cmd to deploy your resources on your portal.

Conclusion :-

In the ever-evolving landscape of #cloud computing, mastering the art of crafting a #virtual network is a fundamental #skill. Through the lens of #Terraform, we’ve demystified the process, empoweri