A senior engineer is writing a reusable module that accepts a variable with complex type constraints and wants to ensure that no caller accidentally passes an empty list of subnet IDs. They write:
variable "subnet_ids" {
type = list(string)
description = "List of subnet IDs for the deployment"
validation {
condition = length(var.subnet_ids) > 0
error_message = "At least one subnet ID must be provided."
}
}
variable "instance_config" {
type = object({
instance_type = string
spot_price = optional(number, null)
ebs_optimized = optional(bool, true)
})
}
The caller passes:
subnet_ids = []
instance_config = { instance_type = "t3.medium" }
Which TWO statements correctly describe the outcome of running terraform plan with this configuration?
- A. Terraform raises a validation error on subnet_ids because the list is empty, displaying "At least one subnet ID must be provided."✓ Correct
- B. Terraform merges the partial instance_config object with the module's local defaults automatically
- C. Terraform raises an error because the optional() modifier is not valid inside object() type constraints
- D. The validation block on subnet_ids is skipped because the variable has no default value
- E. The instance_config variable is accepted, with spot_price defaulting to null and ebs_optimized defaulting to true✓ Correct
- F. Terraform raises a type error on instance_config because spot_price and ebs_optimized are not supplied
Explanation
Validation error on subnet_ids is correct — passing an empty list [] causes length(var.subnet_ids) > 0 to evaluate to false, triggering the custom error_message. Type error on instance_config is wrong — the optional() modifier in object() type constraints (introduced in Terraform 1.3) explicitly allows callers to omit those attributes; omitting them does not cause an error. Defaults applied for instance_config is correct — optional(number, null) defaults spot_price to null and optional(bool, true) defaults ebs_optimized to true when not supplied by the caller. optional() is invalid is wrong — optional() is a fully supported built-in modifier for object type constraints since Terraform 1.3. Validation skipped without default is wrong — validation blocks run whenever a value is provided, regardless of whether the variable has a default; here a value ([]) is explicitly passed. Merges with local defaults is wrong — Terraform's optional() modifier provides per-attribute defaults at the type level; there is no automatic merging with module-level locals.