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.store: Thestoreblock has 4 possible arguments,input,replace,sensitive, andversion.input: (Optional, write-only) This argument can accept any type, and is write-only hence can also accept ephemeral values. The value supplied here during apply will be copied to either of thestoreblock'soutputorsensitive_outputattributes and stored in the resource's state.replace: (Optional) Ifreplaceistrue, any change in astoreblock output value will trigger a replacement of the resource rather than an update.sensitive: (Optional) Ifsensitiveis set totrue, theinputvalue will be stored insensitive_output.version: (Optional) If version is set, changes to theinputvalue won't be reflected inoutputunless there is also a change to theversionargument.
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.store: Thestoreblock has 2 possible outputs,outputorsensitive_output.output,sensitive_output: These attribute are mutually exclusive and set depending on the value of thesensitivestoreargument. They work like the top leveloutputattribute, and capture their value from thestoreblockinputargumnet.
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]
}
}
Here we capture the result of the ephemeral password value during the initial apply, and store it in the state of terraform_data.test.
ephemeral "random_password" "test" {
length = 16
}
resource "terraform_data" "test" {
store {
input = ephemeral.random_password.test.result
sensitive = true
version = 1
}
}
output "password" {
sensitive = true
value = terraform_data.test.store.sensitive_output
}
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"
}
}