Terraform · Infrastructure as code

What is a Terraform module and how do you use one?

MediummodulesreuseregistryUpdated Oct 2026
Quiz this track

Short answer

A module is a directory of Terraform files with inputs, outputs and resources. Root modules call child modules from local paths, Git or the registry, and modules should be versioned and narrowly scoped.

Every Terraform configuration is a module; the one you run is the root module. A child module is called with a module block, receives variables, and returns outputs. Modules package a coherent piece of infrastructure, such as a network, a database or a service baseline.

hcl
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.8.1"

  name = "acme-prod"
  cidr = "10.0.0.0/16"
  azs  = ["us-east-1a", "us-east-1b"]
}

resource "aws_instance" "app" {
  subnet_id = module.vpc.private_subnets[0]
  ami       = data.aws_ami.base.id
}
The module has a versioned source and exposes outputs the root module consumes.

Module sources

  • Local paths such as ./modules/network
  • The public or private registry with an explicit version
  • Git URLs, ideally pinned to a tag or commit
  • S3 or GCS archives for controlled internal distribution

A good module hides incidental complexity and exposes a small, opinionated interface. A bad module wraps every resource one-to-one, forcing callers to pass dozens of pass-through variables and leaking provider details. Design modules around a capability, not around a resource type.

Never rely on a floating module version in production. Pin it, upgrade deliberately, and review the plan for replacements.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between a root and child module?
  2. 2How do you decide a module's boundary?
  3. 3Why pin a registry module version?
  4. 4How do modules receive providers?

Say it in the interview

A module is a reusable directory of Terraform with inputs, outputs and resources. The configuration I run is the root module, and it calls child modules for capabilities like networking or a service baseline. I keep module interfaces small, pin versions, and design around a capability rather than wrapping every resource. That keeps callers from passing dozens of pass-through variables.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.