Question 1 · choose 1
A module has an input variable named env that must be one of dev, stage or prod. A wrong value should stop the run with the author's own error message, checked where the value enters the module. What should the module author add?
- AA type constraint of string on env, with default = "dev"
- BA validation block on env with a condition and an error_message
- CA local map keyed by dev, stage and prod, indexed with var.env
- DA terraform.tfvars file in the module that lists the allowed values
Show the answer and why
AA type constraint of string on env, with default = "dev"
Incorrect
A type constraint checks the kind of value, and a default only applies when no value is given; any other string still passes.
BA validation block on env with a condition and an error_message
Correct
A validation block checks the value a caller supplies against a condition and stops the operation with the error_message when the condition is false.
CA local map keyed by dev, stage and prod, indexed with var.env
Incorrect
An unknown key does fail, but with a generic invalid index error at the place where the map is used, not with the author's message.
DA terraform.tfvars file in the module that lists the allowed values
Incorrect
A .tfvars file assigns values to root module variables; it cannot restrict which values a variable accepts.
Validate inputs where they enter: a validation block on the variable, with a condition such as contains(["dev", "stage", "prod"], var.env) and a clear error_message.
HashiCorp documentation