Overview
Introduction
A reusable Terraform module isn't just a resource block, it's a small contract: declared inputs in variables.tf, the resources that use them in main.tf, declared outputs in outputs.tf, and a README explaining how to call it.
This tool generates that whole four-file skeleton from a module name and a single variable/output pair, so the wiring between files is correct from the start.
What Is Terraform Module Generator?
A Terraform module scaffolder that produces main.tf, variables.tf, outputs.tf, and a README.md usage snippet as one combined text output, each clearly delimited by a file-marker comment.
The generated main.tf references the variable you named, and the generated outputs.tf exposes the output you named, so opening all three files together shows exactly how a module's variable flows in and its output flows out.
How Terraform Module Generator Works
You provide a module name, one variable (name, HCL type, default value), and one output (name, value expression referencing a resource attribute).
The tool renders variables.tf with a typed variable declaration and optional default, main.tf with a resource that consumes that variable, outputs.tf with your output pointing at the expression you gave, and a README.md showing the exact `module { }` block a caller would write.
When To Use Terraform Module Generator
Use it when starting a brand-new internal Terraform module and you want the four standard files wired together correctly before filling in the real resource logic.
It's also a fast way to demonstrate to teammates the file-by-file structure HashiCorp recommends for shareable modules.
Often used alongside Terraform Config Generator, Terraform Variables Generator and Terraform Outputs Generator.
Features
Advantages
- Produces all four conventional module files at once, keeping variable and output names consistent across them.
- Includes a working README usage snippet, not just the Terraform files, so the module is documented from the first commit.
- The main.tf's placeholder resource is a valid, applyable null_resource, so the scaffold can be tested with real `terraform apply` before you swap in the actual resource.
Limitations
- Only scaffolds one variable and one output; a real module usually needs several of each, added afterward with the Variables/Outputs generators.
- The main.tf resource is a placeholder (null_resource) meant to be replaced with whatever your module is actually meant to provision.
Examples
Best Practices & Notes
Best Practices
- Replace the placeholder null_resource in main.tf with your actual resource(s) before publishing the module, keeping the same variable references.
- Add a `versions.tf` pinning required_providers and the Terraform version once the module leaves the prototype stage.
- Keep variable descriptions specific enough that a caller never has to open main.tf to understand what a variable controls.
Developer Notes
The generated main.tf intentionally uses `null_resource` with a `triggers` map referencing the input variable, this is a real, applyable resource (part of Terraform's built-in null provider) so the scaffold's variable-to-resource wiring can be verified with `terraform apply` before any cloud-specific resource is substituted in.
Terraform Module Generator Use Cases
- Bootstrapping a new internal Terraform module's file layout
- Teaching the standard main.tf/variables.tf/outputs.tf/README module convention
- Producing a quick reference module to test a CI pipeline's module-testing step before real resources are added
Common Mistakes
- Publishing the module with the placeholder null_resource still in place instead of swapping in the intended cloud resource.
- Renaming the variable in main.tf without also updating variables.tf and the README's example call, breaking the module's contract.
Tips
- Follow up with the Terraform Variables Generator and Terraform Outputs Generator to flesh out variables.tf and outputs.tf beyond the single pair scaffolded here.