HashiCorp Terraform Associate (004) · Section 4 · 24% of the exam
Use and differentiate resource and data blocks
One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Terraform configuration”. Here is what it covers, the points to remember and a question to try.
What it covers
A resource block tells Terraform to create and manage an object; a data block only reads something that already exists. This lesson covers block syntax, resource addresses, meta-arguments, how a reference to another object creates an implicit dependency, and the attribute types you can refer to.
Points to remember
- A
resourceblock is managed (create, update, replace, destroy); adatablock is read-only. - Resource address is
TYPE.NAME; data address isdata.TYPE.NAME; both are unique in a module. - Reference attributes with
TYPE.NAME.ATTRIBUTE; computed attributes are known only after apply. - A reference creates an implicit dependency and an edge in the dependency graph; unrelated resources run in parallel.
countmakes indexed instances ([0]);for_eachmakes keyed instances (["key"]) and is safer when elements are added or removed.
Try a question
A module receives variable "names" { type = list(string) }. An engineer writes for_each = var.names on a resource and terraform validate fails. What is the correct fix?
- AUse
count = var.names - BChange the argument to
for_each = tolist(var.names) - CUse
for_each = toset(var.names) - DAdd
depends_on = [var.names]
Show the answer
C. for_each requires a map or a set of strings. A list of strings must first be converted to a set with toset(), which also removes duplicates.
Written for practice in the style of the exam: never a question from the real exam.
In the course
The lesson Resource and data blocks, and references between them teaches this objective (about 40 minutes), and 19 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.
More in this section
- Refer to resource attributes and create cross-resource references
- Use variables and outputs
- Understand and use complex types
- Write dynamic configuration using expressions and functions
- Define resource dependencies in configuration
- Validate configuration using custom conditions
- Understand best practices for managing sensitive data, including secrets management with Vault