Terraform interview question · Most asked
What are Terraform modules, and how do you design good ones?
Summary
A module is a directory of Terraform configuration with input variables and outputs that can be called repeatedly; every configuration is a root module that may call child modules from local paths, Git, or a registry. Good modules encapsulate one meaningful concept (a VPC, a service, a database), expose a small, well-documented interface, and have sensible defaults.
Longer Answer
Guidelines: avoid thin wrappers around a single resource; avoid "god modules" that do everything with dozens of flags; pin module versions (registry versions or Git tags); validate inputs (validation blocks, types); output the values consumers need; and do not configure providers inside reusable modules (pass them in). Document with examples, and test with terraform test or Terratest.
Organisations often publish internal modules in a private registry so teams self-serve approved patterns (encrypted buckets, tagged resources).
Key Points
- Reusable units with inputs and outputs.
- One meaningful concept per module.
- Pin versions; validate inputs.
- No provider configuration inside modules.
More most-asked Terraform questions
- How do you handle secrets in Terraform?
- A terraform plan wants to destroy and recreate a database. What do you do?
- What is Terraform, and how does it work at a high level?
- What is Terraform state, why is it needed, and where should it be stored?
- Walk through terraform init, plan and apply. What does each do?
- How would you run Terraform in CI/CD?
- count or for_each: when do you use each?
- How do you bring existing infrastructure under Terraform management?