无法访问原始类型值(字符串)的属性

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

我尝试将我的 apim public ips 列入我的 azure 函数的白名单:

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
}

地形输出

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

terraform apply 我一直收到错误消息:

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

我做错了什么?

data.azurerm_api_management.main.public_ip_address_id 是一个字符串,因此您不能像访问 mapobject 类型一样访问它的值。我相信您打算访问从 data.azurerm_api_management.main.public_ip_addresses 分配的临时 lambda 迭代器变量上的值。其中,用法和语法为:

ip_address = site_config.value.id

从当前 data.azurerm_api_management.main.public_ip_addresses 属性访问 id 值。