CertLumen

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 resource block is managed (create, update, replace, destroy); a data block is read-only.
  • Resource address is TYPE.NAME; data address is data.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.
  • count makes indexed instances ([0]); for_each makes 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?

  1. Arandom_pet.each[*].id, which splats over every instance
  2. Brandom_pet.each[0].id, which reads the first instance
  3. Crandom_pet.each.*.id.all
  4. 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

← Previous objective · All 37 objectives · Next objective →