For_each: Cannot apply App Service outbound IP to postgresql firewall

Terraform (and AzureRM Provider) Version

  • azurerm_2.46.1
  • Terraform v0.14.5

Affected Resource

  • azurerm_postgresql_firewall_rule

Description
Applying App Service outbound ip list to azure postgresql firewall using a for_each loop gives the following error:
30: for_each = zipmap(local.ranges,local.appServiceOutboundIP)
The “for_each” value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on.

I am supplying the output outbound_ip_address_list of app service as input to postgresql module.

Terraform Configuration Files

locals {
  appServiceOutboundIP = flatten(var.appServiceOutboundIP)
  ranges = range(0,length(local.appServiceOutboundIP)-1)  
}

resource "azurerm_postgresql_server" "pg_server" {
  name = "psql-${var.shortLocation}-${var.env}-${var.project}"
  location = var.location
  resource_group_name = var.resource_group_name

  administrator_login = var.postgresql_username
  administrator_login_password = var.postgresql_password

  sku_name = var.postgresql_sku_name
  version = var.postgresql_version
  storage_mb = var.postgresql_storage_mb

  backup_retention_days = var.postgresql_backup_retention_days
  geo_redundant_backup_enabled = var.postgresql_geo_redundant_backup_enabled
  auto_grow_enabled = var.postgresql_auto_grow_enabled

  public_network_access_enabled = false
  ssl_enforcement_enabled          = true
  ssl_minimal_tls_version_enforced = "TLS1_2"

  tags = var.tags
}

resource "azurerm_postgresql_firewall_rule" "appServices" {
  for_each = zipmap(local.ranges,local.appServiceOutboundIP)
  name = "AppService${each.key}"
  resource_group_name = var.resource_group_name
  server_name = azurerm_postgresql_server.pg_server.name
  start_ip_address = each.value
 end_ip_address = each.value
}