Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Terraform to Always update a resource, even if there are no changes

Tags:

terraform

I have a terraform provider that works against an API that can out of band "lose" api objects. I've talked to the vendor and they said its in their backlog but don't seem to have any interest in actually fixing it.

The Terraform provider also does not use data sources, only remote state so that means it cannot actually detect that the field is missing or anything.

If I force terraform to update the resource by changing the state, the field returns however its hacky. Is there something like "ignore_changes" that always updates a resource?

like image 683
Red 5 Avatar asked Oct 26 '25 06:10

Red 5


1 Answers

One of the Terraform provider API operations for managed resource instances is ReadResource, which in the Terraform SDK maps tothe Read callback in schema.Resource.

The purpose of this function is to use the information that was recorded in the Terraform state at the end of the last operation to produce a new object that describes the current state of the object in the remote system. As well as detecting "drift" for attributes of the object itself, this function can also potentially detect and report that the remote object is no longer present.

In the current SDK API, a Read implementation can report that the object no longer exists by calling d.SetId with an empty string as the ID, because the SDK requires that any valid object have a non-empty ID.

Terraform calls ReadResource as part of preparing a plan in terraform plan, or in the implied planning step of a no-arguments terraform apply. Therefore if the provider is able to detect and signal when the object no longer exists or if any of its attributes have changed since the conclusion of the last operation. Terraform will then take that drift into account when comparing with the configuration to produce the proposed plan.

Terraform's workflow generally expects that all resources will converge on a stable state so that users can run terraform plan and see the message that there are no changes pending. A situation where a provider perpetually proposes more changes on every plan is usually considered to be a bug in the provider, and so I wouldn't suggest that as a solution to your problem and Terraform has no features intended to provide such a capability.

like image 133
Martin Atkins Avatar answered Oct 29 '25 09:10

Martin Atkins