UseStateForUnknown doesn't work when /null/ -- is this important?

I’ve been struggling to understand why my provider is seemingly expecting state churn for some optional + computed attributes. Then I noticed the specific language of the UseStateForUnknown() PlanModifier:

copies the prior state value for an attribute into that attribute’s plan, if that state is non-null.

It’s not obvious to me why this PlanModifier only applies to non-null values.

Is there something… Important? …about the fact that it ignores null values in the state?

Options I’m considering:

  • cloning the UseStateForUnknown PlanModifier with the ability to enforce null values.
  • saving my computed + optional lists and sets as empty, rather than null

If I’m missing something important, I’d sure appreciate a nudge to keep me off the rocks.


1 Like

Hi @bflad @austin.valle

We are running into this as well, we have computed + optional attributes in lists which can be null if the user doesn’t specify and the API doesn’t return a value as well.

This creates unnecessary drift detection even if we use UseStateForUnknown PlanModifier.

How can we address this?

1 Like

I agree with you guys, this happens same with me as well. My provider works well on create/read/update/delete/import but if i use the import block to create code and state file then the drift happens and says was null but now have value. i have planmodifier in all attributes and tried adding default value for few attribute as well but the issue is same.

it would be really helpful if we get an idea to overcome the null to be considered in UseStateForUnkown.

For some background from the core Terraform side of things - in the Terraform data model null is the absence of a value, i.e. it is equivalent to being unset. So you’re not exactly “saving null” to the state, you are removing any value which was there, and doing nothing if there was no value.

You can’t use the null state value in place of an unknown, because there is no state value. If UseStateForUnknown copied null, then you would never be able to set a value because it would always plan to take the empty null.