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

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

No comments:

Post a Comment

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...