TF-004 Read, generate and modify configuration Practice Question
Which four of the following are valid techniques for reading, generating, or modifying Terraform configuration? (Choose all that apply. There are four correct answers.)
⚠ Common exam trap
HashiCorp often tests the distinction between commands that modify configuration versus those that only validate or inspect state, so candidates may incorrectly select `terraform validate` or `terraform console` as techniques for generating or modifying configuration when they are purely diagnostic or interactive tools.
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
✓
Using the `templatefile` function to render a template with variables from the current configuration.
The `templatefile` function is a valid technique for reading and generating configuration because it reads an external template file and renders it with variables from the current Terraform configuration, producing a string that can be used in resource arguments or other expressions. This is a core feature for dynamic configuration generation, such as injecting user data into cloud-init scripts or generating complex configuration files for provisioned resources.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Using the `templatefile` function to render a template with variables from the current configuration.
Why this is correct
The `templatefile` function is a built-in Terraform function designed to render a template file (e.g., a shell script, configuration file, or even a part of HCL) by injecting variables from the current configuration. This directly enables the dynamic generation of text-based content, allowing Terraform to create or modify external files or strings based on computed values and variables within the infrastructure code.
- ✗
Using `terraform console` to evaluate expressions and generate valid HCL configuration output.
Why it's wrong here
The `terraform console` command provides an interactive shell for evaluating Terraform expressions and testing logic in real-time. While it is excellent for debugging and exploring configuration values, it is not designed to *generate* valid HCL configuration output that can be directly saved into a `.tf` file for deployment. Its purpose is interactive evaluation, not programmatic configuration generation.
- ✗
Using `terraform state pull` to retrieve remote state and then using `terraform state mv` to rename a resource address in the local state file before pushing it back.
Why it's wrong here
`terraform state pull` retrieves a copy of the remote state to the local filesystem, but `terraform state mv` is intended to operate directly on the *configured backend state* or a local state file, not on a temporarily pulled copy. Attempting to modify a locally pulled state file with `terraform state mv` and then pushing it back is an unsupported and highly error-prone workflow. Resource address changes should be managed directly via `terraform state mv` against the backend or using `moved` blocks in configuration.
- ✓
Using a `data` source with a `for_each` and a `local` value to dynamically generate resource blocks based on a variable input.
Why this is correct
This technique leverages `data` sources to fetch external information, `local` values to transform or prepare that data, and then applies `for_each` to `resource` blocks. This powerful combination enables the dynamic generation of multiple resource instances based on variable inputs or fetched data, effectively "generating" configuration blocks programmatically. It allows for highly flexible and scalable infrastructure definitions by creating resources conditionally and iteratively.
- ✓
Using the `jsonencode` function within a `local` value to programmatically construct a JSON string that is then used as part of a resource argument.
Why this is correct
The `jsonencode` function converts a Terraform value (like a map or list) into a JSON formatted string. When used within a `local` value, it allows for the programmatic construction of complex JSON payloads from structured HCL data. This generated JSON string can then be passed as an argument to a resource that expects JSON input, such as IAM policies or cloud configuration settings, directly "generating" specific content required by a resource.
- ✓
Using `terraform validate` to check configuration syntax and internal consistency before applying changes.
Why this is correct
`terraform validate` is a command that performs checks on the configuration files in the current directory. It verifies syntax correctness, argument types, and internal consistency, ensuring that the configuration is structurally sound and refers to valid elements. While it does not generate new configuration, it "reads" the existing configuration to identify necessary modifications or corrections, making it an essential tool for ensuring configuration validity before deployment.
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.