Terraform Code For Creating Resource Group and vnet in Azure using Modules - BRS MEDIA TECHNOLOGIES
Azure-Terraform

Terraform Code For Creating Resource Group and vnet in Azure using Modules

Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language, or optionally JSON.

Below codes is for Creating Resource Group and vnet in Azure using Modules and it is tested in Azure Platform:

Privider.tf

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  tenant_id       = var.tenant_id
}

Variable.tf

variable "subscription_id" {
  description = "Enter Subscription ID for provisioning resources in Azure"
}
variable "client_id" {
  description = "Enter Client ID for Application in Azure AD"
}
variable "client_secret" {
  description = "Enter Client Secret for Application in Azure AD"
}
variable "tenant_id" {
  description = "Enter Tenant ID / Dirctory ID of your Azure AD. Run Get-AzureSubscription"
}

main.tf


module "Module" {
  source         = "./Module"
  resource_group = "Build-RG"
  location       = "centralindia"
  vnet           = "Build-vnet"

}

Module

main.tf

resource "azurerm_resource_group" "example" {
  name     = var.resource_group
  location = var.location
}

resource "azurerm_virtual_network" "example" {
  name                = var.vnet
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = [var.address_space]

}

resource "azurerm_subnet" "subnet" {
  name                 = var.subnet_name[count.index]
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["${var.subnet_prefix[count.index]}"]
  count                = length(var.subnet_name)
}

variable.tf


variable "resource_group" {
  default = "Dabang-RG"
}

variable "location" {
  default = "westus"
}

variable "vnet" {
  default = "Dabang-vnet"

}

variable "address_space" {
  default = "10.0.0.0/16"

}

variable "subnet_name" {
  default = ["Subnet-1", "Subnet-2", "Subnet-3"]

}

variable "subnet_prefix" {
  default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]

}

command.sh

# make sure terraform CLI is installed
terraform

# format the tf files
terraform fmt

# initialize terraform Azure modules
terraform init

# validate the template
terraform validate

# plan and save the infra changes into tfplan file
terraform plan -out tfplan
terraform plan -out tfplan --var-file="/Config/config.perf.tfvars"

# show the tfplan file
terraform show -json tfplan
terraform show -json tfplan >> tfplan.json

# Format tfplan.json file
terraform show -json tfplan | jq '.' > tfplan.json

# apply the infra changes
terraform apply tfplan

# delete the infra
terraform destroy

# cleanup files
rm terraform.tfstate
rm terraform.tfstate.backup
rm tfplan
rm tfplan.json
rm -r .terraform/



Key Terms:

  • azure
  • ,
  • Open Source Software
  • ,
  • Terraform

Other Angel Softwares

WinSCP

WinSCP utility to transfer files

Contents1 WinSCP utility to transfer files to Unix and Linux from Windows1.1 Security1.2 Built-in Text Editor1.3 Key Features:1.4 Summary1.5 Downloads1.6 […]

PuTTY

PuTTY utility to connect Unix and Linux

Contents1 PuTTY is a utility to connect Unix and Linux from Windows1.0.1 Downloads1.1 PuTTYgen1.1.1 How to use PuTTYgen?1.1.2 Types of […]

7zip

7zip Popular file compression utility

Contents1 7-Zip is a popular open-source file compression utility1.0.1 Key Features:1.0.2 Download1.0.3 Key Terms: 7-Zip is a popular open-source file […]

TrueNAS Scale Logo

Open Storage at Scale-TrueNAS Scale

TrueNAS SCALE is the latest member of the TrueNAS family and provides Open Source HyperConverged Infrastructure (HCI) including Linux containers and […]

Terraform Logo

HashiCorp Terraform-Automate Infrastructure on Any Cloud

Contents1 Build, change, and destroy infrastructure with Terraform.1.0.1 How does Terraform work?1.0.2 The core Terraform workflow consists of three stages:1.0.3 […]

Vagrant Logo

HashiCorp Vagrant – Development Environments Made Easy

Contents1 Introduction to Vagrant1.0.1 Why Vagrant?1.0.2 Powerful features1.0.3 Vagrant vs. Terraform1.0.4 Downloads1.0.5 Key Terms: Introduction to Vagrant Vagrant is a […]