Terraform init with multiple folder sources

I am learning terraform by watching videos, the one I am using now is from 2020. One of the videos has me run the below code to initialize the environment which fails with this error: Too many command line arguments. Did you mean to use -chdir?

terraform init …/…/manifests (command that errors out)

So what is supposed to happen is it creates the state file in say the development directory but also uses the manifests directory for the infrastructure code… that way the dev and prod are identical… except dev maybe has one server running, where prod may have 4 etc…

Here is the directory structure…



Can anybody help me with this? I know you can do this by leveraging modules… but this is just pulling in additional tf.code from a central directory so to speak.

Hi @petetyo,

The legacy capability to specify a different configuration directory on the command line would cause Terraform to read the files from (in your case) ../../manifests instead of the current working directory, and so the somecode.tf files you showed in each environment directory would not be consulted at all in that case.

Child modules would indeed be the usual way to get this effect of instantiating the same module multiple times with separate configuration/state for each environment. That is the most straightforward option and the one that will allow you to use Terraform in the normal way, without any unusual extra arguments.

If you really do want to just re-instantiate the same root module multiple times with separate states then you can use workspaces to achieve that without needing a separate directory for each environment, but be sure to read When to use Multiple Workspaces first to learn about why that mechanism might not be ideal for this particular problem.

If you do want to use this unusual workflow of instantiating the same directory multiple times then it is possible to do it but also rather clunky, due to it not being the primary workflow; it involves setting environment variables and overriding backend configuration on the command line. I can try to describe it in more detail if you have a strong reason to prefer that approach, but I would consider it only a last resort and something you’d probably want to do with a wrapper script rather than directly from the command line.

Hi @apparentlymart ,

I do not wish to use an unusual workflow. I’m a little disappointed in the instructor who was suggesting this approach though. No need to describe it in more detail. The information you have provided me has pointed me in the correct direction.

I love the power of Terraform and look forward to learning more, so I can leverage Infrastructure as Code. Thanks again for all your input. I do greatly appreciate it.