Terraform
terraform_data resource reference
The terraform_data resource type 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.
Arguments
You can use the following arguments in the terraform_data resource type:
input: (Optional) Specifies a value to store in the instance state. Terraform prints the values in theoutputattribute after runningterraform apply.triggers_replace: (Optional) Specifies a value to store in the instance state. Terraform replaces the resource when the value changes.
Attributes
The terraform_data resource exports the following attributes:
id: A string value unique to the resource instance.output: The computed value derived from theinputargument. In plans whereoutputis unknown, Terraform returns the same type of value used in theinputargument.
Examples
The following examples implement common patterns for using the terraform_data resource type:
Provide data for the replace_triggered_by argument
The replace_triggered_by argument directive is one of the arguments you can add to the lifecycle meta-argument. You must specify resource addresses to use this argument because forcing replacement is based on the planned operations for all of the mentioned resources.
Plain data values, such as local values and input variables, aren't valid in replace_triggered_by. Because terraform_data resources plan an action each time the input value changes, you can use this resource type to indirectly specify a plain value to trigger replacement.
variable "revision" {
default = 1
}
resource "terraform_data" "replacement" {
input = var.revision
}
resource "example_database" "test" {
lifecycle {
replace_triggered_by = [terraform_data.replacement]
}
}
Enable arbitrary operations
In the following example, the terraform_data resource serves as a container for arbitrary operations taken by the provisioner "local-exec" block.
resource "aws_instance" "web" {
# ...
}
resource "aws_instance" "database" {
# ...
}
resource "terraform_data" "bootstrap" {
triggers_replace = [
aws_instance.web.id,
aws_instance.database.id
]
provisioner "local-exec" {
command = "bootstrap-hosts.sh"
}
}