Skip to content

Lesson 4 of 9 · 25m

Module Contracts That Survive Teams

Design modules around stable interfaces, explicit ownership, versioning, and composition instead of turning every internal helper into a public API.

Most Terraform module pain is self-inflicted. Teams start with a tidy little wrapper for a VPC, then five teams add booleans, then the module grows to eighty variables, and eventually nobody knows whether changing enable_nat affects dev, prod, or a regulated environment nobody remembered. A module is a contract. Treat it like one.

The interface is the product

A good module exposes the decisions callers should control and hides implementation details they should not:

module "service_network" {
  source  = "git::ssh://git@github.com/acme/tf-modules.git//network/service?ref=v3.4.1"
  version = "3.4.1"

  name                 = "billing"
  cidr_block           = "10.42.0.0/16"
  availability_zones   = ["us-east-1a", "us-east-1b", "us-east-1c"]
  private_subnet_cidrs = ["10.42.1.0/24", "10.42.2.0/24", "10.42.3.0/24"]
  public_subnet_cidrs  = ["10.42.101.0/24", "10.42.102.0/24", "10.42.103.0/24"]
  tags                 = local.common_tags
}

If callers routinely need to fork or wrap the module differently, the contract is wrong. If callers never use half the inputs, the contract is too broad.

Inputs need types and validation

Terraform variable validation is cheap protection at module boundaries:

variable "cidr_block" {
  type        = string
  description = "VPC CIDR range. Must be a /16 for current subnet layout."

  validation {
    condition     = can(cidrnetmask(var.cidr_block))
    error_message = "cidr_block must be valid CIDR notation."
  }
}

Validate assumptions that are expensive to debug downstream: CIDR shape, allowed sizes, mutually exclusive options, required tag keys, or region support. Do not validate every provider detail; the provider schema already does that.

Outputs are part of the API

Export only what other stacks are expected to consume:

output "private_subnet_ids" {
  description = "Subnet IDs for private workloads."
  value       = aws_subnet.private[*].id
}

Avoid dumping entire resources. Output IDs, ARNs, names, endpoints, and structured objects deliberately. Once another team depends on an output, renaming it is a breaking API change even if Terraform internals changed nothing.

Version like software, not like a folder

Pin module versions in shared infrastructure. main is not a release. Tags and changelogs let teams upgrade deliberately:

v3.5.0  Add optional flow log destination; no replacement.
v3.4.2  Fix incorrect IPv6 egress rule.
v4.0.0  Rename subnet_ids output to private_subnet_ids.

Semantic versioning is only meaningful if breaking changes really move the major version. A replacement of production subnets is breaking even when the variable name stayed the same.

Prefer composition over mega-modules

A service platform module can compose network, database, and compute modules, but avoid one module that configures every cloud primitive in the account. Smaller modules are easier to test, replace, and reason about. The composition belongs in the environment root where ownership and variables are visible.

Field notes

  • A module with ten unrelated enable_* booleans usually hides several modules that are afraid to exist.
  • Never silently change resource naming schemes; names often force replacement or break external integrations.
  • Use terraform-docs or generated examples, but review examples as executable contracts rather than documentation decoration.
  • Test module upgrades in a disposable root with realistic inputs. terraform validate does not prove behavioral compatibility.
  • Internal convenience modules can be local paths. Shared team contracts need versioning and a changelog.

Was this lesson helpful?

Sign in to rate