【问题标题】:When using Azure Blob for state management, is there any big benefit for using Terraform Workspaces?使用 Azure Blob 进行状态管理时,使用 Terraform 工作区有什么大好处吗?
【发布时间】:2021-02-23 02:34:21
【问题描述】:
【问题讨论】:
标签:
terraform
terraform-provider-azure
【解决方案1】:
你不能在 terraform 后端设置变量。因此,如果您想在没有不同 terraform 块的情况下重用同一个模块,您可能需要切换到工作区。
有些人会为他们的所有状态使用一个存储帐户。使用键定义项目。后端将负责根据工作空间名称将状态键入到单独的位置。
这是手动驱动 terraform 命令时小型项目的下降。您还可以在代码中引用terraform.workspace 来设置条件。
最大的缺点是处理身份验证和变量。如果您使用不同的订阅并以另一个订阅的状态登录,您会收到可怕的消息,即所有内容都需要更换。所以需要加强态势感知。
跟踪输入的值需要存储在某个地方。当我过去这样做时,我最终将变量存储为本地变量,并以工作区名称为条件。
▶ cat .\main.tf
variable "environment" {
type = string
}
terraform {
backend "azurerm" {
resource_group_name = "tfstate"
storage_account_name = "account"
container_name = var.environment
key = "terraform.tfstate"
}
}
~\projects\test\t8 ◷ 9:32:49 AM
▶ terraform init
Initializing the backend...
Error: Variables not allowed
on main.tf line 9, in terraform:
9: container_name = var.environment
Variables may not be used here.