With the terraform-plugin-framework how can i access the full state which terraform is managing for all providers?

Hello,

With the terraform-plugin-framework how can I access the full state, which terraform is managing for all providers?

For example:

If a resource is read:

func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp resource.ReadResponse) {

var data ExampleResourceModel

// Read Terraform prior state data into the model*

resp.Diagnostics.Append(req.State.Get(ctx, &data)…)*

The state of the component is retrieved via the req.State.Get method.

But how do I access the complete state, everything which is in the terrafrom statefile?

Thanks!

Are you suggesting you want to access the entire state including state for other resources and other providers, besides the one for which

is being called?

I don’t think that’s possible for a provider to do, by design.

yes, exactly.

I want to create a provider / resource which has access to the complete state.
I want to access the complete terraform state file, more or less.
One idee would be to configure the provider with the state file location and let the provider read the statefile, but this is locked at least locally and it seams this would not be a clean solution.

Thanks

There is no clean solution to what you want to do, because the entire design of Terraform is that Terraform core controls the central details of managing state, and providers are called just to perform actions on individual resources.

So, with your intent, you are actively fighting against Terraform design principles.

What is the ultimate goal, that makes you want to do this?

Okay, good to know.

one use case would be to use for example to the amount of resources in the state as a parameter for an backend system which the resource is is controlling over the backend api.
So that the own resources know how many resources overall exist in this particular configuration.(as an example).

I see… at that point, I think you should wrap Terraform in a script which counts the resources by running terraform state list and counting the lines, and writes the result to a .tfvars file before starting the Terraform run.

The provider interface just doesn’t give them access to what you want in any clean way - you’d be stuck having to try to break out of the provider abstraction and effectively “reverse-engineer” the currently running Terraform operation. It’s likely to prove too fragile to be worth attempting.

thanks for this informations, this clarifies it for me.