Changing workspace directory - state file

We currently use different workspaces for our DEV and PROD accounts, but the structure is not currently split by environment. I want to move over to a more modular approach as well as having the environments in their own directories.

My question is, if I do this refactoring and change the directory under which the workspace runs, do I need to do anything to also move the location of the state file (as the docs say it is stored in the root of the directory it runs under, and this will now be a subdirectory). Or does the workspace handle this automatically?

It’s not possible to answer this question, because the proposed change:

is too vaguely described to be certain what is meant.

Also this question is in the TFC&E category, but some of the text sounds more like it’s talking about running Terraform CLI locally:

There is also the complication that “workspace” is an overridden term, with overlapping but different meanings, between Terraform CLI, and Terraform Cloud & Enterprise.

EDIT: Screenshots or textual representations of the tree of files as it is currently and how you intend to change it, would probably help.

Sorry if my post was unclear…

This is using Terraform Cloud - it allows you to use the same repo for multiple workspaces, and the different state files are stored within Terraform Cloud (see Refactor Monolithic Terraform Configuration | Terraform | HashiCorp Developer).

At the moment our files are stored like the following:

|- modules
|-- external-ecs-service
|-- |- data.tf
|-- |- main.tf
|-- |- outputs.tf
|-- |- variables.tf
|-- internal-ecs-service
|-- |- data.tf
|-- |- main.tf
|-- |- outputs.tf
|-- |- variables.tf
|- ecs.tf
|- s3.tf
|- variables.tf
|- vpc.tf

and the only thing that currently separates the accounts is the branch name / terraform cloud variables.

Now I want to move to the following:

|- modules
|-- ecs-service
|-- |- data.tf
|-- |- main.tf
|-- |- outputs.tf
|-- |- variables.tf
|-- network
|-- |- data.tf
|-- |- main.tf
|-- |- outputs.tf
|-- |- variables.tf
|-- s3
|-- |- data.tf
|-- |- main.tf
|-- |- outputs.tf
|-- |- variables.tf
|- dev
|-- main.tf
|-- variables.tf
|- prod
|-- main.tf
|-- variables.tf

So now things are shared through the modules, but the prod and dev environments can have separate configurations (as we will be adding more environments)

So for example when I change the prod one to run from the prod directory, I just want to make sure that the state won’t be lost (as it currently runs from the root)

In Terraform Cloud, state storage is a part of the workspace. You can freely reconfigure the VCS branch and working directory that Terraform Cloud looks at, whilst keeping your state.

The only thing to watch out for is that you appropriately synchronize your rearrangement commits to Git, and your reconfiguration of Terraform Cloud.

Terraform does have a safety measure, where it refuses to run at all if pointed to a directory that contains no .tf files at all - but accidentally running Terraform against a wrong directory containing some .tf files could lead to lots of undesired changes - including resource destruction.

For that reason, you might want to employ a protective countermeasure, such as locking the workspace, or switching it to manual apply mode, whilst you make the changes, to ensure a Git-triggered Terraform run doesn’t have unwanted consequences.