What are Terraform modules and why do you use them?
Learn what Terraform modules are, how to call them with inputs and outputs, version pinning, and why they make infrastructure reusable and consistent.
Expected Interview Answer
A Terraform module is a reusable, self-contained package of configuration — a folder of .tf files with defined inputs and outputs — that you call to provision a set of related resources without duplicating code.
Every Terraform configuration has a root module, and you can call child modules with a `module` block, passing inputs and consuming outputs. Modules can be sourced locally, from the Terraform Registry, or from Git. They let teams package best-practice patterns (like a standard VPC or database setup) once and reuse them across projects and environments, promoting consistency, testability, and maintainability.
- Eliminates copy-pasted configuration
- Encapsulates best practices in one place
- Enables consistent infrastructure across environments
- Makes configurations easier to test and review
- Supports versioning and sharing via registries
AI Mentor Explanation
A module is like a set training drill your academy has perfected — the same fielding routine reused by every team. Instead of re-teaching it from scratch, each squad runs the drill with their own players (inputs) and gets fielding results (outputs). Terraform modules package a proven infrastructure pattern once so every project runs the same reliable routine instead of reinventing it.
Step-by-Step Explanation
Step 1
Identify repetition
Spot resource groups you configure repeatedly, like a VPC or a standard service.
Step 2
Create the module
Put the .tf files in a folder with variable inputs and output values defined.
Step 3
Call the module
Use a `module` block with a source and pass required inputs.
Step 4
Consume outputs
Reference child module outputs as module.<name>.<output> in the caller.
Step 5
Version and share
Source from a registry or Git with a version pin for stable reuse.
What Interviewer Expects
- Definition of a module as a reusable configuration package
- Knowledge of root versus child modules
- How to pass inputs and read outputs of a module
- Awareness of module sources (local, registry, Git) and version pinning
- Concrete reasons: DRY, consistency, testability
Common Mistakes
- Thinking a module is a single resource rather than a package
- Not pinning a version when sourcing remote modules
- Over-nesting modules until the configuration is hard to follow
- Forgetting to declare outputs a caller needs
- Putting everything in the root module and never abstracting
Best Answer (HR Friendly)
“A Terraform module is a reusable bundle of infrastructure code, like a template. Instead of writing the same setup again for every project, you package it once and reuse it with different settings, which keeps everything consistent and much easier to maintain.”
Code Example
module "network" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.0"
name = "prod-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
}
output "vpc_id" {
value = module.network.vpc_id
}Follow-up Questions
- What is the difference between a root module and a child module?
- How do you pass values into a module and read its outputs?
- Why should you pin module versions?
- Where can module source code come from?
- How do modules help with DRY infrastructure?
MCQ Practice
1. What best describes a Terraform module?
A module is a container of .tf files with defined inputs and outputs, reusable across configurations.
2. How do you reference an output of a module named network?
Child module outputs are accessed with the module.<name>.<output> syntax.
3. Why should you pin a version when using a registry module?
Pinning a version prevents unexpected changes when the module is updated upstream.
Flash Cards
What is a Terraform module? — A reusable package of .tf configuration with defined inputs and outputs.
What is the root module? — The top-level working directory Terraform runs; it can call child modules.
How do you read a module output? — Use module.<name>.<output>, for example module.network.vpc_id.
Why pin a module version? — To get reproducible behavior and avoid breaking changes from upstream updates.