Lesson 1 of 9 · 24m
Module Interfaces That Age Well
Design modules around stable ownership and narrow contracts instead of mirroring every provider argument.
Most Terraform pain starts as a module that seemed convenient in week one. It exposes forty variables, internally creates a network, three security groups, a database, DNS, and half an observability stack. Six months later nobody knows what changing one input will destroy. Good module design is not about making resources reusable in the abstract; it is about making ownership and change understandable.
A module is an ownership boundary
The first question is not "what can we parameterize?" It is "what lifecycle belongs together?" Resources that must be created, rolled back, and destroyed together belong in the same module. Resources with different owners or different risk profiles should not.
module "app_service" {
source = "./modules/service"
name = "billing-api"
environment = "prod"
image = "registry.internal/billing/api:2026.11.30"
subnet_ids = data.aws_subnets.private.ids
security_group_ids = [aws_security_group.service.id]
desired_count = 3
max_memory_mb = 1024
}The module receives the network objects instead of creating them. That is deliberate. Networking often outlives services, changes less often, and belongs to a different control loop. The interface says exactly what the service needs without hiding a second architecture inside it.
Inputs should express intent, not provider plumbing
If callers write AWS-shaped configuration for every input, your module is just indirection. Prefer intent-level inputs with guarded defaults:
variable "publicly_accessible" {
type = bool
default = false
}
variable "deletion_protection" {
type = bool
default = true
}Inside the module, translate intent into provider detail. Reserve escape hatches such as extra_tags or iam_policy_arns for real exceptions, and name them as exceptions. Once every resource has a settings object input, you no longer have an interface — you have YAML with compilation steps.
Outputs are the public API
Output concrete dependency data: identifiers, endpoints, ARN values, DNS names. Do not output entire provider resources just because you can. Downstream code then couples itself to provider internals you may need to change.
output "service_security_group_id" {
value = aws_security_group.service.id
description = "Security group attached to service tasks."
}Version modules once other teams consume them. A semantic version is a promise about compatibility, not a decorative tag. Breaking input or output changes require a major version and migration notes.
Field notes
- Small modules are not automatically good; twenty tiny modules can be a distributed monolith.
- A module that creates both data and compute usually makes replacement decisions harder than necessary.
- If callers routinely fork the module, the boundary or the interface is wrong — not the callers.
Was this lesson helpful?