Skip to content

Data Sources Reference

A data source is a read-only query a provider answers — an AMI ID, the availability zones, the current account. There is one way in: a data block in the configuration. That makes data.<type>.<name>.<attr> resolvable, and every plan reads it. No tool reads a data source on its own, so to see a value:

  • Read the plan. A read made at plan is in the document’s prior_state (show, which reads plan.uri). Planning only to look commits nothing: cancel the convergence rather than apply it.
  • Expose it as a root output. After an apply, output returns it any time.
data "aws_ami" "latest" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.latest.id
instance_type = "t3.micro"
}

count/for_each expand it into keyed instances, with count.index/each.key in scope. Deleting the block un-declares it: its stored value is pruned at the next apply.

The plan is one dependency-ordered pass over the whole module tree: a data source is read after what it depends on and before what depends on it, across module boundaries. The document shows which kind of read it was:

In the plan Meaning
prior_state Read at plan; the value the plan was computed against
resource_changes[] with ["read"] Moved to apply; action_reason says why; counted in plan.read
deferred_changes[] The provider said “not yet”; a later round reads it

up’s refresh: false skips refreshing managed objects only: data sources are read at every plan regardless. A read the provider rejects fails the plan with the provider’s error.

A read that cannot happen at plan moves to apply, as in Terraform:

  • read_because_config_unknown — the query holds a value nothing knows yet (values = [aws_vpc.main.id] before the VPC exists). It is read during the apply, once the value exists.
  • read_because_dependency_pending — a depends_on target still has a change pending. This is the case unknown-ness misses: a query can be entirely known and still name an object that does not exist yet.
data "local_file" "rb" {
filename = local_file.note.filename # known at plan: a literal in local_file.note
depends_on = [local_file.note] # without this, the read goes out now and fails
}

The ordering must be declared: Turf does not infer it from the query, any more than Terraform does. A module call’s depends_on is inherited by the data sources inside it. Either way nothing in the file needs changing to resolve it: approve the round. What needs the value known at plan (a count, a provider block) defers to the next round, which apply plans by itself.

State’s data.* entries are a cache of the last read, as in Terraform. A read is committed when its round applies — the value the plan read, so the apply issues no second read — and a round that failed partway commits the reads it made. A plan that is cancelled or never approved commits nothing. A destroy with refresh re-reads only the data sources the teardown evaluates (with refresh: false, it uses the committed reads), and a completed destroy empties the data namespace with everything else.

For a fresh value — the latest AMI — just run up again: every plan re-reads.

Turf™ is a trademark of Turf LLC. Terraform® is a trademark of HashiCorp. Ansible® is a trademark of Red Hat, LLC, registered in the United States and other countries. Helm®, Kubernetes®, Prometheus® and OpenTelemetry® are registered trademarks, and Jaeger™ is a trademark, of the Linux Foundation. OpenTofu® and Model Context Protocol® are registered trademarks of LF Projects, LLC. All other trademarks are the property of their respective owners. Turf is not affiliated with, endorsed by, or sponsored by HashiCorp, IBM, Red Hat, the Linux Foundation, LF Projects or the OpenTofu project.

© 2026 Turf LLC. Free for individuals, education and small businesses within the Community limits, under the Turf Community Edition License 1.0. Government and larger organizations need a commerciallicense.turf.build · Discord ·GitHub