HashiCorp Terraform Associate 004: Terraform Module Design Patterns
Terraform modules help teams turn recurring infrastructure decisions into reviewed, reusable configuration. A useful module is not simply a folder with resources: it establishes a stable interface, states assumptions, and limits the risk that a small change in one environment accidentally alters another. This matters when an organization uses Terraform across accounts, regions and teams. The current HashiCorp Terraform Associate (004) exam examines configuration, modules, state and safe workflows; practical design judgment makes those concepts stick.
On this page
Start with a narrow responsibility
A network module might own a VPC, subnets, route tables and tags, but should not also decide application database passwords or production deployment schedules. Split modules along operational ownership and lifecycle rather than treating every individual resource as a separate abstraction. Avoid a giant universal module with dozens of booleans: such interfaces are hard to test and make upgrades risky. A root module should compose child modules and express environment-specific choices. Treat a registry module as a product whose users need predictable behavior.
Make variables and outputs deliberate
Define input variables with types, descriptions, useful validation and defaults only when a default has an unambiguous safe meaning. Prefer explicit required values for account IDs, exposure levels and encryption-related settings. Outputs should expose meaningful resource identities or endpoints, not entire secret-bearing objects. Sensitive marking reduces ordinary output display but is not a substitute for protecting state or avoiding secrets in configurations. Examine whether an ephemeral or write-only mechanism is supported for the particular resource/provider before relying on it.
Version modules and providers separately
A module version represents the contract of the reusable component; the provider version controls how resources are implemented. Put provider version constraints in modules and provider selections/lock-file decisions in the root configuration. Pin immutable module releases for production; test updates in a disposable environment, review the plan, and document breaking changes. If a provider schema changes, simply incrementing a module version does not guarantee the stored state is compatible. For a migration, check state moves, resource addresses, data source behavior and lifecycle implications.
Use plans and tests as release gates
For each proposed module change, run formatting and validation, compare a plan against representative environments and evaluate whether any resources will be replaced or exposed. Test a first deployment, an idempotent second run and a deliberate input change. Where practical, use Terraform test facilities and policy checks to protect tagging, encryption and network boundaries. Never paste credentials into a public example, and do not run a destructive apply simply to see whether the plan works. A reviewer should be able to say which resources change and why before approving a production rollout.
Example: a safer shared-network module
Suppose one team needs a network in three environments. A reusable module accepts CIDR ranges, subnet classifications, region, DNS choices and tagging context. The root supplies distinct environment inputs and backend state storage. The module emits IDs that application teams can consume without duplicating topology logic. An unsafe module would quietly create public routes whenever an optional argument was omitted; a safer one requires explicit exposure choices and makes them visible in review. The result is reduced drift and a clearer incident-response ownership map.
What to study and practice
Explain the difference between root and child modules, provider requirements, variables, outputs, local values and data sources. Build a small module that evolves through a backwards-compatible release and then a deliberately breaking change. Compare its dependency graph and plan under each version. Verify official docs when behavior depends on Terraform or provider versions. The most useful outcome is the ability to predict changes, review interfaces and maintain infrastructure safely, not merely recall syntax.