azure 将值从Terraform动态加载到Map

ndh0cuux  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(86)

我正在尝试将一些外部数据从json文件加载到Terraform to merge到appSettings map
它把它作为一个元组加载进来--无论我做什么转换,我都不能从中得到一个Map:
Call to function "merge" failed: arguments must be maps or objects, got "tuple".
Json文件

[
  {
    "appCode": "value",
    "containerName": "value",
    "databaseName": "value",
    "referer": "bvalue",
    "shortCode": "value",
    "user": "value"
  },
  {
    "appCode": "value",
    "containerName": "value",
    "databaseName": "value",
    "referer": "value",
    "shortCode": "value",
    "user": "value"
  }
]

当地人:

customerSettings = jsondecode(file("vars/${var.environment}.json"))

  customerAppSettingsFromJson = {
    for index, externalCustomer in local.customerSettings :
    externalCustomer => {
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__AppCode"       = "${externalCustomer.appCode}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__ContainerName" = "${externalCustomer.containerName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__DatabaseName"  = "${externalCustomer.databaseName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__Referer"       = "${externalCustomer.databaseName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__ShortCode"     = "${externalCustomer.shortCode}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__User"          = "${externalCustomer.user}"
    }
  }

Main.tf appSettings块,已推断出一个,一个来自vars,另一个来自json

app_settings = merge({}, var.app_settings, local.customerAppSettingsFromJson)
nfs0ujit

nfs0ujit1#

您已经非常接近解决方案,但下面是如何将对象列表转换为贴图:

customerSettings = jsondecode(file("vars/${var.environment}.json"))

  customerAppSettingsFromJson = {
    for index, externalCustomer in local.customerSettings :
    index => {
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__AppCode"       = "${externalCustomer.appCode}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__ContainerName" = "${externalCustomer.containerName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__DatabaseName"  = "${externalCustomer.databaseName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__Referer"       = "${externalCustomer.databaseName}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__ShortCode"     = "${externalCustomer.shortCode}"
      "DynamicCosmosDbSettings__CosmosHostSettings__${index}__User"          = "${externalCustomer.user}"
    }
  }

相关问题