Terraform module sources and versions support variables and locals
Introduced 2026-04-29 · score 0.96
HOW TO USE IT
When selecting a module source or registry version from configuration values, use input variables marked `const = true`; you can reference them directly or through locals. Provide their values with a supported variable mechanism, then run `terraform init` after changing the module source or version.
variable "module_source" {
type = string
const = true
}
module "example" {
source = var.module_source
}
terraform plan -var='module_source=./<module-directory>'variable "module_repo" {
type = string
const = true
}
variable "module_ref" {
type = string
const = true
}
locals {
module_source = "git::https://example.com/${var.module_repo}.git?ref=${var.module_ref}"
}
module "example" {
source = local.module_source
}
terraform plan -var='module_repo=<repository>' -var='module_ref=<branch-or-tag>'variable "module_version" {
type = string
const = true
}
module "example" {
source = "<registry-namespace>/<module>/<provider>"
version = var.module_version
}
terraform plan -var='module_version=<version-constraint>'WHY THIS HELPS
Select a module source or version using a configuration value.
REQUIREMENTS AND LIMITATIONS
- Version: 1.15.0
- const: Set to `true` for any input variable referenced by a module `source` or `version`; its value must be known during configuration loading.
- source (required): Module source expression; it can reference constant input variables and local values.
- version: Registry module version constraint; it can reference constant input variables and local values. Use only with a registry source.
- -var 'NAME=VALUE': Set one root input variable value; the option can be repeated.
- -var-file=FILENAME: Set root input variable values from a `.tfvars` file; the option can be repeated.
- TF_VAR_name: Environment variable form for setting an input variable; replace `name` with the variable name.
- Prerequisite: Terraform v1.15.0 introduced `const` variables for module source/version expressions.
- Prerequisite: Run `terraform init` after modifying a module `source` or `version`.
- Nested modules can use variables in module sources only when their input variables are declared `const = true`.
- The release text states that variables and locals are supported in module source and version attributes, but the linked description examples only show `source`.
FIRST-PARTY EVIDENCE
- https://github.com/hashicorp/terraform/releases/tag/v1.15.0
- https://github.com/hashicorp/terraform/pull/38217
- https://developer.hashicorp.com/terraform/language/modules/configuration
- https://developer.hashicorp.com/terraform/cli/commands/plan
- https://developer.hashicorp.com/terraform/cli/config/environment-variables
- https://developer.hashicorp.com/terraform/language/block/module?product_intent=terraform
Check the installed version and the source before using it. Entry JSON