【问题标题】:create terraform hcl variable as a map创建 terraform hcl 变量作为地图
【发布时间】:2020-12-01 21:26:29
【问题描述】:

我正在使用 tfe 提供程序进行 terraform 工作区自动化,并希望使用以下数据结构中的 custom_tags 创建一个 terraform HCL 变量作为地图。

workspaces = {
  "PROD" = {
    "custom_tags" = {
      "Application" = "demo"
      "EnvironmentType" = "prod"
      "NamePrefix" = "sof"
      "ProductType" = "terraform"
    }
    "env_variables" = {}
    "id" = "alfsdfksf"
    "name" = "PROD"
    "repo" = "github/something"
    "tf_variables" = {}
  }
  "UAT" = {
    "custom_tags" = {
      "Application" = "demo"
      "EnvironmentType" = "uat"
      "NamePrefix" = "sof"
      "ProductType" = "terraform"
    }
    "env_variables" = {}
    "id" = "ws-k7KWYfsdfsdf"
    "name" = "UAT"
    "repo" = "github/otherthing"
    "tf_variables" = {}
  }
}

这是我的资源块

resource "tfe_variable" "terraform_hcl_variables" {
  for_each     = { for w in local.workspaces : w.name => w }
  key          = "custom_tags"
  value        = each.value.custom_tags
  category     = "terraform"
  hcl          = true
  sensitive    = false
  workspace_id = tfe_workspace.main[each.key].id
}

而且,我收到了这个错误。感谢您提供任何帮助来解决此问题。

**each.value.custom_tags is object with 4 attributes
Inappropriate value for attribute "value": string required.**

预期结果

custom_tags 应创建为 HCL 变量

custom_tags = 
{
   "Application" = "demo"
   "EnvironmentType" = "prod"
   "NamePrefix" = "sof"
   "ProductType" = "terraform"
}

【问题讨论】:

    标签: terraform


    【解决方案1】:

    很遗憾,您不能这样做。 value 属性必须是字符串,但您正在尝试为其分配一个“具有 4 个属性的对象”。

    您可以使用jsonencode 将您的each.value.custom_tags 转换为字符串,但这可能不是您想要的。

    【讨论】:

    • 感谢您的反馈。
    • @doubledecker 没问题。你解决了吗?
    • 我设法将所有自定义标签(应用程序、环境、名称前缀、产品类型)添加到工作区中。并使用类似each.value.application 的东西使用值并将值分配为单个字符串。非常基本,多余,但它对我有用。这是来自资源块 value = "{\n\"EnvironmentType\" = \"${each.value.environment}\"\n \"Application\" = \"${each.value.application}\"\n \"ProductType\" = \"${var.application}\"\n \"NamePrefix\" = \"${var.name_prefix}\"\n}" 的值
    • @doubledecker 很高兴听到这个消息。如果你愿意,你可以回答你的问题。或者,如果我的回答有帮助,我们将不胜感激。
    猜你喜欢
    • 2021-11-25
    • 2020-11-21
    • 2022-01-16
    • 2022-12-15
    • 2020-10-09
    • 2021-01-31
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多