Workspaces and TF_DATA_DIR

The docs for TF_DATA_DIR say:

TF_DATA_DIR changes the location where Terraform keeps its per-working-directory data, such as the current remote backend configuration.

By default this data is written into a .terraform subdirectory of the current directory, but the path given in TF_DATA_DIR will be used instead if non-empty.

In most cases it should not be necessary to set this variable, but it may be useful to do so if e.g. the working directory is not writable.

This is exactly what I’m looking for - the folder where I am executing terraform is not writable so I’d like the data to be written to a separate folder. However, I’d also like to use workspaces, but when I try to create a new workspace with TF_DATA_DIR set I get the error:

mkdir terraform.tfstate.d: read-only file system

Is there a way to make workspaces use TF_DATA_DIR to write their data too?

Hi @tomyan,

The directory used for tracking workspace states for the local backend is customizable via its workspace_dir argument.

With that said, if you are working in a read-only filesystem I imagine you’re setting up some sort of automation around Terraform for team use, in which case I’d recommend moving away from the local backend and using remote state instead. That way Terraform will write the state snapshots directly to a remote data store, and not write them to local disk at all.

The “data dir” that TF_DATA_DIR refers to will then be the only local directory that Terraform will try to write to, and it will only place in there disposable data that can be recreated by terraform init, and so you can safely place TF_DATA_DIR in a temporary directory used only for a single run. (This is similar to how Terraform Cloud and Terraform Enterprise run Terraform in an automated way, for example.)

Thanks @apparentlymart, that’s exactly what I needed to work out what was wrong. Cheers!