0
0
Terraformcloud~10 mins

Module inputs (variables) in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Module inputs (variables)
Define variable in module
Call module with input values
Terraform reads input values
Module uses inputs to create resources
Outputs reflect resource state based on inputs
Terraform modules receive input values through variables defined inside the module and passed when calling the module. These inputs control resource creation.
Execution Sample
Terraform
variable "region" {
  type    = string
  default = "us-east-1"
}

module "example" {
  source = "./modules/example"
  region = "us-west-2"
}
Defines a variable 'region' with default, then calls a module passing a different region value.
Process Table
StepActionVariableValueEffect
1Define variable 'region' with defaultregionus-east-1Variable ready with default
2Call module 'example' passing regionregionus-west-2Input overrides default
3Terraform reads module inputregionus-west-2Module uses passed value
4Module creates resources using regionregionus-west-2Resources created in us-west-2
5Module outputs reflect resource stateregionus-west-2Outputs show resources in us-west-2
6Execution ends--All inputs processed, resources created
💡 Module inputs processed; resources created using passed variable values
Status Tracker
VariableDefinedPassed to ModuleUsed in ModuleFinal
regionus-east-1 (default)us-west-2us-west-2us-west-2
Key Moments - 2 Insights
Why does the module use 'us-west-2' instead of the default 'us-east-1'?
Because the module call passes 'region = "us-west-2"', which overrides the variable's default value as shown in execution_table step 2 and 3.
What happens if no value is passed to the module for 'region'?
Terraform uses the variable's default value 'us-east-1' inside the module, as defined in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what value does the module variable 'region' have?
Aus-east-1
Bundefined
Cus-west-2
Dnull
💡 Hint
Check the 'Value' column at step 3 in the execution_table
At which step does Terraform read the input value passed to the module?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where Terraform reads module input in the execution_table
If the module call did not pass any value for 'region', what would be the final value used inside the module?
Anull
Bus-east-1
Cus-west-2
Dempty string
💡 Hint
Refer to variable_tracker and key_moments about default values
Concept Snapshot
Terraform modules use variables to accept inputs.
Variables have defaults but can be overridden when calling the module.
Passed values replace defaults inside the module.
Modules use these inputs to create resources.
Outputs reflect the resource state based on inputs.
Full Transcript
In Terraform, modules can accept inputs through variables defined inside them. These variables can have default values. When you call a module, you can pass values to these variables, which override the defaults. Terraform reads these input values during execution and uses them to create resources inside the module. The outputs from the module then reflect the state of these resources based on the inputs provided. This process allows modules to be reusable and configurable by changing input values without modifying the module code.