Skip to main content

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 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 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 and Input Variables 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.

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)

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"
}
}

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.