0
0
Terraformcloud~5 mins

Module sources (local, registry, git) in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to reuse code in Terraform projects stored in a local Git repository. This helps keep your infrastructure code organized and consistent without copying files around.
When you have multiple Terraform projects that share common modules stored in a local Git repository.
When you want to track changes in your modules using Git version control locally.
When you want to test module changes locally before pushing them to a remote Git server.
When you want to avoid duplicating module code by referencing a single source in Git.
When you want to use specific versions or branches of your modules stored in Git.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

module "example_module" {
  source = "git::file:///home/user/terraform-modules/example-module.git"
  # Add module input variables here
  example_variable = "Hello from local Git module"
}

This Terraform configuration uses a module stored in a local Git repository.

terraform block sets the required Terraform version.

module "example_module" block declares the module usage.

source uses the git::file:// prefix to point to the local Git repository path.

You can pass variables to the module as usual.

Commands
This command initializes the Terraform working directory. It downloads the module from the local Git repository and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
This command shows what Terraform will do based on the module code and variables. It helps verify the module is loaded and working.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions are needed.
Key Concept

If you remember nothing else from this pattern, remember: use the git::file:// prefix in the module source to load modules from a local Git repository.

Common Mistakes
Using a local file path without the git::file:// prefix in the module source.
Terraform treats it as a normal file path and does not use Git features like versioning or branches.
Always prefix the local Git repository path with git::file:// to tell Terraform to use Git.
Using a relative path without the full absolute path for the local Git repository.
Terraform may not find the repository correctly if the path is relative and the working directory changes.
Use the full absolute path starting with git::file:/// for local Git module sources.
Summary
Declare modules with source using git::file:/// followed by the local Git repo path.
Run terraform init to download and prepare the module from the local Git repository.
Use terraform plan to verify the module loads and shows planned infrastructure changes.