Terraform Module

· 1 min read · Terraform

A Terraform module is a directory of .tf files that groups resources into a reusable unit with input variables and outputs. Every configuration is itself a module (the root module), and it can call other modules (child modules).

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 and can be refactored with moved blocks.

Read the modules tutorial, share Terraform projects and project structure.

More tutorials that use Terraform Module

#Terraform #OpenTofu