0
0
TerraformHow-ToBeginner · 4 min read

How to Use Local Modules in Terraform: Simple Guide

To use a local module in Terraform, create a module folder with configuration files and call it in your root configuration using the module block with the source set to the local path. This lets you organize and reuse infrastructure code easily within the same project.
📐

Syntax

A local module is referenced in your Terraform configuration using a module block. The source attribute points to the relative path of the module folder. You can pass variables to the module and use outputs from it.

  • module <name>: Defines the module block with a unique name.
  • source: Path to the local module folder (e.g., ./modules/my_module).
  • variables: Inputs passed to the module.
  • outputs: Values exported by the module for use outside.
terraform
module "example_module" {
  source = "./modules/example"

  variable1 = "value1"
  variable2 = "value2"
}
💻

Example

This example shows a root Terraform configuration calling a local module that creates an AWS S3 bucket. The module folder contains the bucket resource and an output for the bucket name.

terraform
# Root configuration (main.tf)
module "s3_bucket" {
  source = "./modules/s3_bucket"

  bucket_name = "my-unique-bucket-12345"
}

output "bucket_name" {
  value = module.s3_bucket.bucket_name
}


# Module configuration (modules/s3_bucket/main.tf)
variable "bucket_name" {
  type = string
}

resource "aws_s3_bucket" "this" {
  bucket = var.bucket_name
  acl    = "private"
}

output "bucket_name" {
  value = aws_s3_bucket.this.bucket
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: bucket_name = "my-unique-bucket-12345"
⚠️

Common Pitfalls

Common mistakes when using local modules include:

  • Incorrect source path: The path must be relative to the root configuration file.
  • Missing variables: All required variables in the module must be provided.
  • Not initializing Terraform: Always run terraform init after adding or changing modules.
  • Output name conflicts: Avoid duplicate output names in root and modules.
terraform
/* Wrong: source path points outside project or typo */
module "bad_module" {
  source = "../wrong_path/module"
}

/* Right: correct relative path */
module "good_module" {
  source = "./modules/good_module"
}
📊

Quick Reference

ConceptDescriptionExample
module blockDefines a module call in root configmodule "name" { source = "./path" }
sourceLocal path to module folder"./modules/my_module"
variablesInputs passed to modulevariable1 = "value"
outputsValues exported from moduleoutput "name" { value = ... }
terraform initInitialize modules and providersterraform init

Key Takeaways

Use the module block with source set to the local folder path to call a local module.
Pass all required variables to the module to avoid errors.
Run terraform init after adding or changing modules to download and initialize them.
Keep module paths relative to the root configuration file.
Use outputs from modules to share information with the root configuration.