Unable to use map(any) in dynamic block for azurerm_api_management_api

I am trying to use map in dynamic block for azurerm_api_management_api. Getting error. Tried different ways, but unable to achieve.

Beow is my variables and resource:

variable "api_names" {
    description = "(optional) describe your variable"
    type    = map(any)
    default = {
        "example-api" = {
            "path" = "example"
            "revision" = 1
            "protocols" = ["https"]
            "content_format" = null
            "content_value" = null
        },
        "example-api2" = {
            "path" = "example2"
            "revision" = 2
            "protocols" = ["https"]
            "content_format" = null
            "content_value" = null
        }
    }
}

resource "azurerm_api_management_api" "example" {
  for_each            = var.api_names
  name                = each.key
  resource_group_name = azurerm_resource_group.example.name
  api_management_name = azurerm_api_management.example.name
  display_name        = each.key
  revision            = each.value.revision
  path                = each.value.path
  protocols           = each.value.protocols

  dynamic "import" {
    for_each = var.api_names
    content {
        content_format = each.value.content_format
        content_value  = each.value.content_value
    }
  }
}

I have tried for_each = var.api_names[*], But no luck.

Getting Error:


Error: import: attribute supports 1 item maximum, config has 2 declared

  on modules\apim\resource.tf line 20, in resource "azurerm_api_management_api" "example":
  20: resource "azurerm_api_management_api" "example" {



Error: import: attribute supports 1 item maximum, config has 2 declared

  on modules\apim\resource.tf line 20, in resource "azurerm_api_management_api" "example":
  20: resource "azurerm_api_management_api" "example" {

Is this possible to use as above or am I doing anything wrong.

TIA

The issue here is that azurerm_api_management_api only supports one import block per resource, and you are trying to specify two. The documentation isn’t completely explicit about this, but it does refer to “an import block”, not a “a list of import blocks”. The source code shows that only one is supported, due to MaxItems: 1.

I’m not familiar with this resource or this API, so I can’t suggest a workaround. Hope this helps to explain the error you’re seeing, though!

Thanks for the confirmation, I had a doubt, but not sure