Data Resource: Read computed value from state

Regarding this issue, is there a way to read the existing value from the state file? My purpose is to read “lease_id” from state file, if the lease is still active continue without creating a new Vault credential.

Hi @hus,

Just a thought.
Could you do a tf plan -out #plan.tfplan# | tf show -json #out.json# and then use something like jq to search for “lease_id” in the #out.json#.


Thanks, I’m looking for a way to check the state value in the data source, using the Vault provider.

The state file will capture the data sources when used with a null resource.
Other than the option of parsing the state file for “lease_id”, the data source output could be looked into.


I’m having weird deja vu, I thought I answered this already, but it may have been a similar question. :smiley:

Data sources only get the configuration values to work with, and do not get the prior state. This is on the assumption that data sources will be replacing the prior state entirely. So computed values not found in the config will not be accessible in Read.

Thanks @paddy I can say that it’s not de ja vu :grinning:

Now I understand thank you!

The Get functions are able to read state when we use them with resources because CurrentState field in type ReadResourceRequest allows resources to read terraform state, is this correct?

That’s accurate! The ResourceData type kind of smooshes together the Config, PriorState, PlannedNewState, and NewState in those request and response types, and that can get confusing at times, because as you can see, no two requests or responses get or expect the same data back, which can be challenging. So data sources don’t get state, because they’re supposed to overwrite it with whatever the current state of the data source is, while resources do get state, because they’re only supposed to update the parts of it in the plan, and so being able to reference existing values is necessary.

1 Like