TF-004 Read, generate and modify configuration Practice Question
Which THREE variable declarations are valid in Terraform?
⚠ Common exam trap
Terraform often tests the strict type-default compatibility rule, where candidates mistakenly assume Terraform will implicitly convert a string to a number or a single value to a list, but Terraform requires exact type matching for defaults.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
variable "enabled" { type = bool default = true }
Terraform variable declarations require a `type` argument and optionally a `default` value. Here, `type = bool` is a valid primitive type, and `default = true` provides a boolean default, which matches the type constraint. This syntax follows Terraform's variable block specification, where the default value must be compatible with the declared type.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
variable "enabled" { type = bool default = true }
Why this is correct
This variable declaration is valid because the `type` constraint is correctly set to `bool`, and the `default` value provided, `true`, is a literal boolean value. Terraform successfully validates that the default value's type precisely matches the explicitly declared `bool` type, ensuring strict type consistency for the variable. This configuration allows the variable to accept only `true` or `false` values.
- ✓
variable "tags" { type = map(string) default = {} }
Why this is correct
This variable declaration is valid as it correctly specifies `map(string)` as its type, indicating a map where all values must be strings. The provided `default` value, `{}`, is an empty map literal, which perfectly conforms to the `map(string)` type constraint. Terraform allows an empty map as a valid default for any map type, as it contains no elements that would violate the value type constraint.
- ✓
variable "region" { type = string default = "us-east-1" }
Why this is correct
This declaration is valid because the `type` is explicitly set to `string`, and the `default` value `"us-east-1"` is a literal string. Terraform successfully validates that the default value's type aligns perfectly with the declared `string` type constraint. This ensures that if no value is explicitly provided for `region` during execution, it will default to the specified string, maintaining type safety throughout the configuration.
- ✗
variable "names" { type = list(string) default = "name" }
Why it's wrong here
This variable declaration is invalid because of a fundamental type mismatch between the declared `type` and the `default` value. The `type` is specified as `list(string)`, which expects a collection of strings, such as `["name1", "name2"]`. However, the `default` value provided, `"name"`, is a single string literal, not a list. Terraform will raise a type conversion error, as a string cannot be implicitly converted to a list of strings.
- ✗
variable "count" { type = number default = "1" }
Why it's wrong here
This variable declaration is invalid due to a critical type mismatch. The `type` is explicitly declared as `number`, which expects an integer or float value without quotes, like `1` or `1.0`. However, the `default` value provided, `"1"`, is a string literal because it is enclosed in double quotes. Terraform will fail to validate this, as a string cannot be automatically coerced into a number type for a default value, leading to a configuration error.
Go deeper
Related to this question
About these practice questions
Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.