- Break infrastructure into reusable modules with clear interfaces.
- Pass variables between root modules and child modules.
- Manage module versioning and the module registry workflow.
- Use
depends_on,for_each, and data sources to handle relationships.
- Call a module:
module "network" { source = "./modules/network" cidr_block = "10.0.0.0/16" }
- Pin module versions:
version = "~> 2.0" - Explicit dependency:
depends_on = [module.network]
Create a simple module structure:
mkdir -p modules/random_pet
cat <<'HCL' > modules/random_pet/main.tf
resource "random_pet" "this" {
prefix = var.prefix
}
output "name" {
value = random_pet.this.id
}
HCL
cat <<'HCL' > modules/random_pet/variables.tf
variable "prefix" {
type = string
}
HCLThen consume it in main.tf and run terraform init && terraform apply to observe the generated pet name.