在 Terraform 模块定义中合并地图
Merging map in Terraform module definition
我在 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 得到了这个想法。但是我收到 无效表达式 错误。
此处正确的语法是什么?
看起来 {
和 }
导致字符串插值出现问题。您可以将代码更改为,
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}"
希望这能解决您的问题。
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)
我在 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 得到了这个想法。但是我收到 无效表达式 错误。
此处正确的语法是什么?
看起来 {
和 }
导致字符串插值出现问题。您可以将代码更改为,
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}"
希望这能解决您的问题。
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)