Files
opentf/website/docs/language/resources/tf-data.mdx
Martin Atkins dc9bec611c website: Reorganize and tidy some lifecycle-related information
Over time the discussion about "lifecycle" blocks in the documentation
became confusing because the docs originally written for managed resource
lifecycle got partially generalized for resources of other modes and for
module calls, even though each of those has a completely different
lifecycle and thus a different set of lifecycle settings.

This is a first pass at trying to reorganize that so that the "lifecycle"
page is really just an index of all of the different kinds of lifecycle
block that exist in the language, while the main documentation for each
use of that block type now belongs with the documentation of the block
it's nested within.

While working on this I also found that there was some duplication inside
the "data sources" page where the same information was described multiple
times, and a few other cases where things had become inconsistent over
time. This also includes a little extra content to try to clarify the
difference between managed, data, and ephemeral resources and to make it
explicit that the "Resources" section is focused only on managed resources
because that is the primary resource mode.

As usual there's lots more that could be done here -- this documentation
has gradually evolved over time and is full of weird quirks due to that
evolution -- but I decided to draw a line here so that the diff wouldn't
get too large.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-17 15:08:14 -07:00

83 lines
3.0 KiB
Plaintext

---
description: >-
Retrieves the root module output values from an OpenTofu state snapshot stored
in a remote backend.
---
# The `terraform_data` Managed Resource Type
The `terraform_data` implements the standard resource lifecycle, but does not directly take any other actions.
You can use the `terraform_data` resource without requiring or configuring a provider. It is always available through a built-in provider with the [source address](../../language/providers/requirements.mdx#source-addresses) `terraform.io/builtin/terraform`.
The `terraform_data` resource is useful for storing values which need to follow a manage resource lifecycle, and for triggering provisioners when there is no other logical managed resource in which to place them.
## Example Usage (data for `replace_triggered_by`)
[The `replace_triggered_by` lifecycle argument](behavior.mdx#replace_triggered_by) requires all of the given addresses to be for resources, because the decision to force replacement is based on the planned actions for all of the mentioned resources.
Plain data values such as [Local Values](../../language/values/locals.mdx) and [Input Variables](../../language/values/variables.mdx) don't have any side-effects to plan against and so they aren't valid in `replace_triggered_by`. You can use `terraform_data`'s behavior of planning an action each time `input` changes to _indirectly_ use a plain value to trigger replacement.
```hcl
variable "revision" {
default = 1
}
resource "terraform_data" "replacement" {
input = var.revision
}
# This resource has no convenient attribute which forces replacement,
# but can now be replaced by any change to the revision variable value.
resource "example_database" "test" {
lifecycle {
replace_triggered_by = [terraform_data.replacement]
}
}
```
## Example Usage (`null_resource` replacement)
```hcl
resource "aws_instance" "web" {
# ...
}
resource "aws_instance" "database" {
# ...
}
# A use-case for terraform_data is as a do-nothing container
# for arbitrary actions taken by a provisioner.
resource "terraform_data" "bootstrap" {
triggers_replace = [
aws_instance.web.id,
aws_instance.database.id
]
provisioner "local-exec" {
command = "bootstrap-hosts.sh"
}
}
```
`moved` block can be used to migrate from the `null_resource` to the `terraform_data` resource. [Migration guide](https://search.opentofu.org/provider/hashicorp/null/latest/docs/guides/terraform-migration)
## Argument Reference
The following arguments are supported:
* `input` - (Optional) A value which will be stored in the instance state, and reflected in the `output` attribute after apply.
* `triggers_replace` - (Optional) A value which is stored in the instance state, and will force replacement when the value changes.
## Attributes Reference
In addition to the above, the following attributes are exported:
* `id` - A string value unique to the resource instance.
* `output` - The computed value derived from the `input` argument. During a plan where `output` is unknown, it will still be of the same type as `input`.