Terraform部分远程后端不能包含插值?

Terraform部分远程后端不能包含插值?,terraform,terraform-provider-aws,terraform-enterprise,Terraform,Terraform Provider Aws,Terraform Enterprise,我正在尝试在Jenkins动态配置Terraform企业工作区。为此,我需要能够在main.tf中动态设置远程后端工作区名称。像这样: # Using a single workspace: terraform { backend "remote" { hostname = "app.xxx.xxx.com" organization = "YYYY" # new workspace variable workspaces { name = "

我正在尝试在Jenkins动态配置Terraform企业工作区。为此,我需要能够在main.tf中动态设置远程后端工作区名称。像这样:

# Using a single workspace:
terraform {
  backend "remote" {
    hostname = "app.xxx.xxx.com"
    organization = "YYYY"


    # new workspace variable
    workspaces {
      name = "${var.workspace_name}"
    }
  }
}
现在当我跑步时:

    terraform init -backend-config="workspace_name=testtest"
我得到:

Error loading backend config: 1 error(s) occurred:

* terraform.backend: configuration cannot contain interpolations

The backend configuration is loaded by Terraform extremely early, before
the core of Terraform can be initialized. This is necessary because the backend
dictates the behavior of that core. The core is what handles interpolation
processing. Because of this, interpolations cannot be used in backend
configuration.

If you'd like to parameterize backend configuration, we recommend using
partial configuration with the "-backend-config" flag to "terraform init".

我想用terraform做的事情可能吗?

您不能将任何变量
“${var.workspace\u name}”
或插值放入后端远程状态存储。 但是,您可以在后端值旁边创建一个文件,该文件在
main.tf
文件中可能如下所示:

# Terraform backend State-Sotre
terraform {
  backend "s3" {}
}
并放入
dev.backend.tfvars
中,例如:

bucket         = "BUCKET_NAME"

encrypt        = true

key            = "BUCKET_KEY"

dynamodb_table = "DYNAMODB_NAME"

region         = "AWS_REGION"

role_arn       = "IAM_ROLE_ARN"
你也可以使用。
希望能有所帮助。

嘿,我找到了正确的方法:

虽然语法有点复杂,但远程后端支持部分后端初始化。这意味着配置可以包含如下后端块:

terraform {
  backend "remote" { }
}
然后,Terraform可以用如下动态设置的后端配置进行初始化(用适当的值替换ORG和WORKSPACE):


这个答案适用于S3后端?我正在尝试做一个连接到terraform enterprise的远程后端。我不太了解Terraform Enterprise,但我想它的功能与开源版本相同。有了GCP提供程序,instanceI的
terraform{backend“gcs{}}
实际上需要后端是远程terraform enterprise。我觉得必须有一种方法可以使用此设置在TFE上创建一个具有动态名称的工作区。一种选择是进行API调用,但这种远程后端设置非常干净且易于理解。此外,它不会复制工作区和所有这些。我希望能够这样做。谢谢-这是我第一次发现使用文件而不是CLI变量进行TF部分配置的适当示例。
terraform init -backend-config "organization=ORG" -backend-config 'workspaces=[{name="WORKSPACE"}]'