Prompt
How do I structure reusable modules for infrastructure as code teams?
Latest observation
A good way to structure reusable IaC modules is to optimize for clear contracts, composability, and versioning rather than trying to make one “universal” module. Teams usually do best with a layered approach:
1) Split by responsibility
Create modules around a single infrastructure concern, for example:
network-vpcsubnetsecurity-grouprds-postgreseks-clusters3-bucketiam-roledns-record
Avoid “platform” modules that do too much, like web-app-stack or full-environment, unless they are thin wrappers over smaller modules.
2) Keep modules small and composable
A reusable module should:
- do one thing well
- expose a small number of inputs
- return outputs that other modules can consume
- avoid hardcoding environment-specific values
This makes it easier for teams to combine modules into different stacks.
3) Define a stable interface
Treat each module like an API:
- Inputs: required variables, optional defaults, types, validation
- Outputs: only what callers actually need
- Behavior: document assumptions and constraints
Example:
- Input:
vpc_cidr - Output:
vpc_id,private_subnet_ids
Good module interfaces reduce coupling and help prevent accidental breaking changes.
4) Separate reusable modules from live environments
A common structure is:
modules/for reusable building blockslive/orenvs/for actual deployments
Example:
repo/
modules/
vpc/
ecs-service/
rds/
live/
dev/
network/
app/
prod/
network/
app/
The live layer wires modules together with environment-specific settings.
5) Standardize conventions across teams
Make modules consistent in:
- naming
- tagging
- labels
- input types
- output names
- security defaults
- logging/monitoring hooks
This reduces cognitive load and makes modules easier to adopt across teams.
6) Version modules explicitly
Use semantic versioning or release tags so teams can safely consume changes.
v1.2.3for stable releases- document breaking changes
- deprecate instead of deleting immediately
If modules are in a shared registry or separate repo, versioning becomes especially important.
7) Test modules like software
Reusable IaC modules should have:
- unit-like checks for formatting and validation
- plan tests
- integration tests in a sandbox account/subscription
- policy checks for security/compliance
For example:
- static validation
- ephemeral test environments
- assertions on expected outputs
- security scanning
8) Minimize cross-module hidden dependencies
Prefer explicit data flow:
- pass IDs, ARNs, names, and config in as inputs
- expose outputs clearly
Avoid modules that implicitly depend on:
- global variables
- shared state assumptions
- manually created resources
9) Use wrappers for opinionated defaults
If some teams need a standardized setup, create a thin wrapper module or environment stack that:
- sets defaults
- composes several lower-level modules
- applies org-wide policies
This keeps the core modules reusable while still supporting standardization.
10) Document usage with examples
Each module should include:
- purpose
- inputs and outputs
- example usage
- common pitfalls
- upgrade notes
A module without documentation is hard to reuse safely.
Practical team pattern
A strong org-level pattern is:
- Foundation modules: network, IAM, logging, encryption
- Service modules: database, queue, compute, storage
- Environment stacks: dev/stage/prod compositions of modules
- Policy modules/controls: guardrails, compliance rules
Example repository layout
infra/
modules/
vpc/
iam-role/
s3-bucket/
rds/
stacks/
dev/
main.tf
staging/
main.tf
prod/
main.tf
examples/
minimal/
production/
tests/
Rule of thumb
If a module has lots of conditional logic for many different use cases, it may be:
- too broad
- trying to be an environment stack instead of a module
- missing a clean abstraction boundary
In that case, split it into smaller modules and compose them at a higher layer.
If you want, I can also give you:
- a Terraform-specific module structure,
- a Pulumi or CloudFormation version, or
- a recommended repo layout for a multi-team platform org.