Bitzu

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

Single choice

Which two arguments must a validation block contain?

  1. condition and error_message
  2. rule and message
  3. expression and severity
  4. 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.

Select all that apply

Which statements about variable validation are true?

  1. It runs before Terraform generates the plan
  2. A failure shows error_message and stops the operation
  3. It runs only after apply finishes
  4. It queries the cloud API to confirm the value exists
  5. 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.

True or false

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.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module