Terraform Modules: What They Are and How to Create One
Fast recall
Quick revision
Core answer
Whether you understand modules as Terraform's primary abstraction for code reuse and can describe the structure of a well-designed module with clear inputs, outputs, and documentation.
Creating a module that hard-codes values that should be variables, making it useless for reuse, or not providing outputs that callers need to reference the created resources.
Discuss the module composition pattern, when to use the Terraform Registry vs internal modules, module versioning with Git tags, and the trade-off between abstraction and flexibility.
Interview-ready guide
Interview answer
A Terraform module is a directory of .tf files with explicit input variables and output values that packages an infrastructure pattern for reuse. Every Terraform configuration is technically a module — the distinction is whether other configurations call it.
Module structure
- A module directory contains at minimum three files by convention:
main.tffor resources,variables.tffor input declarations with types and descriptions, andoutputs.tffor values the module exports to callers variables.tfdeclares what the module accepts:variable "instance_type" { type = string; description = "EC2 instance type"; default = "t3.micro" }— callers passinstance_type = "t3.large"in themoduleblock; the default makes the variable optionaloutputs.tfexposes values from inside the module:output "alb_dns_name" { value = aws_lb.main.dns_name }— the caller accesses it asmodule.my_service.alb_dns_nameand can pass it to other resources
Calling a module
- A
moduleblock in any.tffile invokes a module:module "payment_service" { source = "./modules/ecs-service"; service_name = "payment"; container_image = var.payment_image }—sourceis a local path, a Git URL, or a Terraform Registry address - After adding or changing a
source, runterraform initbeforeplan— Terraform downloads and installs the module; failing to run init after a source change causes a "module not installed" error during plan - Pin module versions from the Registry:
version = "~> 5.0"— the~>operator allows patch updates but not minor version changes; unpinned modules silently upgrade and can break working configurations
When to extract a module
- Extract a module when the same resource group (ECS task + ALB + target group + security group) is defined for every microservice — writing it eight times is duplication that modules eliminate
- A good module has a narrow, well-defined purpose — a module that creates a VPC, an EKS cluster, and an RDS database is too broad; three separate modules that compose are easier to update and test independently
Terraform Modules: What They Are and How to Create One
Direct Answer
A Terraform module is a reusable directory of .tf files with defined input variables, output values, and resources. Create one by extracting repeated infrastructure patterns into a named directory with variables.tf, main.tf, and outputs.tf.
Code Example
# Module structure: modules/spring-app/
# ├── variables.tf — inputs
# ├── main.tf — resources
# └── outputs.tf — outputs
# variables.tf
variable "app_name" {
description = "Name of the Spring Boot application"
type = string
}
variable "instance_count" {
description = "Number of ECS tasks to run"
type = number
default = 2
}
variable "docker_image" {
description = "Docker image URI (ECR)"
type = string
}
# main.tf — creates ECS service, task definition, ALB target group
resource "aws_ecs_service" "app" {
name = var.app_name
desired_count = var.instance_count
task_definition = aws_ecs_task_definition.app.arn
# ... other config
}
# outputs.tf
output "service_url" {
description = "URL of the deployed application"
value = "https://${aws_lb.app.dns_name}"
}
# Calling the module from root configuration
module "payment_service" {
source = "./modules/spring-app"
app_name = "payment-service"
instance_count = 3
docker_image = "123456789.dkr.ecr.us-east-1.amazonaws.com/payment:v1.2.3"
}
# Reference module output
output "payment_url" {
value = module.payment_service.service_url
}
Common Mistakes & Pitfalls
Creating a module that hard-codes values that should be variables, making it useless for reuse, or not providing outputs that callers need to reference the created resources.
Check your understanding
Answer these without looking back at the article.
- How do you version and publish a Terraform module?
- When should you use a public Registry module vs write your own?
- How do you test a Terraform module?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.