CertLumen

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 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 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?

  1. AUse count = var.names
  2. BChange the argument to for_each = tolist(var.names)
  3. CUse for_each = toset(var.names)
  4. 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

← Previous objective · All 37 objectives · Next objective →