Skip to content

Commit 3cc1b99

Browse files
committed
Extend module docs with dynamic sources information
1 parent 926e31a commit 3cc1b99

File tree

1 file changed

+69
-1
lines changed
  • content/terraform/v1.15.x (beta)/docs/language/modules

1 file changed

+69
-1
lines changed

content/terraform/v1.15.x (beta)/docs/language/modules/sources.mdx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ last_modified: 2025-11-19T19:11:19Z
1313
# Module Sources
1414

1515
The `source` argument in [a `module` block](/terraform/language/modules/syntax)
16-
tells Terraform where to find the source code for the desired child module.
16+
is an expression that tells Terraform where to find the source code for the
17+
desired child module.
18+
19+
The `source` expression can use references to input variables and local values.
20+
Any input variable referenced in `source` must declare `const = true`.
1721

1822
Terraform uses this during the module installation step of `terraform init`
1923
to download the source code to a directory on local disk so that other Terraform commands can use it.
@@ -485,3 +489,67 @@ Terraform will still extract the entire package to local disk, but will read
485489
the module from the subdirectory. As a result, it is safe for a module in
486490
a sub-directory of a package to use [a local path](#local-paths) to another
487491
module as long as it is in the _same_ package.
492+
493+
## Source Expressions
494+
495+
A module `source` can be an expression, which allows you to build a source address from constant input variables and local values.
496+
497+
When a `source` expression refers to an input variable, that variable must set `const = true`. Terraform requires this so it can fully determine the module source during `terraform init`.
498+
499+
For example, you can select a module source using a constant variable:
500+
501+
```hcl
502+
module "consul" {
503+
source = var.module_source
504+
}
505+
```
506+
507+
```hcl
508+
variable "module_source" {
509+
type = string
510+
const = true
511+
}
512+
```
513+
514+
You can also compose a source from constant variables and local values:
515+
516+
```hcl
517+
module "vpc" {
518+
source = local.vpc_source
519+
}
520+
```
521+
522+
```hcl
523+
variable "module_repo" {
524+
type = string
525+
const = true
526+
}
527+
528+
variable "module_ref" {
529+
type = string
530+
const = true
531+
}
532+
533+
locals {
534+
vpc_source = "git::https://example.com/${var.module_repo}.git?ref=${var.module_ref}"
535+
}
536+
```
537+
538+
The `version` argument also accepts expressions. If a `version` expression
539+
refers to an input variable, that variable must also set `const = true`.
540+
541+
```hcl
542+
module "consul" {
543+
source = "hashicorp/consul/aws"
544+
version = var.consul_version
545+
}
546+
```
547+
548+
```hcl
549+
variable "consul_version" {
550+
type = string
551+
const = true
552+
}
553+
```
554+
555+
The `source` and `version` expressions can only reference things that are known during configuration loading. Terraform will show an error if they reference something that is only known after a plan.

0 commit comments

Comments
 (0)