【问题标题】:Merging map in Terraform module definition在 Terraform 模块定义中合并地图
【发布时间】:2019-07-09 08:03:21
【问题描述】:

我在 Terraform 中将 Azure Function App 公开为一个模块,我希望该模块允许用户扩展配置参数:

resource "azurerm_function_app" "test" {
  name                      = "${var.prefix}-listener"
  resource_group_name       = "${var.resource_group_name}"
  location                  = "${var.resource_group_location}"
  app_service_plan_id       = "${var.app_service_plan_id}"
  storage_connection_string = "${var.storage_account_connection_string}"

  app_settings = {
    HASH = "${data.archive_file.test.output_base64sha256}"
    WEBSITE_USE_ZIP = "https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}"
  }

  site_config {
    always_on = true
  }
}

但是,在该示例中,app_settings 是固定的,我希望它可以扩展此地图。比如:

  app_settings = ${merge({
    HASH = "${data.archive_file.test.output_base64sha256}"
    WEBSITE_USE_ZIP = "https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}"
  }, ${var.app_settings})}

我从阅读merge function 中得到了这个想法。但是我收到 Invalid expression 错误。

这里的正确语法是什么?

【问题讨论】:

  • 您的${var.app_settings} 定义是什么?是地图吗?
  • @NancyXiong 是的
  • 试试这个:app_settings = "${merge({ "HASH" = "${data.archive_file.test.output_base64sha256}", "WEBSITE_USE_ZIP" = "https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}" }, "${var.app_settings}")}"

标签: terraform terraform-provider-azure


【解决方案1】:

看起来{} 导致字符串插值出现问题。您可以将代码更改为,

app_settings = "${merge(
    map("HASH","${data.archive_file.test.output_base64sha256}"),
    map("WEBSITE_USE_ZIP","https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}"), 
    var.app_settings}"

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    map 已弃用

    │ Call to function "map" failed: the "map" function was deprecated in
    │ Terraform v0.12 and is no longer available; use tomap({ ... }) syntax to
    │ write a literal map.
    

    所以现在你可以使用

    app_settings = merge(
        tomap({ "HASH" = "${data.archive_file.test.output_base64sha256}", "WEBSITE_USE_ZIP" = "https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}"}), 
        var.app_settings)
    
    

    您也可以通过以下方式实现:

    
    locals {
     app_settings = {
       HASH = "${data.archive_file.test.output_base64sha256}"
       WEBSITE_USE_ZIP = "https://${var.storage_account_name}.blob.core.windows.net/${azurerm_storage_container.test.name}/${azurerm_storage_blob.test.name}${data.azurerm_storage_account_sas.test.sas}"
     }
    }
    

    然后

    app_settings = merge(local.app_settings, var.app_settings)
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2021-05-16
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2022-11-09
      • 2019-11-09
      • 2022-01-13
      相关资源
      最近更新 更多