Terraform Enterprise Workspace working directory

I am trying to determine a good repository structure setup in relation to the amount of TFE workspaces that I may need.

Right now my repository structure looks like this

pre-production/
  - resource1/
     - group1-resource1.tf
     - group2-resource1.tf
  - resource2/
      -group1-resource2.tf

modules/
  - module1
  - module2

Essentially what this leads to is that I have to have a TFE workspace pointing to the working directory of resource1/ and then I have to have another workspace for the working directory of resource2/

Is there a way to have one TFE workspace provision resources for the resource1/ and resource2/ directories? Do I need to restructure my repository to achieve this?

Hi @rk92,

Terraform Cloud and Terraform Enterprise both have a setting in the workspace configuration where you can enter the path from the root of your configuration package (your repository, if you are using version control integration) to the root module for that workspace’s configuration.

If you enter a path into that field then the remote execution environment will switch into that directory, as if you had run cd PATH, before running any Terraform commands.

Because the entire repository is still visible in the execution environment in this case, it still works to call the modules "../../modules/module1". If you use the remote operations feature in Terraform CLI with these workspaces configured with a working directory then the local client will include the full directory structure starting at your repository root when it uploads your working directory contents into Terraform Cloud.

Thanks for the reply.

Just to make sure I understood this, instead of having the working directory as pre-production/resource1 I should have it as modules/module1 ? Or are you referring to root module of the workspace’s configuration as the pre-production/ directory?

This would still follow a pattern of one workspace per module is that correct? I would need another workspace associated to modules/module2 to provision resources from pre-production/resource2?

I do use a VCS driven workflow where after merging a PR into master a Plan and Apply will run automatically.

In the TFE workspace TF resource this is just the working_directory input?
https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace

Hi @rk92,

The working directory setting specifies a single root module that acts as the top of the tree of modules. That root module presumably includes module blocks that refer to shared modules that aren’t intended to be used alone as root modules.

In your case I believe your intent is for each subdirectory of preproduction to be a root module, and so I’d expect to configure one workspace for each of those directories.

I think your modules directory contains shares modules that are used only from module blocks in other modules, in which case these should not be separate workspaces, because they will be included into the module tree of the other workspaces you already configured.

Thanks, makes more sense now. So generally a best practice is to set 1 workspace per directory that would provision resources? There is no way to have a single workspace that would be able to provision files from both pre-production/resource-1 and pre-production/resource2 ?

One workspace for pre-production/resource1 and one for pre-production/resource2 is the best approach in this case?

Also you’re correct in that pre-production/resource1/group1-resource1.tf has a module block that uses a source of ../../modules/module1