Terraform Module Sources: Local, Registry, and Git
Terraform modules are fundamental building blocks for Infrastructure as Code (IaC). They allow you to encapsulate and reuse infrastructure configurations. A crucial aspect of module development is understanding how to source and reference these modules. This section explores the three primary module sources: local paths, the Terraform Registry, and Git repositories.
1. Local Module Sources
Local module sources allow you to reference modules that are located within your local filesystem. This is particularly useful during module development and testing, as it enables rapid iteration without needing to publish your module to a registry or repository. You specify a relative or absolute path to the directory containing the module's
main.tf
Local modules are for development and internal use.
Use local sources to reference modules directly from your file system, ideal for rapid development and testing.
When using a local source, Terraform looks for the module in the specified directory relative to the calling module. For example, source = "./modules/vpc"
would reference a module located in a subdirectory named modules
within the current directory. This method is simple and effective for organizing your Terraform code within a single project or monorepo.
Rapid iteration and testing during module development.
2. Terraform Registry Module Sources
The Terraform Registry is a centralized repository for sharing and discovering Terraform modules. It hosts both public and private modules, making it easy to find and use pre-built infrastructure components. Modules from the registry are referenced using a specific format that includes the namespace, name, provider, and optionally a version constraint.
The Terraform Registry is the standard way to share and consume reusable infrastructure modules across teams and organizations.
The general format for referencing a registry module is
source = "/ / "
source = "hashicorp/aws/aws"
version = "~> 1.0"
Referencing a module from the Terraform Registry involves specifying its unique identifier. This identifier typically follows the pattern: namespace/name/provider
. For example, hashicorp/aws/aws
means the module is published by hashicorp
(namespace), is named aws
(name), and is for the aws
provider. Version constraints are crucial for managing dependencies and ensuring predictable deployments. Common version constraints include exact versions (= 1.2.3
), pessimistic constraints (~> 1.2
), or greater than/less than operators.
Text-based content
Library pages focus on text content
3. Git Module Sources
Git repositories are another common and powerful way to source Terraform modules. This method is ideal for private modules within your organization or for collaborating on open-source modules. Terraform can directly clone a Git repository and use the module within it. You can specify a branch, tag, or commit hash for precise version control.
The syntax for Git sources includes the repository URL and optional parameters like
ref
source = "git::https://github.com/hashicorp/terraform-aws-modules.git?ref=v3.0.0"
Source Type | Use Case | Versioning | Discovery |
---|---|---|---|
Local | Development, internal projects | Filesystem path | Manual discovery |
Registry | Public/Private sharing, collaboration | Version constraints (tags, semantic versioning) | Terraform Registry UI/CLI |
Git | Private repos, version control | Branch, tag, commit hash | Git repository browsing |
A commit hash.
Choosing the Right Module Source
The choice of module source depends on your project's needs and your organization's workflow. Local sources are best for initial development. The Terraform Registry is excellent for widely shared, version-controlled modules. Git sources provide flexibility for private repositories and fine-grained control over module versions.
Learning Resources
The official Terraform documentation detailing all available module source types and their usage.
The official Terraform Registry where you can discover and publish modules.
A popular collection of reusable Terraform modules for AWS, hosted on GitHub, demonstrating Git sourcing.
A blog post from HashiCorp discussing best practices for creating and using Terraform modules, including sourcing.
An article explaining the different Terraform module sources and when to use each.
A tutorial on creating your own Terraform modules, which covers local sourcing and testing.
A video that delves into advanced Terraform concepts, including module usage and sourcing strategies.
Specific documentation examples for using Git repositories as Terraform module sources.
Details on how to specify and manage versions for Terraform modules, crucial for registry and Git sources.
Information on the expected structure of modules published to the Terraform Registry.