Terraform plan gives Unable to find remote state

Hi Terraform team
I made a thread two weeks ago regarding the project structure.

The final resulting structure are multiple resource stacks:

There’s only one issue I’m having. If I try to run Terraform plan within the Kubernetes as well as Networking stack, I get the following error:

.\terraform.exe plan
data.terraform_remote_state.Kubernetes_crossstackreferenceinputNetwork_0E12A4F6: Reading...
data.terraform_remote_state.Kubernetes_crossstackreferenceinputResourceGroup_921D924C: Reading...
data.terraform_remote_state.Kubernetes_crossstackreferenceinputResourceGroup_921D924C: Still reading... [10s elapsed]
data.terraform_remote_state.Kubernetes_crossstackreferenceinputNetwork_0E12A4F6: Still reading... [10s elapsed]
│ Error: Unable to find remote state
│   with data.terraform_remote_state.Kubernetes_crossstackreferenceinputNetwork_0E12A4F6,
│   on cdk.tf.json line 18, in data.terraform_remote_state.Kubernetes_crossstackreferenceinputNetwork_0E12A4F6:
│   18:         "workspace": "${terraform.workspace}"
│ No stored state was found for the given workspace in the given backend.

The same error applies even after initialising and planning the ResourceGroup stack.
What am I missing? The ResourceGroup stack pm its own is planning, same as the Storage stack but that’s obvious as those don’t have “dependencies”, unlike the Networking and Kubernetes stacks

Thank you as always!

Bumping just in case

Which backend do you have configured?
Are you intentionally using Terraform workspaces and if so, have you selected the workspace that is in use?

1 Like

Thank you for replying @jsteinich ! I was losing my mind over this.
I did not have a backend configured yet, I did not deem that necessary as this error is not unique to only StorageAccounts but my Kubernetes class as well (it references the networking resources).
No, I am not using workspaces, there is no declaration of a workspace in any of my classes.
I’ll try adding a backend config and see if that changes things!

Okay, It seems to not give the “unable to read state” error (for now?)
Instead I get an error that the value of the ResourceGroup.Name is too long (over 90 chars). This happens with the following declaration of the StorageAccount stack:

            AzurermBackend azurermBackend = new(this, new AzurermBackendProps
                ResourceGroupName = DefaultVars.getResourceGroupName(),

The ResourceGroup.Name is simply "cas-sys-devtest". I originally referenced it through the name of the resource itself (ResourceGroup.Name). But that causes a value of over 200 chars to be generated, which bricks the interpreter.

(value of the 200 char long resourcegroup name:

In order to try and fix it, I tried to assign a hardcoded value, and as such, directly bind cas-sys-devtest to the name.

This causes yet another error:

Error: Failed to get existing workspaces: Error retrieving keys for Storage Account “authtickets”: storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=404 – Original Error: autorest/azure: Service returned an error. Status=404 Code=“ResourceGroupNotFound” Message=“Resource group ‘cassys-we-devtest-rg’ could not be found.”

What is the appropriate way of passing the name of the resourcegroup here?

Backends can’t reference other resources. Everything needs to be known at synth time. This means hardcoding, reading from a configuration file, or using more advanced Terraform workflows in addition to cdktf.

I’m not very familiar with the Azure backend, but from the looks of the error I have a few ideas:

  • The resource group hasn’t been created yet.
  • The name entered doesn’t match what it was created with.
  • The credentials used to access to have access to the group.