What are Terraform modules?

· Category: DevOps & CI/CD

Short answer

Terraform modules are reusable containers for multiple resources that are used together. They encapsulate logic and can be shared across projects.

How it works

A module is a directory containing .tf files. It defines inputs, resources, and outputs. Other configurations call the module using the module block.

Example

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"
  name    = "my-vpc"
  cidr    = "10.0.0.0/16"
}

Why it matters

Modules reduce duplication and enforce standards. They allow teams to abstract complex infrastructure into simple, reusable components.

Tips

  • Publish modules to a private registry.
  • Document inputs and outputs.
  • Use semantic versioning for modules.

Common issues

  • Module sources must be accessible.
  • Version pinning prevents unexpected updates.
  • Circular dependencies between modules cause errors.