TERRAFORM-ASSOCIATE-004 · Question #267
You have defined the values for your variables in the file terraform.tfvars, and saved it in the same directory as your Terraform configuration. Which of the following commands will use those values w
The correct answer is C. All of the above. Terraform automatically loads variables from terraform.tfvars in the working directory, and also supports explicit loading via the -var-file parameter.
Question
Options
- Aterraform plan
- Bterraform plan -var-file=terraform.tfvars
- CAll of the above
- DNone of the above
How the community answered
(41 responses)- B2% (1)
- C93% (38)
- D5% (2)
Why each option
Terraform automatically loads variables from `terraform.tfvars` in the working directory, and also supports explicit loading via the `-var-file` parameter.
While `terraform plan` alone does work due to automatic loading, it is not the only correct option as explicitly using `-var-file` also works.
While `terraform plan -var-file=terraform.tfvars` works, it is not the only correct option as `terraform plan` alone also works due to automatic loading.
Terraform automatically discovers and loads variable definitions from files named `terraform.tfvars` or `*.auto.tfvars` present in the current working directory. Additionally, the `-var-file` parameter allows explicit specification of a variable definition file, overriding or merging with other variable sources. Since `terraform.tfvars` is automatically loaded, both `terraform plan` and `terraform plan -var-file=terraform.tfvars` will successfully use the defined values.
Both option A and option B are valid methods for Terraform to load variables from `terraform.tfvars`, making this choice incorrect.
Concept tested: Terraform variable loading precedence
Source: https://developer.hashicorp.com/terraform/language/values/variables#variable-definition-files
Topics
Community Discussion
No community discussion yet for this question.