Terraform Module Best Practices: Structure, Versioning and Composition

Write reusable Terraform modules: inputs and outputs, module structure, versioning and registries, composition over deep nesting, testing and anti-patterns.

Published December 4, 2026

Modules are Terraform's unit of reuse. A good module packages a well-understood piece of infrastructure (a VPC, a service with its load balancer and alarms, a database with backups) behind a small, documented interface. A bad module wraps every resource attribute as a variable, nests other modules five levels deep and breaks every consumer on each change.

The concept

A module is a directory of .tf files with variables (inputs), resources and outputs. Root modules are what you run plan and apply in; child modules are called from root modules. Design modules around a purpose with opinionated defaults, expose only inputs consumers genuinely need to vary, validate inputs, and output the identifiers other configurations need.

Version modules like libraries. Publish them to a registry (public, private, or a git repository with tags), pin versions in consumers, use semantic versioning, and document breaking changes. Prefer composition, root modules wiring several focused modules together, over deep hierarchies of modules calling modules.

Module structure

modules/service/main.tfvariables.tfoutputs.tfversions.tfresourcesinputs + validationIDs, URLsprovider constraints
Conventional module file layout.

Good vs bad modules

Good moduleAnti-pattern
Clear purpose ("web service with ALB + alarms")Thin wrapper around one resource
Few, validated inputs with sensible defaultsEvery attribute exposed as a variable
Pinned versions via registry or git tagsConsumers reference main branch
Composition in root modulesModules nested many levels deep
Outputs for integrationConsumers reach into internal resources

Composition

Root module prod/Compositionwires modules togethermodule "network"v2.1.0module "database"v1.4.2module "service"v3.0.1
Root modules compose focused, versioned modules.

Practical examples

Variables with validation

Catch mistakes at plan time.

variable "environment" {
  type        = string
  description = "Deployment environment"
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "environment must be dev, staging or prod."
  }
}

variable "instance_count" {
  type    = number
  default = 2
}

Consuming a versioned module

Pin to a tag or registry version.

module "payments_service" {
  source  = "git::https://github.com/example/tf-modules.git//service?ref=v3.0.1"
  name        = "payments"
  environment = "prod"
  image       = "registry.example.com/payments:2.3.1"
}

output "payments_url" { value = module.payments_service.url }

Test a module

terraform test runs plans/applies with assertions.

# tests/service.tftest.hcl
run "defaults" {
  command = plan
  variables { name = "demo", environment = "dev", image = "nginx:1.27" }
  assert {
    condition     = aws_ecs_service.this.desired_count == 2
    error_message = "default desired_count should be 2"
  }
}

Common mistakes

  • Modules that expose dozens of pass-through variables.
  • Unpinned module sources that change underneath consumers.
  • Breaking changes without a major version bump.
  • Deep nesting that makes plans hard to understand.

Key takeaways

  • Modules package a purpose behind a small interface.
  • Validate inputs, provide defaults, output what consumers need.
  • Version and pin modules like libraries.
  • Compose in root modules; avoid deep nesting.

FAQ

Should I use public registry modules?

They are a great starting point; review them, pin versions, and fork or wrap when you need different defaults.

How big should a module be?

Big enough to represent a meaningful unit you deploy together, small enough to understand in one sitting.

How do I refactor resources into a module without recreating them?

Use moved blocks to tell Terraform the new address of existing resources.

More DevOps articles