Changing Computed Attribute Error in Framework


I am using the Provider Framework and have a Computed attribute which changes based on another attribute. I will call the attribute in the config A and the dependent attribute B.

The value of B is calculated when the values in A are processed. This works fine for Create, but Update gives the “produced an unexpected new value” error if a value is added to or removed from A.

To me, it seems logical, that a Computed value can change and that’s ok. I saw another post about a custom plan modifier, but the values in B must be calculated from the values in A and doing this changes the values permanently in the backing API … in this case, the value of B is not known until the apply processing completes.

Here is the schema snippet
attrs[“A”] = schema.SetAttribute{
Description: “A”,
ElementType: types.Int64Type,
Optional: true,

attrs["B"] = schema.MapAttribute{
	Description: "B",
	ElementType: types.Int64Type,
	Computed:    true,
	PlanModifiers: []planmodifier.Map{

What is the purpose of this? It’s storing another ID which gets created after processing A and makes it easier during Updates because all of the data is in state instead of figuring it out each time.

How can I fix this so that updated the computed value B doesn’t cause an error?


These two things:

the value of B is not known until the apply processing completes



Seem directly at odds with one another.

If the value must be recomputed at apply time, then copying the old state value into the plan isn’t appropriate.

As @hQnVyLRx mentions, UseStateForUnknown is telling the framework to plan on always preserving the prior state value when it sounds like what really should be happening is that it is conditional when the prior state is preserved. To implement this behavior today in the framework would require some custom plan modification logic to encode the expectations.

There is also a feature request for a “UnknownIf” attribute plan modifier(s) (Consider UnknownIf Attribute Plan Modifiers · Issue #605 · hashicorp/terraform-plugin-framework · GitHub), which could help remove the custom plan modifier logic with a schema implementation such as (rough sketch):

PlanModifiers: []planmodifier.Map{
	mapplanmodifier.UseStateForUnknown(), // always preserve prior state
	mapplanmodifier.UnknownIfChanged(path.Root("a")), // except this case :)

If that sounds like something you might be interested in, please feel free to :+1: and/or comment about your use case there (even if its just a link to this topic) to help the maintainers prioritize that enhancement.