Interview › Infrastructure as Code (Terraform, Ansible)
What is a Terraform module, and why use modules?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
A Terraform module is a directory of Terraform configuration that exposes inputs and outputs. Modules are used to standardize patterns, reduce duplication, encode security defaults, and give teams a reusable interface for common infrastructure such as VPCs, EKS clusters, IAM roles, and service stacks.
Technical explanation
A module should hide implementation details but expose the decisions callers legitimately need.
Good modules include examples, validation, sane defaults, outputs, documentation, and versioned releases.
Bad modules become black boxes if they expose too little or unsafe free-for-all wrappers if they expose everything.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Write a reusable module for: What is a Terraform module, and why use modules?
2. Create modules/s3_bucket with variables, locals, resource, and outputs:
variable "name" { type = string }
variable "tags" { type = map(string) default = {} }
locals { common_tags = merge(var.tags, { managed_by = "terraform" }) }
resource "aws_s3_bucket" "this" { bucket = var.name tags = local.common_tags }
output "bucket_id" { value = aws_s3_bucket.this.id }
3. Call it from the root module and pass the output to another module:
module "logs" { source = "../../modules/s3_bucket" name = "company-prod-logs" tags = var.tags }
module "app" { source = "../../modules/app" log_bucket_id = module.logs.bucket_id }
4. Add validation, README examples, and a minimal Terratest or plan test before releasing v1.0.0.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?