HashiCorp Terraform Associate (004) · Section 4 · 24% of the exam
Refer to resource attributes and create cross-resource references
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 has resource "random_pet" "each" { for_each = var.names ... }. Which expression returns a list of every pet's id?
- A
random_pet.each[*].id, which splats over every instance - B
random_pet.each[0].id, which reads the first instance - C
random_pet.each.*.id.all - D
[for p in random_pet.each : p.id]
Show the answer
D. A resource that uses for_each is an object keyed by instance key. A for expression over it, or values(random_pet.each)[*].id, collects an attribute from every instance.
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
- Use and differentiate
resourceanddatablocks - 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