地形局部远程后端不能包含插值



我正在尝试在 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".

我想用地形做的事吗?

不能将任何变量"${var.workspace_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"

您也可以对 s3 后端使用部分配置。希望它能有所帮助。

嘿,

我找到了正确的方法:

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

terraform {
  backend "remote" { }
}

然后可以使用动态设置的后端配置初始化 Terraform,如下所示(将 ORG 和 WORKSPACE 替换为适当的值(:

terraform init -backend-config "organization=ORG" -backend-config 'workspaces=[{name="WORKSPACE"}]'

相关内容

  • 没有找到相关文章

最新更新