0
0
Terraformcloud~10 mins

Default values in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Default values
Start variable declaration
Check if input value provided?
NoUse default value
Yes
Use provided input value
Variable value set
End
Terraform variables check if a user input is given; if not, they use the default value specified.
Execution Sample
Terraform
variable "region" {
  type    = string
  default = "us-west-1"
}

output "selected_region" {
  value = var.region
}
Defines a variable with a default region; outputs the selected region value.
Process Table
StepInput Provided?Variable Value SetOutput Value
1No"us-west-1" (default)"us-west-1"
2Yes (e.g., "eu-central-1")"eu-central-1""eu-central-1"
💡 Execution ends after variable value is set and output is produced.
Status Tracker
VariableStartAfter No InputAfter Input Provided
var.regionundefined"us-west-1""eu-central-1"
output.selected_regionundefined"us-west-1""eu-central-1"
Key Moments - 2 Insights
What happens if no value is provided for the variable?
Terraform uses the default value specified in the variable block, as shown in execution_table row 1.
Can the default value be overridden?
Yes, if a value is provided during terraform apply or in a tfvars file, it overrides the default, as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the variable value when no input is provided?
Aundefined
B"eu-central-1"
C"us-west-1"
Dnull
💡 Hint
Check execution_table row 1 under 'Variable Value Set'
At which step does the variable value get overridden by user input?
AStep 2
BStep 1
CNever
DBefore Step 1
💡 Hint
See execution_table row 2 where input is provided
If the default value is removed, what happens when no input is provided?
ATerraform uses null as value
BTerraform throws an error
CTerraform uses an empty string
DTerraform uses a random value
💡 Hint
Default values prevent errors when no input is given; removing default causes error if no input
Concept Snapshot
Terraform variables can have default values.
If no input is given, Terraform uses the default.
User input overrides the default value.
Defaults prevent errors from missing inputs.
Use 'default = value' inside variable blocks.
Full Transcript
In Terraform, variables can have default values. When Terraform runs, it checks if the user provided a value for the variable. If not, it uses the default value specified. If the user provides a value, that value overrides the default. This behavior ensures Terraform has a value to use and avoids errors from missing inputs. The example shows a variable 'region' with a default of 'us-west-1'. If no input is given, Terraform uses 'us-west-1'. If the user provides 'eu-central-1', Terraform uses that instead. Removing the default without providing input causes an error.