TF-004 Read, generate and modify configuration Practice Question
A developer wants to conditionally create a resource based on a variable that is a boolean. Which syntax should they use?
⚠ Common exam trap
HashiCorp often tests the distinction between `count` and `for_each` for conditional creation, and the trap here is that candidates mistakenly think `count` can accept a boolean directly or that `for_each` with a single-element list is the correct approach for a simple boolean condition.
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
✓
Use 'count = var.create ? 1 : 0'
In Terraform, the `count` meta-argument accepts a number, and the ternary expression `var.create ? 1 : 0` evaluates to 1 (true) to create one instance of the resource or 0 (false) to create none. This is the standard pattern for conditionally creating a single resource based on a boolean variable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use 'if var.create' inside the resource block
Why it's wrong here
Terraform's HashiCorp Configuration Language (HCL) does not support `if` statements as a construct to conditionally define or wrap entire resource blocks. While `if` expressions can be used for values, attempting to place `if var.create` directly within or around a `resource` block will result in a parsing error. Resource lifecycle management is exclusively handled by meta-arguments like `count` or `for_each`, not imperative conditional logic at the block level.
- ✗
Use 'for_each = var.create ? [1] : []'
Why it's wrong here
The `for_each` meta-argument specifically requires a map or a set of strings as its input value to iterate over. The expression `var.create ? [1] : []` evaluates to either a list containing the integer `1` or an empty list. Neither of these types—a list of integers or an empty list—satisfies the strict type requirements for `for_each`, which expects unique string keys for resource instance identification. This would lead to a type constraint error during Terraform validation.
- ✗
Use 'count = var.create'
Why it's wrong here
The `count` meta-argument in Terraform strictly expects a non-negative integer value to determine the number of resource instances to create. A boolean variable `var.create` evaluates to `true` or `false`, which are not numeric types. Terraform does not perform implicit type conversion from boolean to integer for `count`, meaning this assignment would result in a type mismatch error during configuration parsing and validation. An explicit conversion is required.
- ✓
Use 'count = var.create ? 1 : 0'
Why this is correct
This is the correct and idiomatic pattern for conditionally creating a single resource in Terraform. The ternary operator `var.create ? 1 : 0` explicitly converts the boolean value of `var.create` into the required integer `0` or `1`. If `var.create` is `true`, `count` becomes `1`, ensuring the resource is created. If `var.create` is `false`, `count` becomes `0`, preventing the resource from being created or causing its destruction if it already exists.
Go deeper
Related to this question
About these practice questions
This TF-004 question is part of Courseiva's 428-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.