Reusable interfaces and validation
Design the public contract
A module interface is the set of inputs a caller supplies and outputs it can depend on. A useful module hides an implementation decision while exposing enough information for its consumers. For example, a naming module can standardize service names and tags without creating a cloud resource. Define its accepted settings, validation, defaults, and output shape before writing the caller. A small module is not automatically better: avoid wrapping a single resource merely to expose every one of its arguments unchanged.
Declare typed inputs and defaults
The following complete child module belongs in modules/naming/main.tf. It uses object attributes to group related settings. The optional tags attribute defaults to an empty map. Both the environment and service name have explicit rules. All examples retain the provisional Terraform 1.14.8 pin; execution and exact patch support have not been verified.
variable "service" { description = "Logical service settings." type = object({ name = string environment = string tags = optional(map(string), {}) }) nullable = false validation { condition = contains(["dev", "test", "prod"], var.service.environment) error_message = "Environment must be dev, test, or prod." } validation { condition = can(regex("^[a-z][a-z0-9-]{0,19}$", var.service.name)) error_message = "Name must start with a lowercase letter and contain at most 20 lowercase letters, digits, or hyphens." }}
locals { name = "${var.service.environment}-${var.service.name}" tags = merge(var.service.tags, { environment = var.service.environment })}
output "metadata" { description = "Canonical name and tags for downstream consumers." value = { name = local.name tags = local.tags }}nullable = false disallows a null object itself, not null nested attributes in general. The validation rules here reject unsupported environment and name values. The name pattern is a local organizational convention, not a universal cloud naming rule. merge resolves duplicate keys using the later map, so callers cannot override the canonical environment tag with a different value. Document this precedence.
Call with minimal settings
The following root module call omits tags intentionally.
module "naming" { source = "./modules/naming" service = { name = "catalog" environment = "test" }}The expected metadata is name test-catalog and tags containing environment=test. An omitted or explicitly null optional tags attribute receives its non-null default. An environment of qa fails the declared contract. A name of Catalog fails the naming pattern. No external API or provider is needed for this naming logic.
Expose only stable outputs
The metadata output is a small object containing name and tags. Callers need not know which locals computed it. Include output descriptions, input types, default values and an example invocation in the module README. Avoid exposing an entire provider object merely for convenience: it creates a broad contract and may expose sensitive fields. A narrow output is still a public interface, so document any intended change to its meaning.
Review optional features
A default should be safe and predictable. For example, an optional map of extra labels can be empty, but a required network identifier should not default to an arbitrary production network. If a module creates optional infrastructure, explain which objects are absent when disabled and what its outputs become. Use explicit stable keys for independently managed instances. Do not silently change existing defaults in a new release. Test a minimal call, a typical call with extra tags and invalid input separately.
Practice
In a copy of the fixture, set environment to test and omit tags. Predict the name and tags before running any commands. Then try an owner tag and a conflicting environment tag; explain why the canonical environment wins. Identify which part of the interface is responsible for rejecting an uppercase name.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reusable interfaces and validation — knowledge check
Review interfaces, composition and upgrade safety.
What is the public interface of a Terraform child module?
What happens if tags is omitted in the example?
Which environment tag wins when caller tags conflict?
What does nullable = false on the service variable guarantee?
Which is a potentially breaking contract change?