casparjespersen
casparjespersen

Reputation: 3830

Merging map in Terraform module definition

I am exposing an Azure Function App as a module in Terraform, in which I want the module allowing the user to extend a configuration parameter:

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
  }
}

However, in that example the app_settings is fixed, and I would like it to be possible extending this map. Something like:

  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})}

I got that idea from reading the merge function. However I get an Invalid expression error.

What is the correct syntax here?

Upvotes: 0

Views: 934

Answers (2)

It looks like { and } are causing problems with the string interpolation. You could change your code to,

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}"

Hopefully that should solve your problem.

Upvotes: 2

Krzysztof Madej
Krzysztof Madej

Reputation: 40553

map was deprecated

│ 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.

so now you can use

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)

You can also achieve this in following way:


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}"
 }
}

and then

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

Upvotes: 0

Related Questions