# Workspaces and TF\_DATA\_DIR

**URL:** <https://discuss.hashicorp.com/t/workspaces-and-tf-data-dir/8027>\
**Category:** Terraform\
**Created:** [April 24, 2020, 3:32pm UTC](https://discuss.hashicorp.com/t/workspaces-and-tf-data-dir/8027 "2020-04-24T15:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomyan](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tomyan/32/6272_2.png) [@tomyan](https://discuss.hashicorp.com/u/tomyan)\
**Post date:** [April 24, 2020, 3:32pm UTC](https://discuss.hashicorp.com/t/workspaces-and-tf-data-dir/8027/1 "2020-04-24T15:32:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 24, 2020, 7:03pm UTC](https://discuss.hashicorp.com/t/workspaces-and-tf-data-dir/8027/2 "2020-04-24T19:03:49Z")

</div>

Hi @tomyan,

The directory used for tracking workspace states for the local backend is customizable via its [`workspace_dir`](https://www.terraform.io/docs/backends/types/local.html#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](https://learn.hashicorp.com/terraform/development/running-terraform-in-automation) for team use, in which case I’d recommend moving away from the local backend and using [remote state](https://www.terraform.io/docs/state/remote.html) 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.)

---

<div class="post-metadata">

**Author:** ![tomyan](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tomyan/32/6272_2.png) [@tomyan](https://discuss.hashicorp.com/u/tomyan)\
**Post date:** [April 26, 2020, 10:27pm UTC](https://discuss.hashicorp.com/t/workspaces-and-tf-data-dir/8027/3 "2020-04-26T22:27:51Z")

</div>

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