azureterraformazure-functionsazure-rm-templateazure-rm

Add list of configs in app_settings configuration block


In terraform I declared a function_app resource:

resource "azurerm_windows_function_app" "func" {
  name                = local.func_app_name
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  
  service_plan_id = azurerm_service_plan.asp.id
  
  storage_account_name       = azurerm_storage_account.sa.name
  storage_account_access_key = azurerm_storage_account.sa.primary_access_key

  app_settings = {
    key1 = "value1"
    key2 = "value2"
  }

}

But i have a new requirements to add a new settings to app_settings.

New settings are placed in list because i iterate over it in another place in a code:

locals {
     publish_topics = {
       topicTest = "topic1",
       topicDev  = "topic2"
  }
}

How to add static values and values from a list from locals to app_settings config?


Solution

  • Add list of configs in app_settings configuration block:

    Use below terraform code to achieve your requirement.

    locals {
        publish_topics = {
           topic1 = "topic1"
           topic2 = "topic2"
      }
    }
    provider "azurerm" {
      features {}
    }
    
    data "azurerm_resource_group" "example" {
      name     = "xxxx"
    }
    
    resource "azurerm_service_plan" "example" {
      name                = "jahservice-plan"
      resource_group_name = azurerm_resource_group.example.name
      location            = azurerm_resource_group.example.location
      os_type             = "Windows"
      sku_name            = "Y1"
    }
    resource "azurerm_storage_account" "sa" {
      name                     = "xxx"
      resource_group_name      = azurerm_resource_group.example.name
      location                 = azurerm_resource_group.example.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    resource "azurerm_windows_function_app" "func" {
      name                = "newfuncj"
      location            = data.azurerm_resource_group.rg.location
      resource_group_name = data.azurerm_resource_group.rg.name
      
      service_plan_id = azurerm_service_plan.example.id
      
      storage_account_name       = azurerm_storage_account.sa.name
      storage_account_access_key = azurerm_storage_account.sa.primary_access_key
    
      app_settings = merge(
        {
          key1 = "value1"
          key2 = "value2"
        },
        { for topic in local.publish_topics : 
              "topic:${topic}" => topic 
        }
      )
    }
    

    enter image description here

    enter image description here

    Refer here for relevant usage of locals block and retrieving it using foreach.

    Update:

    You can also use

    for key, value in local.publish_topics : key => value as suggested by @michasaucer.