Terraform Associate 004 ยท Dependencies, validation and sensitive data
Variable validation blocks
validation condition and error_message inside a variable, typical checks, when it runs (4g).
Key points
- A validation block inside a variable has a condition (true means valid) and an error_message shown when it is false.
- Terraform checks it immediately, before generating a plan, and stops the operation on failure.
- Typical checks: allowed values with contains(), length limits, and patterns with can(regex(...)).
Source: https://developer.hashicorp.com/terraform/language/expressions/custom-conditions
Practice questions
Which two arguments must a validation block contain?
- condition and error_message
- rule and message
- expression and severity
- assert and level
Show answer
Answer: A. condition and error_message
condition is the test that must be true and error_message explains the failure to the user.
Which statements about variable validation are true?
- It runs before Terraform generates the plan
- A failure shows error_message and stops the operation
- It runs only after apply finishes
- It queries the cloud API to confirm the value exists
- A failure only produces a warning
Show answer
Answer: A. It runs before Terraform generates the plan, B. A failure shows error_message and stops the operation
Validation runs before the plan is generated and stops the operation with the error_message on failure. It does not wait for apply or query real infrastructure.
Variable validation blocks are evaluated only during terraform apply.
Show answer
Answer: False
False. They are evaluated immediately, before a plan is generated, so bad input is caught early.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
