Search code examples
terraformazure-functionsapim

Can't access attributes on a primitive-typed value (string)


I try to whitelist my apim public ips over my azure function :

apim.tf

data "azurerm_api_management" "main" {
  name                = "my-apim"
  resource_group_name = "my-rg"
}

output "apim_ip" {
  value = data.azurerm_api_management.main.public_ip_addresses
}

terraform output

apim_ip = tolist([
  "1.2.3.4",
])

func.tf

resource "azurerm_linux_function_app" "az_func" {
  name                = var.my_func_name
  resource_group_name = azurerm_resource_group.main.name
  location            = var.location

  storage_account_name       = azurerm_storage_account.main.name
  storage_account_access_key = azurerm_storage_account.main.primary_access_key

  service_plan_id = azurerm_service_plan.azfunc.id

  site_config {
    dynamic "ip_restriction" {
      for_each = data.azurerm_api_management.main.public_ip_addresses
      content {
        ip_address = data.azurerm_api_management.main.public_ip_address_id.value
      }
    }
  }
}

On terraform apply I keep having the error message :

Can't access attributes on a primitive-typed value (string).

What am I doing wrong?


Solution

  • data.azurerm_api_management.main.public_ip_address_id is a string, and therefore you cannot cannot access values from it as if it were a map or object type. I believe you meant to access values on the temporary lambda iterator variable assigned from data.azurerm_api_management.main.public_ip_addresses. In that, the usage and syntax would be:

    ip_address = site_config.value.id
    

    to access the id value from the current data.azurerm_api_management.main.public_ip_addresses attribute.