Module Sources and Versioning
What You'll Learn
You'll learn how to specify where Terraform should find modules and how to control which version of a module to use.
Understanding Module Sources
When you call a module using a module block, you must tell Terraform where to find it. The source argument accepts several types of locations:
Local Path
Point to a directory on your filesystem:
module "network" {
source = "./modules/vpc"
}
Terraform Registry
Reference public or private modules from the Terraform Registry:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.14.0"
}
Git Repository
Pull modules directly from version control:
module "app" {
source = "git::https://github.com/yourorg/terraform-modules.git//app"
}
You can also specify branches, tags, or commits:
source = "git::https://github.com/yourorg/modules.git//vpc?ref=v1.2.0"
Version Constraints
For registry modules, use the version argument to ensure stability:
module "database" {
source = "terraform-aws-modules/rds/aws"
version = "~> 5.0" # Any 5.x version
}
Think of versioning like pinning a recipe: you want the right version so your infrastructure "bakes" consistently every time.
Version constraints prevent unexpected breaking changes when module authors update their code. Without them, terraform init always pulls the latest version—which might introduce changes you haven't tested.
When to Use Each Source Type
- Local paths: Development and tightly-coupled modules
- Registry: Stable, community-vetted modules
- Git: Private organizational modules or specific commits
Key Takeaway: Module sources tell Terraform where to find reusable code, and version constraints ensure your infrastructure remains predictable and stable across deployments.