Does Terraform consider a change in a computed value a non-change?

Hi :wave:t2:

I have an attribute called source_code_hash that is set to Computed: true. I also have another attribute called filename which is a file path.

In my provider I have a Read CRUD method that generates a hash of the file and updates the internal state value for source_code_hash (using d.Set()) if the file has changed (as a change in the file content would obviously result in a new hash being produced).

When using the Delve step debugger I can see that the internal state representation shows the source_code_hash attribute with the new hash value, but at the end of running terraform plan the output suggests there are no differences. If I then manually check the terraform.tfstate file I still see the old value for source_code_hash is set for source_code_hash (as if calling d.Set() hadn’t succeeded, although step debugging suggests otherwise, and also that call doesn’t error).

So there are two issues:

  1. Although I’m calling d.Set() in my ‘Read’ CRUD method to update the source_code_hash attribute (and it looks like it’s changing internally), the end result is the state file hasn’t changed and I think that is fundamentally driving the second issue.

  2. Changing the state doesn’t trigger my ‘Update’ CRUD method to be called (i.e. Terraform doesn’t think there’s anything to do).

I feel like this is somehow fundamentally caused by the source_code_hash attribute being a Computed value. But I’m not sure why that would be.

I’m taking a guess here, but because source_code_hash is a computed attribute, and the user otherwise hasn’t modified the filename attribute, then I’m thinking that although I’ve called d.Set() to update the state (and calling d.Get() returns the updated value) maybe Terraform doesn’t persist the state back to disk because the user hasn’t updated filename and as source_code_hash is ‘computed’ Terraform sees no point in persisting that? But I honestly would have thought if you change the computed attribute then that would be important to keep stored in the state file. Similarly, maybe as only the ‘computed’ attribute has been modified (internally by my provider) Terraform considers nothing changed. Again I’m just taking guesses here as I’ve no idea.

Any guidance/help appreciated here as I’m a bit stuck now.

Many thanks!