# Terraform Module

> A Terraform module is a reusable set of resources in a directory, with input variables and outputs. Learn how modules work in Terraform and OpenTofu.

- Source: https://www.itwonderlab.com/terraform-module/
- Published: 2026-06-24
- Updated: 2026-06-24
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

A **Terraform module** is a directory of `.tf` files that groups resources into a reusable unit with [input variables and outputs](https://www.itwonderlab.com/terraform-variables-outputs-locals/). Every configuration is itself a module (the *root module*), and it can call other modules (*child modules*).

```hcl title="main.tf"
module "network" {
  source   = "./modules/network"
  vpc_cidr = "10.0.0.0/16"
}

resource "aws_instance" "web" {
  subnet_id = module.network.private_subnet_ids[0]
  # ...
}
```

The `source` can be a local path, a Git repository, or a registry module such as `terraform-aws-modules/vpc/aws`. Pin the version of external modules. Modules also support [`for_each` and `count`](https://www.itwonderlab.com/terraform-for-each-vs-count/) and can be refactored with [`moved` blocks](https://www.itwonderlab.com/terraform-import-moved-removed/).

Read the [modules tutorial](https://www.itwonderlab.com/aws-terraform-tutorial-terraform-modules/), [share Terraform projects](https://www.itwonderlab.com/share-terraform-projects/) and [project structure](https://www.itwonderlab.com/terraform-project-structure/).
