Nick
Nick

Reputation: 2034

How to change the path for local backend state when using workspaces in terraform?

What is the expected configuration for using terraform workspaces with the local backend?

The local backend supports workspacing, but it does not appear you have much control over where the actual state is stored.

When you are not using workspaces you can supply a path parameter to the local backend to control where the state files are stored.

# Either in main.tf
terraform {
  backend "local" {
    path = "/path/to/terraform.tfstate
  }
}

# Or as a flag
terraform init -backend-config="path=/path/to/terraform.tfstate"

I expected analogous functionality when using workspaces in that you would supply a directory for path and the workspaces would get created under that directory

For example:

terraform new workspace first
terraform init -backend-config="path=/path/to/terraform.tfstate.d"
terraform apply
terraform new workspace second
terraform init -backend-config="path=/path/to/terraform.tfstate.d"
terraform apply

would result in the state

/path/to/terraform.tfstate.d/first/terraform.tfstate
/path/to/terraform.tfstate.d/second/terraform.tfstate

This does not appear to be the case however. It looks like the local backend ignores the path parameter and puts the workspace configuration in the working directory.

Am I missing something or are you unable to control local backend workspace state?

Upvotes: 2

Views: 9608

Answers (1)

Nick
Nick

Reputation: 2034

There is an undocumented flag for the local backend workspace_dir that solves this issue.

The documentation task is tracked here

terraform {
  backend "local" {
    workspace_dir = "/path/to/terraform.tfstate.d"
  }
}

Upvotes: 5

Related Questions