Terraform Cloud can't find modules after state migration

Hello,

I have been using the Terraform CLI successfully but have decided to move my state out of an S3 backend and into Terraform Cloud. To do this, I added the required cloud settings to the terraform block, and then ran terraform init.

terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "dev-workspace"
    }
  }
}

All seemed to be working fine, and the resources managed by the state file were displayed in Terraform Cloud.

However, now whenever I try to plan/apply it fails, with the error stating no such file or directory for my modules directory. But as can be seen at the end of the output below, I am able to change to that directory without issue.

Why can Terraform no longer find my modules?

user@ubuntu:~/terraform/envs/dev$ terraform apply
Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://app.terraform.io/app/my-org/dev-workspace/runs/run-xxUvi7tmy3mc87mg

Waiting for the plan to start...

Terraform v1.5.7
on linux_amd64
Initializing plugins and modules...

Initializing Terraform Cloud...
Initializing modules...
- sandbox in 
â•·
│ Error: Unreadable module directory
│ 
│ Unable to evaluate directory symlink: lstat ../../modules: no such file or
│ directory
╵

â•·
│ Error: Unreadable module directory
│ 
│ The directory  could not be read for module "sandbox" at main.tf:1.
╵

Operation failed: failed running terraform init (exit 1)
user@ubuntu:~/terraform/envs/dev$ cd ../../modules
user@ubuntu:~/terraform/modules$ 

Hi @david.gard,

I’m guessing from the current working directory shown in your shell prompt that you have a VCS repository that contains multiple Terraform configurations each in its own subdirectory, and then there are some shared modules also in the same repository but in a sibling or parent directory to the root module you are trying to use here.

If that’s true then you will need to tell Terraform Cloud, in your workspace settings, that your root module is not in the root of the repository. To do that, find the “working directory” setting and specify a path relative to the repository root, such as envs/dev.

That setting has two different effects:

  1. If you use Terraform Cloud’s VCS integration then it will know which subdirectory of the connected repository to use when automatically triggering a run.
  2. If you use remote operations with Terraform CLI then the local CLI will infer that it needs to upload the entire directory tree at ../../ in order to ensure that the entire repository will be available in the remote execution environment.

In your case it’s the second of these that is important: currently Terraform CLI thinks your working directory is the root of the relevant repository and so only uploads that directory to Terraform Cloud. Your parent directory containing the modules directory is therefore missing. If you specify the working directory then Terraform CLI will understand your directory layout enough to know it should upload the whole thing, including that modules directory.

1 Like

Thanks for the clear explanation, that was indeed my issue and everything is now working as expected.