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:

image
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:
"${data.terraform_remote_state.Storage_crossstackreferenceinputResourceGroup_C0FA14C2.outputs.ResourceGroup_crossstackoutputazurermresourcegroupResourceGroupazurermresourcegroupBD617C89name_8FD233F4}")

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.

Hi, was this issue resolved? If so, please can you tell us how.

thanx

Hey, I haven’t been able to spend too much time on the issue back then as I was in a time constraint.

What I did in order to fix it is to get rid of the file structure and just create one God file, structured with all the resources per category in the form of comments.

Not my proudest moment but I had to deliver results

1 Like