Table of contents
Open Table of contents
Inheritance
There is no implicit inheritance. Things do not inherit from each other. In essence, imagine a bunch of blocks and those blocks can only communicate but attaching them to each other.
Module Definition
Consider the following snippet”
module "this" {
selfdefined = "hi"
enabled = var.enabled
...
}
We have a defined a variable here; selfdefined. Defined locally within the module.
But we can also define it separately enabled in a variable file as is convention. The variables.tf using the keyword variable adds variables to the variable namespace which is scoped to the module the file belongs to.
Variable Resolution Order
- -var CLI flags
terraform apply -var="enabled=false"
terraform apply -var-file=prod.tfvars
- Auto-loaded tfvars
- terraform.tfvars <- only applicable to the directory that terraform is running in (working directory)
- *.auto.tfvars
- Environmental variables
export TF_VAR_enabled=false
- Default value In the terraform code; if not present, an error will be thrown
Module Intercommunication
The major thing to understand is that implicit sharing is impossible. A child modules can NEVER access a variable, resource, or state from its parents or sibling modules without explicitly beging passed.
That means
-
Variables MUST flow downwards. Declare a variable at the root module and even child must pass it to the next child to propagate it to the bottom.
-
Outputs MUST flow downwards. A child module can expose values via output. Only the direct parent can access these output values
-
Sibling modules can never communicate without a parent module that calls both of them.