tofu-module
Creates or refactors an OpenTofu module with the house file layout, typed and validated inputs, described outputs, a version constraint, a documented README and an examples directory. Use when a new m
Files1
View on GitHubSKILL.mdskills/tofu/tofu-module/SKILL.md
GitHubModule
Write a module someone else can consume without reading its internals.
When to use
- A piece of infrastructure is used more than once or earns its own interface.
- Existing root module code has grown and should be extracted.
Steps
- Define the interface first: what the module owns, what it takes as input, what it returns, and
what it deliberately leaves out. Write this into the module
README.mdbefore the code. - Check for a maintained upstream module that already does the job. Use it and say so instead of writing a new one.
- Create the files:
main.tf,variables.tf,outputs.tf,versions.tf,README.md. Provider configuration stays in the root module, this module only declaresrequired_providers. - Write the variables: typed, described, a default only where safe,
validationwhere a wrong value fails late,sensitive = truefor a secret. - Implement the resources with
for_each, stable names and no environment specific value. - Write only the outputs a consumer needs, each with a description. No secret in an output, even marked sensitive.
- Wire the module into the root module and run the plan workflow. An extraction's first plan must
show moves only, so use
movedblocks ortofu state mv, and show that plan as evidence. - Document the README: purpose, inputs, outputs, an example usage block, requirements. Generate
the inputs and outputs table with
terraform-docswhen it is available. - Add an
examples/directory with at least one working call of the module. - Add
tofu testcases undertests/where a wrong input or a wrong output would otherwise slip through silently. Skip it where the plan workflow already proves correctness. - Report the interface, the plan result and anything the module intentionally leaves out.
Guardrails
- No provider block inside a reusable module, only
required_providers. - No wrapper around an upstream module that only renames its inputs.
- An extraction that changes more than the resource addresses is a bug, not a refactor.
- No secret in an output, sensitive or not.
- An in-repository submodule called by two or more environments of the same repository needs
no
examples/directory and no README inputs/outputs table: the environments calling it are the examples, and the README states purpose, inputs and outputs in prose instead.
References
opentofu-conventionsfor file layout, variables and state rules.tofu-planfor the plan and apply workflow used while extracting or wiring the module.