You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`.
17
21
18
22
Terraform uses this during the module installation step of `terraform init`
19
23
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
485
489
the module from the subdirectory. As a result, it is safe for a module in
486
490
a sub-directory of a package to use [a local path](#local-paths) to another
487
491
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:
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