TF-Associate-004 - Terraform configuration - Section 4a

Use and differentiate resource and data blocks.

A resource block creates and manages an object; a data block reads existing information without managing it. Candidates should choose the correct block for a stated need and understand that a data source is refreshed during plan and produces read-only attributes.

resource blockdata sourcemanaged versus read-onlydata source refresh during plan

Practice question for this objective

Free sampleTerraform configurationmedium

An engineer wants to attach a firewall rule to an existing virtual network that another team manages in the same cloud account, without Terraform taking ownership of that network or being able to delete it. How should the engineer reference the network in this configuration?

# The virtual network already exists and is owned by another team.
# This configuration must read its id but never manage it.
  • ADeclare the network with a resource block and set a lifecycle prevent_destroy flag so Terraform cannot delete it.
  • BImport the network into state with a resource block, which lets Terraform read it while leaving it untouched.
  • CUse a data block for the network and reference its exported id from the firewall rule resource. Correct
  • DHardcode the network id as a string variable, because Terraform cannot reference objects it does not create.
Reference infrastructure managed elsewhere through a data block, which reads attributes without owning the object's lifecycle. Data sources let a configuration consume attributes of objects it does not manage, so Terraform reads the network's id at plan time but never creates, updates, or destroys it.

Why A is wrong: A resource block would try to create and own the network, and prevent_destroy only blocks deletion, so Terraform would still attempt to manage it.

Why B is wrong: Importing brings the object under full management by a resource block, which is the opposite of the read-only, unmanaged relationship required here.

Why C is correct: A data block reads an existing object without managing its lifecycle, exposing attributes such as the id for other resources to consume.

Why D is wrong: Terraform can reference external objects through data sources, so hardcoding is unnecessary and forgoes validation that the network actually exists.

See more TF-Associate-004 practice questions, answers explained.

More in this domain

Back to all Terraform configuration objectives, or the TF-Associate-004 cert hub.

Examworthy is not affiliated with or endorsed by HashiCorp. Original, blueprint-aligned practice material only.