Parameterize Block in Terraform

I am creating a module on top of the azurerm_app_service. I don’t want to grow the input variable list to collect all the possible values for the bigger blocks eg site_config .

variable var1{}
variable var2{}
..
..
..
variable varN{}

resource "azurerm_app_service" "example" {
  name                = "example-app-service"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  app_service_plan_id = azurerm_app_service_plan.example.id

    site_config {
        key1 = var.var1
        key2 = var.var2
        ...
        ...
        keyN = var.varN
        
      }

  app_settings = {
    "SOME_KEY" = "some-value"
  }
}

I want to parametrize the entire block itself.

something like this

variable var_block{
  type = map()
}

resource "azurerm_app_service" "example" {
  name                = "example-app-service"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  app_service_plan_id = azurerm_app_service_plan.example.id

      site_config {
        var.var_block   
      }

  app_settings = {
    "SOME_KEY" = "some-value"
  }
}

Looking for something like **kwargs in the python world.

Is it possible to achieve?

You should be able to just pass the map variable directly to the “site_config” field, without the curly brackets, like this:

site_config = var.var_block

site_config is a block and it doesnt support assignment with =.

Hello,

You can try using dynamic block for Site_Config

dynamic “site_config” {
for_each = each.value.config
content {
key1 = config.value.key1
}
}

config should be a variable with map of objects inside your app-service while using for_each

Thank you! Not sure whether this will work for my requirement.

I want the key also to be dynamic. Something like this,

  dynamic "sku" {
    for_each = var.site_config
    content {
      each.key = each.value
    }
}

where var.site_config is a map.

Hi @apparentlymart,

Is this something you can help me with? I also found a similar question in stack overflow cloudflare - Attributes as Blocks with dynamic key and value in Terraform - Stack Overflow. Is there any next way to overcome this?

Hello @venkadeshwarank @support is there a solution for this usecase ?

No, the Terraform language does not have support for parameterising the keys of blocks dynamically.