Documenting and publishing modules
Generate input and output docs with terraform-docs, then publish with release tags to the Terraform registry, the OpenTofu registry or a private one.
Writing a module: inputs, outputs and structureModule sources and versioning
Documenting and publishing modules
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Inputs and outputs are already documented in the code, through each variable's `description` and `type`. A tool like `terraform-docs` turns them into tables so the README never drifts from the code.
- Put generated docs between marker comments, and run the generator in CI or a pre-commit hook so a stale README fails the build.
- The public Terraform registry needs a public GitHub repository named `terraform-<PROVIDER>-<NAME>`, the standard module structure, and at least one `x.y.z` release tag. Pushing a new tag releases a new version.
- A private registry, HCP Terraform's or one that implements the registry protocol, drops the GitHub-only rule. Git tags with `?ref=` work with no registry at all.
- Treat versions as a contract. A removed input, a renamed output or a changed default is a breaking change and deserves a major version, with `moved` blocks so callers' state follows.
The README that lied
A team writes a module and a README with a nice table of inputs. Over a year, people add nine variables and remove three. Nobody touches the table. A new user follows it, passes an argument that was deleted, and gets a plan error. The information the table was meant to hold was always in the code: every variable and output block has a name, a type and, if you wrote one, a description. The job is to generate the table from there.
terraform-docs
terraform-docs is an open-source command line tool that reads a module’s .tf files and writes Markdown for its inputs, outputs, providers and requirements. I couldn’t install it here, so I wrote a small stand-in in Python to show the idea on the module from the previous lesson. It reads variables.tf and outputs.tf and prints tables in a similar shape. The real tool’s output differs in details.
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| cidr_block | CIDR for the VPC. | `string` | n/a | yes |
| azs | Availability zones to spread subnets over. | `list(string)` | n/a | yes |
| enable_nat | Create a NAT gateway for private subnets. | `bool` | `true` | no |
| tags | Tags applied to every resource. | `map(string)` | `{}` | no |
## Outputs
| Name | Description |
|------|-------------|
| vpc_id | ID of the VPC. |
| private_subnet_ids | IDs of the private subnets, one per AZ. |
That is why the previous lesson insisted on a description for every variable and output. They are the documentation source.
The real tool is used in the wild. The public terraform-aws-modules/vpc/aws README contains a generated section, which I confirmed by fetching its README from the registry API:
<!-- BEGIN_TF_DOCS -->
## Requirements
| Name | Version |
| ---- | ------- |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 6.28 |
The BEGIN_TF_DOCS marker is how the tool knows which part of the file it owns, so the hand-written intro above it survives each run. The usual setup is to run the tool from a pre-commit hook, and again in CI with a check that the working tree has no changes afterwards. A stale table then fails a pull request. Carl Lindberg’s video, below, shows the GitHub Actions version. I did not run his workflow.
Docs also include things the generator cannot write: what the module is for, one honest example under examples/, upgrade notes, and a statement of what it does not do. The standard module structure expects a README at the root and in each nested module, and says the README does not need to document inputs and outputs, because tooling generates them.
Releases are tags
The public Terraform registry works from Git. The publishing docs list the requirements:
- The module is in a public GitHub repository. This applies to the public registry only, and a private registry can ignore it.
- The repository is named
terraform-<PROVIDER>-<NAME>, such asterraform-aws-ec2-instance. - The repository description is one sentence, used as the module’s short description.
- The layout follows the standard module structure.
- Releases are tags named as a semantic version, optionally prefixed with
v, likev1.0.4or0.9.2. At least one is needed to publish, and tags that don’t look like versions are ignored.
After the one-time upload, you release by pushing a tag. The registry parses the module’s name, documentation, inputs, outputs and dependencies, and does the same for submodules and examples. This is from the docs, and I did not publish anything. I read the result of someone else’s publish instead. The public API for the VPC module reports 247 versions, and the OpenTofu registry reports the same count for the same module. The x-terraform-get header I showed in the sources lesson pointed at a commit, and that is the consequence of a tag.
Other places to publish
A public registry is not the only option.
- A private registry. HCP Terraform and Terraform Enterprise include one, and a service implementing the registry protocol works too. It drops the GitHub requirement.
- Plain Git with tags. Callers write
git::...?ref=v1.4.2. There is no search page or generated docs, but nothing to run. - The OpenTofu registry. OpenTofu has its own registry, which serves the same module protocol, so
sourceaddresses for public modules resolve with either tool. Providers are separate: the registry a tool uses is part of the move between them, covered in the OpenTofu migration lesson.
Treat versions as a contract
Callers pin your module, so what a version means matters. A new optional variable with a safe default is a minor release. A bug fix is a patch. Removing a variable, renaming an output, or changing a default so existing plans change are breaking, and deserve a major version and a note saying how to upgrade. If a refactor changes resource addresses inside the module, ship moved blocks, as covered in the moved blocks lesson, so callers’ state follows without a destroy. Keep them in the module for as long as old versions exist in the wild.
About the video
The embed is Carl Lindberg’s “Effortless Terraform documentation, here’s how!”, published May 2025, 14 minutes. Its description says it shows terraform-docs with Azure modules, generating .tfvars files, and generating docs automatically in GitHub CI/CD pipelines. That covers the documenting half of this lesson, with Azure examples. It does not cover publishing, which comes from the registry docs above and WORLD OF AWS’s August 2025 video in deeper, a small-channel walkthrough with timestamps that include repository naming, versioning and publishing on the registry. English was judged from the title, description and the English auto-caption track.
Where this fits
This ends the modules group. The next group is about how real repositories are laid out and how one codebase serves several environments: repository layouts for Terraform.
Check yourself
Why generate a module's input table instead of writing it in the README?
A hand-written table drifts as soon as someone adds a variable. The tool reads the `variable` blocks and their descriptions, so the table matches the code, and CI can fail the build when it is out of date.
You push the tag `latest` to a module repository that is on the public registry. Does a release appear?
No. The registry reads tags that are semantic versions, optionally prefixed with `v`, like `v1.0.4`. Other tags are ignored.
You rename an output in a published module. How should you release it?
As a major version, because callers that read the old output name will break. Say so in a changelog, and add `moved` blocks if any resource addresses changed.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.