Feature Request: Lifecycle pre-update hook for resource metadata modification
Terraform Version
Terraform v1.15.4
Use Cases
Many organizations require resource metadata such as tags or labels to reflect the last modification performed through Terraform.
Common examples include:
- modified-datetime
- last-updated
- updated-by
- change-ticket
- deployment-reference
Today Terraform can set those values during resource creation, but it has no native mechanism to update them automatically when a resource is modified.
This makes governance, auditing and operational tracking difficult, especially in large environments where thousands of resources are managed through Terraform.
The end goal is to allow users to automatically update metadata whenever Terraform detects a resource change and applies an update.
Attempted Solutions
Several approaches were evaluated:
Using timestamps
tags = {
modified-datetime = timestamp()
}
This causes perpetual diffs because the timestamp changes on every plan.
Using external data sources
data "external" "git_date" {
program = ["bash", "-c", "git log -1 --format=%cs"]
}
This only reflects repository state and not actual Terraform resource updates.
Using null_resource triggers
resource "null_resource" "metadata" {
triggers = {
modified_datetime = timestamp()
}
}
This does not provide a way to update attributes of another resource before it is modified.
###Using lifecycle
lifecycle {
ignore_changes = [
tags["modified-datetime"]
]
}
This avoids drift but prevents Terraform from managing the value.
None of these approaches provide a generic and reliable mechanism to update resource metadata only when a resource is actually modified.
Proposal
Introduce a lifecycle hook executed immediately before a resource update operation. Example:
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
tags = {
Environment = "prod"
}
lifecycle {
before_update {
tags = merge(self.tags, { modified-datetime = timestamp() }) }
}
}
References
Related discussions:
- Support for resource lifecycle hooks
- Support for update-time metadata tracking
- Common requests around automatic tagging and auditing
I was unable to find a Terraform Core feature that currently addresses this requirement.
Source: hashicorp/terraform