Resources versus data sources
Choose between managing and looking up
A resource declaration asks Terraform to track a managed object and its lifecycle. A data source obtains information for expressions without taking ownership of the looked-up object. Suppose another team manages a shared network while your team manages a service. You can look up the network and supply its identifier when declaring the service. This does not transfer management of the network to your team.
Read a managed resource address
This complete provider-free local fixture uses the built-in Terraform provider. It does not create external infrastructure. Use a fresh directory, the inherited provisional CLI version and a reviewed binary; results below are predictions, not executed tests.
resource "terraform_data" "catalog" { input = "catalog-test"}output "requested_name" { value = terraform_data.catalog.input}output "recorded_name" { value = terraform_data.catalog.output}The resource address is terraform_data.catalog in this root module. Its type is terraform_data and its label is catalog. The requested_name output reads the configured input; recorded_name reads the computed output. In a fresh plan, requested_name is known while recorded_name may remain unknown until apply. After a successful reviewed local apply, both should equal catalog-test. This example does not require apply for the worksheet. The generated id is separate from the resource address; neither the label nor address is automatically a cloud identifier.
Use an existing object without managing it
The following HCL is fictional, non-executable teaching material. It assumes the demo provider from the previous lesson and an invented schema.
data "demo_network" "shared" { name = "training-network"}resource "demo_service" "catalog" { name = "catalog-test" network_id = data.demo_network.shared.id}The first block asks for the existing network; its expression address starts with data.. The second block creates or manages the catalog service and uses the returned network ID. Terraform can infer a dependency from that reference. A lookup can fail when no match is found, the result is ambiguous (depending on provider semantics), or permission is missing. In the worksheet, use synthetic responses instead of contacting any service.
Know when a data source is evaluated
A lookup may run during planning when its inputs are available. If an input depends on an as-yet-unknown resource result, Terraform can postpone the read. Consider this separate fictional illustration:
resource "demo_network" "new" { name = "training-new"}data "demo_network_details" "new" { network_id = demo_network.new.id}If new.id is unknown during the initial plan, the details read is deferred to apply. A data source is not a promise of an offline or side-effect-free plan: it can contact an API, encounter throttling or retrieve confidential information. Treat its results and saved plans as potentially sensitive. Inspect proposed changes rather than assuming every value is known.
Assign exactly one lifecycle owner
Keep a simple ownership inventory: shared network — network team state; catalog service — application team state. Do not declare the same real object as independently managed in two states. A data source can reference the shared object without introducing a second lifecycle owner. Adoption of an existing unmanaged resource requires a coordinated import and configuration review, taught later; simply adding a resource block is not safe adoption. If a team moves ownership, coordinate the state transition before making changes.
Practice with synthetic read results
Assume a fictional network query returns { "id": "net-training-07" }. The service receives net-training-07 as its network_id, while its own generated ID is a separate value. If lookup fails, stop and investigate the intended account, region, permissions and selection criteria rather than creating a duplicate network. The next chapter explains provider installation and version locks.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Resources versus data sources — knowledge check
Check provider configuration, schemas and lifecycle ownership.
Which block looks up an existing network without owning it?
What is the root address of resource terraform_data named catalog?
When may a data read be deferred?
Who should manage the same existing object in normal operation?
Does a data source guarantee that planning is offline?