Eduarn – Online & Offline Training with Free LMS for Python, AI, Cloud & More

Tuesday, March 31, 2026

Terraform Modules Explained with Azure Example (Reusable Infrastructure Made Easy)

 

Terraform, Azure, DevOps, Infrastructure as Code, Terraform Modules, Azure DevOps, Cloud Automation, Eduarn

๐Ÿ“˜ What is a Terraform Module?

In Terraform, a module is a container for multiple resources that are used together.

๐Ÿ‘‰ Simple definition:
A module = reusable Terraform code block

  • Root module → main configuration
  • Child module → reusable components

๐ŸŽฏ Why Use Modules?

  • Reusability (write once, use many times)
  • Standardization across environments
  • Cleaner and organized code
  • Easy scaling for large infrastructure

☁️ Azure Use Case

Using AzureRM Provider, you can create reusable modules for:

  • Resource Groups
  • Virtual Networks
  • Storage Accounts
  • AKS Clusters

๐Ÿ“ Project Structure

/home/user/tfmodule
│
├── provider.tf
├── m_imp_rg.tf
└── modules/
    └── rg/
        ├── rg.tf
        ├── variable.tf
        └── output.tf

⚙️ Module Code (Resource Group)

๐Ÿ“Œ modules/rg/rg.tf

resource "azurerm_resource_group" "rg" {
  name     = var.rg_name
  location = var.rg_location

  tags = {
    environment = "Dev"
  }
}

๐Ÿ“Œ modules/rg/variable.tf

variable "rg_name" {
  default = "demo_rg"
}

variable "rg_location" {
  default = "eastus"
}

๐Ÿ“Œ modules/rg/output.tf

output "rg_output" {
  value = azurerm_resource_group.rg
}

๐Ÿ‘‰ This output exposes the resource group details to the root module.


๐Ÿงฉ Root Module Usage

๐Ÿ“Œ provider.tf

provider "azurerm" {
  features {}
}

๐Ÿ“Œ m_imp_rg.tf

module "prj_001_rg" {
  source      = "./modules/rg"
  rg_name     = "mydemorg"
  rg_location = "eastus"
}

module "prj_002_rg" {
  source      = "./modules/rg"
  rg_name     = "mydemorg2"
  rg_location = "eastus"
}

๐Ÿ‘‰ Same module reused twice → creates two resource groups


๐Ÿ“ค Access Module Output

output "myoutput" {
  value = module.prj_001_rg.rg_output
}

๐Ÿ‘‰ Syntax:

module.<module_name>.<output_name>

๐Ÿ”Ž What Happens Here?

  • Module rg is defined once
  • Called multiple times with different inputs
  • Each call creates a separate Azure Resource Group
  • Outputs allow sharing values

๐Ÿ’ก Real-World Benefits

  • Create 10+ resource groups using one module
  • Maintain consistency across environments
  • Reduce duplication and errors
  • Faster deployments

๐Ÿง  Best Practices

  • Keep modules small and focused
  • Use variables for flexibility
  • Expose only required outputs
  • Follow naming conventions
  • Store modules in version-controlled repos

๐ŸŽ“ Learn with EduArn

At Eduarn, we teach real-world DevOps using:

  • Terraform
  • Azure cloud projects
  • Hands-on labs and corporate scenarios

✔ Retail training (individual learners)
✔ Corporate training (team upskilling)


๐Ÿš€ Final Thoughts

Terraform modules are the foundation of scalable infrastructure design.

If you're not using modules yet, you're:
❌ Writing duplicate code
❌ Increasing risk
❌ Slowing down deployments

๐Ÿ‘‰ Start modularizing today and build like a pro DevOps engineer.


#Terraform #Azure #DevOps #InfrastructureAsCode #TerraformModules #CloudComputing #EduArn

 

 Copy Pest code:

#!/bin/bash

# Create directory structure
mkdir -p tfmodule/modules/rg
cd tfmodule || exit

# Create provider.tf
cat <<EOF > provider.tf
provider "azurerm" {
  features {}
}
EOF

# Create main module usage file
cat <<EOF > m_imp_rg.tf
module "prj_001_rg" {
  source      = "./modules/rg"
  rg_name     = "mydemorg"
  rg_location = "eastus"
}

module "prj_002_rg" {
  source      = "./modules/rg"
  rg_name     = "mydemorg2"
  rg_location = "eastus"
}

output "myoutput" {
  value = module.prj_001_rg.rg_output
}
EOF

# Create module: rg.tf
cat <<EOF > modules/rg/rg.tf
resource "azurerm_resource_group" "rg" {
  name     = var.rg_name
  location = var.rg_location

  tags = {
    environment = "Dev"
  }
}
EOF

# Create module: variable.tf
cat <<EOF > modules/rg/variable.tf
variable "rg_name" {
  default = "demo_rg"
}

variable "rg_location" {
  default = "eastus"
}
EOF

# Create module: output.tf
cat <<EOF > modules/rg/output.tf
output "rg_output" {
  value = azurerm_resource_group.rg
}
EOF

echo "✅ Terraform module structure created successfully!"
echo "๐Ÿ‘‰ Next steps:"
echo "1. terraform init"
echo "2. terraform plan"
echo "3. terraform apply" 

No comments:

Post a Comment

Hands-on Learning vs Theoretical Learning: Which One Actually Gets You Hired in 2026?

๐ŸŽฏ Introduction: The Real Problem Nobody Talks About You studied for years. You passed exams. You collected certificates. But when it’s ...