Terraform modules can be sourced from various locations, categorized as either local or remote sources. Understanding these sources is essential for organizing and reusing infrastructure code effectively.
**Local Module Sources**
Local modules are stored on the same filesystem where Terraform is …Terraform modules can be sourced from various locations, categorized as either local or remote sources. Understanding these sources is essential for organizing and reusing infrastructure code effectively.
**Local Module Sources**
Local modules are stored on the same filesystem where Terraform is executed. They are referenced using relative or absolute file paths. For example:
hcl
module "vpc" {
source = "./modules/vpc"
}
Local modules are ideal for development, testing, and when modules are specific to a single project. They offer fast access since no network requests are required, and changes are reflected instantly. However, sharing local modules across teams requires manual file distribution.
**Remote Module Sources**
Remote modules are hosted externally and downloaded by Terraform during initialization. Common remote sources include:
1. **Terraform Registry** - The public registry or private registries:
hcl
module "vpc" {
source = "hashicorp/vpc/aws"
version = "3.0.0"
}
2. **GitHub/Bitbucket** - Version control repositories:
hcl
module "vpc" {
source = "github.com/example/terraform-modules//vpc"
}
3. **S3 Buckets** - AWS storage:
hcl
module "vpc" {
source = "s3::https://bucket-name.s3.amazonaws.com/modules/vpc.zip"
}
4. **HTTP URLs** - Generic web locations:
hcl
module "vpc" {
source = "https://example.com/modules/vpc.zip"
}
Remote modules enable team collaboration, version control, and centralized module management. The `terraform init` command downloads remote modules to the `.terraform/modules` directory.
**Key Considerations**
- Remote modules support versioning for stability
- Local modules are best for rapid iteration
- Remote sources require network connectivity
- Both types use the same module block syntax
- Mixing local and remote modules in a single configuration is fully supported
Choosing between local and remote sources depends on your workflow, team size, and module reusability requirements.
Local and Remote Module Sources in Terraform
Why This Topic Is Important
Understanding local and remote module sources is fundamental to the Terraform Associate exam. Modules are the building blocks of reusable Terraform configurations, and knowing how to source them correctly enables you to write maintainable, scalable infrastructure code. This topic typically appears in multiple exam questions and is essential for real-world Terraform usage.
What Are Module Sources?
Module sources define where Terraform should look to find the module code you want to use. When you call a module in your configuration, you must specify a source argument that tells Terraform the location of that module's files.
There are two main categories:
1. Local Module Sources Local modules are stored on the same filesystem as your root configuration. They use relative or absolute file paths.
Key characteristics of local modules: - Path must start with ./ or ../ - No download step required - Changes are reflected when you run terraform init or terraform apply - Ideal for organization-specific modules within the same repository
2. Remote Module Sources Remote modules are stored externally and must be downloaded before use. Terraform supports multiple remote source types:
Terraform Registry (Public and Private): module "vpc" { source = "hashicorp/consul/aws" version = "0.1.0"}
1. Terraform parses your configuration files 2. For each module block, it reads the source argument 3. Local modules are referenced from the filesystem 4. Remote modules are downloaded to the .terraform/modules directory 5. A .terraform/modules/modules.json file tracks downloaded modules
Version Constraints
Version constraints work differently based on source type:
- Terraform Registry: Supports the version argument - Git sources: Use ref argument for branches, tags, or commits - Local modules: Do not support versioning
Example with Git ref: module "vpc" { source = "git::https://example.com/vpc.git?ref=v1.2.0"}
Subdirectories in Remote Sources
You can reference subdirectories using double slashes: module "vpc" { source = "git::https://example.com/modules.git//vpc"}
The double slash separates the repository URL from the subdirectory path.
Exam Tips: Answering Questions on Local and Remote Module Sources
Tip 1: Remember the path prefixes Local modules MUST start with ./ or ../. If you see a path that looks like modules/vpc, this would be interpreted as a Terraform Registry address, not a local path.
Tip 2: Know when terraform init is required You must run terraform init after adding a new module or changing a module source. This downloads remote modules and updates the module cache.
Tip 3: Understand version vs ref The version argument only works with Terraform Registry modules. For Git-based sources, use the ref query parameter in the source URL.
Tip 5: Downloaded module location Remote modules are stored in .terraform/modules/ after running terraform init.
Tip 6: Double slash for subdirectories When a question mentions accessing a specific folder within a repository, look for the // syntax in the answer options.
Tip 7: Private registry format Private registry modules use the format: hostname/namespace/name/provider
Tip 8: Local modules and versioning If asked about versioning local modules, remember that local modules do not support the version argument. Version control must be managed through your VCS system.