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

Showing posts with label Terraform Modules. Show all posts
Showing posts with label Terraform Modules. Show all posts

Thursday, April 2, 2026

Terraform Modules in Azure: Step-by-Step Guide with Count, for_each & YAML Examples

 

Terraform Modules in Azure: Step-by-Step Guide with Count, for_each & YAML Examples By EduArn.com

Introduction

In modern cloud environments, writing reusable and scalable infrastructure is critical. Using Terraform with Microsoft Azure, you can achieve this efficiently through Terraform modules.

This blog provides:
✅ Simple module example
count implementation
for_each implementation
✅ YAML-based configuration
✅ Fully working step-by-step code


๐Ÿ“ฆ Step 1: Simple Terraform Module (Basic Example)

๐Ÿ“ Folder Structure

project/
├── main.tf
├── provider.tf
└── modules/
    └── rg/
        ├── main.tf
        ├── variables.tf
        └── outputs.tf

๐Ÿ”น Module Code

modules/rg/main.tf

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

modules/rg/variables.tf

variable "rg_name" {
  type = string
}

variable "rg_location" {
  type = string
}

modules/rg/outputs.tf

output "rg_name" {
  value = azurerm_resource_group.rg.name
}

๐Ÿ”น Root Module

provider.tf

provider "azurerm" {
  features {}
}

main.tf

module "rg" {
  source = "./modules/rg"

  rg_name     = "demo-rg"
  rg_location = "eastus"
}

▶️ Run

terraform init
terraform plan
terraform apply

๐Ÿ” Step 2: Using count in Module (Multiple Resources)

variable "rg_names" {
  default = ["rg-dev", "rg-test", "rg-prod"]
}

module "rg" {
  source = "./modules/rg"

  count       = length(var.rg_names)
  rg_name     = var.rg_names[count.index]
  rg_location = "eastus"
}

๐Ÿ‘‰ Creates:

  • rg-dev
  • rg-test
  • rg-prod

๐Ÿ”„ Step 3: Using for_each (Recommended)

variable "rgs" {
  default = {
    dev  = "rg-dev"
    test = "rg-test"
    prod = "rg-prod"
  }
}

module "rg" {
  source = "./modules/rg"

  for_each = var.rgs

  rg_name     = each.value
  rg_location = "eastus"
}

๐Ÿ“ค Output

output "rg_names" {
  value = {
    for key, mod in module.rg :
    key => mod.rg_name
  }
}

๐Ÿงพ Step 4: Using YAML Configuration (Advanced)

๐Ÿ“ config.yaml

resource_groups:
  dev: rg-dev
  test: rg-test
  prod: rg-prod

๐Ÿ”น Terraform Code

locals {
  config = yamldecode(file("${path.module}/config.yaml"))
}

module "rg" {
  source = "./modules/rg"

  for_each = local.config.resource_groups

  rg_name     = each.value
  rg_location = "eastus"
}

๐Ÿง  Key Concepts

FeatureUse
Module        Reusable code
count        Simple iteration
for_each        Map-based iteration
YAML        External config

⚖️ Count vs for_each

Feature    count            for_each
Input    List            Map
Stability   ❌ Index-based            ✅ Key-based
Recommended  ⚠️ Limited            ✅ Yes

๐ŸŽฏ Best Practices

  • Use modules for reusable design
  • Prefer for_each over count
  • Use YAML for external configuration
  • Keep modules small and focused
  • Avoid hardcoding values

๐ŸŽ“ Learn with Eduarn

At Eduarn, we provide hands-on training on:

  • Terraform
  • Microsoft Azure

✔ Real-time projects
✔ Corporate training
✔ Job-oriented learning

๐Ÿ‘‰ Visit: https://eduarn.com


๐Ÿš€ Conclusion

Terraform modules are essential for building scalable, reusable, and production-ready infrastructure.

By mastering:

  • Basic modules
  • count
  • for_each
  • YAML configs

๐Ÿ‘‰ You become a real-world DevOps engineer


๐Ÿ”ฅ Hashtags

#Terraform #Azure #DevOps #InfrastructureAsCode #CloudAutomation #TerraformModules #EduArn

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" 

Terraform Modules in Azure: Step-by-Step Guide with Count, for_each & YAML Examples

  Introduction In modern cloud environments, writing reusable and scalable infrastructure is critical. Using Terraform with Microsoft Azur...