在 terraform 中使用工作空间时如何更改本地后端状态的路径?

Nic*_*ick 2 automation terraform

将 terraform工作区本地后端结合使用的预期配置是什么?

本地后端支持工作空间,但似乎您对实际状态的存储位置没有太多控制权。

当您不使用工作区时,您可以path向本地后端提供一个参数来控制状态文件的存储位置。

# 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"
Run Code Online (Sandbox Code Playgroud)

我期望在使用工作区时具有类似的功能,因为您将提供一个目录,path并且工作区将在该目录下创建

例如:

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
Run Code Online (Sandbox Code Playgroud)

将导致状态

/path/to/terraform.tfstate.d/first/terraform.tfstate
/path/to/terraform.tfstate.d/second/terraform.tfstate
Run Code Online (Sandbox Code Playgroud)

然而,情况似乎并非如此。看起来本地后端忽略了路径参数并将工作区配置放在工作目录中。

我是否遗漏了什么或者您无法控制本地后端工作区状态?

Nic*_*ick 5

本地后端有一个未记录的标志workspace_dir可以解决此问题。

文档任务在此处跟踪

terraform {
  backend "local" {
    workspace_dir = "/path/to/terraform.tfstate.d"
  }
}
Run Code Online (Sandbox Code Playgroud)