HashiCorp Terraform Associate (004) · Section 4 · 24% of the exam
Use variables and outputs
One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Terraform configuration”. Here is what it covers, the points to remember and a question to try.
What it covers
Input variables parameterize a module, locals name intermediate values, and outputs publish results. This lesson covers variable arguments, validation, how Terraform picks a value when several sources set the same variable, sensitive and ephemeral values, and how outputs are read.
Points to remember
- Variables are inputs (
var.x), locals are private names (local.x), outputs are results. - A variable with no
defaultis required; the default must be a literal. - Precedence low to high: default,
TF_VAR_,terraform.tfvars,terraform.tfvars.json,*.auto.tfvars, then-var/-var-file. validationblocks need a booleanconditionand anerror_message; since 1.9 they can refer to other objects.sensitive = trueredacts output but the value is still in state;ephemeral = true(1.10+) keeps it out of plan and state.
Try a question
An engineer has a variable that must be a TCP port between 1024 and 65535, and wants a clear message at plan time. Which construct is designed for this?
- AA
lifecycleblock with a custom condition on every resource that uses the port - BA
checkblock that runs after apply and reports the port problem as a warning - C
nullable = falseon the variable so that a null port is rejected for the range - DA
validationblock with aconditionin the variable
Show the answer
D. A variable validation block evaluates a boolean condition on the input while planning and returns your error message when it is false.
Written for practice in the style of the exam: never a question from the real exam.
In the course
The lesson Input variables, locals and outputs teaches this objective (about 40 minutes), and 18 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.
More in this section
- Use and differentiate
resourceanddatablocks - Refer to resource attributes and create cross-resource references
- Understand and use complex types
- Write dynamic configuration using expressions and functions
- Define resource dependencies in configuration
- Validate configuration using custom conditions
- Understand best practices for managing sensitive data, including secrets management with Vault