Writing a module: inputs, outputs and structure
Design a module's interface first: which variables are required, which have defaults, what to output, the standard file layout and where providers belong.
What modules are and the root moduleType constraints and complex variables
Writing a module: inputs, outputs and structure
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A module's variables and outputs are its public interface. Callers depend on them, so choose them first and change them carefully.
- A variable with no `default` is required, and Terraform fails the caller with a missing-argument error. Give a default only when the default is correct for most callers.
- Give every variable a `type` and a `description`, validate what you can, and output only what a caller needs to wire things together, such as IDs and ARNs.
- The standard layout is `main.tf`, `variables.tf`, `outputs.tf`, a README, a license and an `examples/` directory. Declare provider requirements in a `required_providers` block, and do not put `provider` blocks in a module others call.
- A wide interface is a design smell. The popular AWS VPC module has 239 inputs, none required. That is flexible for its audience and expensive for yours.
The interface is the module
The first version of a module is easy: lift the resources into a directory, replace the values that differ with variables, and you are done in an afternoon. Six months later, eleven teams call it, and nobody can rename a variable without a coordinated change across eleven repositories. The resources are the implementation. The variables and outputs are a promise. So design them first, and think of the module as a function you are going to publish.
Inputs
A variable block is an input. The decisions are about what to expose and what to require.
variable "cidr_block" {
description = "CIDR for the VPC."
type = string
validation {
condition = can(cidrnetmask(var.cidr_block))
error_message = "cidr_block must be a valid CIDR."
}
}
variable "azs" {
description = "Availability zones to spread subnets over."
type = list(string)
}
variable "enable_nat" {
description = "Create a NAT gateway for private subnets."
type = bool
default = true
}
This is hand-written, from the language docs. It is a module’s variables file, and I did not run it, as there is no Terraform on this machine. Three rules cover most of it.
- No default means required.
cidr_blockandazshave none, so a caller who leaves them out gets an error. Use that for values that have no safe default. - A default is a commitment.
enable_nat = truesays that is what most callers want. Defaults are cheap to add and hard to change later, since changing one silently changes every caller’s plan. - Always
typeanddescription. The type makes errors early and the description becomes documentation, as the documenting lesson shows. Addvalidationfor values that can be wrong in obvious ways. The type constraints lesson covers objects and optional attributes for when a list of strings is not enough.
To see what a mismatched call looks like, I wrote a small checker, a Python stand-in that reads the variables.tf above and compares a call’s arguments against it. Its wording is mine, not Terraform’s:
call 1: cidr_block only
Error: Missing required argument "azs".
call 2: cidr, azs, enable_nat_gateway (typo)
Error: Unsupported argument "enable_nat_gateway": module has no variable named "enable_nat_gateway".
call 3: cidr, azs
ok
Real Terraform reports both kinds of mistake at plan time. That is why a typed, small interface is useful: errors point at the call.
Outputs
An output is what a caller can read, as module.network.vpc_id. Output what other modules will need to connect to yours: IDs, ARNs, names, endpoints. Do not mirror every attribute. Each output is another thing callers can depend on, and one you cannot remove without a breaking change. Give each a description. Mark one sensitive = true if it carries a secret, as in the lesson on sensitive values.
How wide is too wide
I looked at a popular module for a reference point, using the public registry API. The terraform-aws-modules/vpc/aws module, at version 6.9.0 when I looked, has 239 inputs, 119 outputs and 79 resources, and none of its inputs is required:
$ curl -s https://registry.terraform.io/v1/modules/terraform-aws-modules/vpc/aws | jq -c '.root | {inputs: (.inputs|length), outputs: (.outputs|length), resources: (.resources|length)}'
{"inputs":239,"outputs":119,"resources":79}
That module serves a very large audience with every VPC shape, so its width is a choice. Your module usually has a smaller audience, and a smaller interface is easier to review and keep stable. When you notice yourself adding the fifteenth boolean toggle, consider two modules instead.
Layout
The docs describe a standard module structure, recommended for modules in separate repositories. Only the root module itself is required, and the tooling, including the registry, uses the layout to generate documentation. The recommended files are:
main.tf: the primary entry point. Nested module calls go here.variables.tfandoutputs.tf: the declarations.README.md, and aLICENSEfile.examples/: working examples that call the module.modules/: optional nested modules for pieces advanced users might want alone.
A versions.tf with required_providers is common, and it matters. The docs say every module must declare the provider versions it needs, so Terraform can find one version that satisfies all modules. The provider docs add a rule that matters more: a module intended to be called by other modules must not contain provider blocks. Provider configurations are defined only in the root module and passed down, by inheritance or with the providers argument. A module with its own provider configuration cannot use for_each, count or depends_on on its calls.
Habits
Write the call first. Before writing the module, write the module block you want callers to type. If it takes more than six lines for the common case, the interface is too wide. Keep resource names inside the module stable too, because renaming aws_vpc.this is the same state problem the moved block lesson covers. Avoid reaching outside with data sources for things the caller knows. Pass them in.
About the video
The embed is Bryan Krausen’s “Learn to Create Local Modules using Terraform”, published March 2025, 23 minutes, an excerpt of his HashiCorp Terraform course. His description says he writes a very basic module and gets outputs from it, to pass into other modules or elsewhere in the code. It does not cover the standard structure, version constraints or provider rules, which this lesson adds from the docs. Ned Bellavance’s older “Creating Modules in Terraform”, from December 2020, is in deeper, and its description mentions best practices and publishing. English was judged from the title, description and the English auto-caption track.
Where this fits
A module in a local folder is the easy case. Teams share them through a registry or Git, which raises the question of versions: module sources and versioning.
Check yourself
A caller omits an argument for a variable that has no `default`. What happens?
Terraform stops with a missing required argument error. A variable without a default is required, which is the way to force callers to make a choice.
Why shouldn't a reusable module contain a `provider` block?
Provider configurations belong to the root module and are passed down. A module with its own provider block cannot be used with `for_each`, `count` or `depends_on`, and it fixes region and credentials for everyone who calls it.
A module creates ten resources but callers only need the VPC ID and subnet IDs. What should it output?
The VPC ID and subnet IDs, with descriptions. Outputs are the contract. Exposing every attribute makes each one something callers can depend on, and you cannot change it later without breaking them.
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.