Skip to main content

Terraform Module

A Terraform module is a folder of Terraform files that creates a related set of resources as one reusable unit. Every configuration is a module, and the term usually means a child module called from a root module.

What Is a Terraform Module?

The directory where you run Terraform is the root module. A child module is called with a module block, takes inputs through variables, and returns outputs.

HCL
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
resource "aws_instance" "app" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t3.medium"
subnet_id = module.vpc.private_subnets[0] # module output
}

Write a module when a pattern repeats or another team needs it without learning its internals.

Common Mistake

Putting a backend block or provider configuration inside a child module. Keep both in the root module.

Frequently Asked Questions

What is the difference between a root and a child module?

The root module is the directory you run Terraform from. A child module is a separate directory or package the root calls through a module block.

When is a module a bad idea?

When it wraps a single resource or is used only once. Extra layers make readers jump between folders.

Found a mistake, an outdated step or a broken link?