Retrieve principal ID of an Azure appservice MSI

Hi, I was previously using for_each for appservice creation with which I was able to retrieve principal_id of all appservices to use in access_policy resource. But when I changed access_policy resource to a conditional resource by using count, I am having trouble retrieving principal_id of identity since I cannot use splat on a map.
I ran out of ideas and wanted to check if anyone has previously successfully retrieved similar values or have any inputs on how I can retrieve principal_Id.

resource “azurerm_app_service” “webapp” {
for_each = var.appservice
name = each.value
identity {
type = “SystemAssigned”
resource “azurerm_key_vault_access_policy” “azaccesspolicy” {
count = var.keyvault_name != null ? local.appservicecount : 0
#object_id = lookup(each.value.appservice.identity[0],“principal_id”)
object_id =??
secret_permissions = [“get”]
My state file looks something like below
“name”: “webapp”,
“each”: “map”,
“provider”: “provider.azurerm”,
“instances”: [
“identity”: [
“identity_ids”: ,
“principal_id”: “011111-8888-4a76-0987-1234567890”,
“type”: “SystemAssigned”

Just in case if someone run into similar issue, I was able to achieve it by using for to populate principal_ids to a list and then using count.index on the list to retrieve the id.
locals { principal_id = [for i in azurerm_app_service.webapp : lookup(i.identity[0], "principal_id")] }
And in the access_policy resource, I used
object_id = local.principal_id[count.index]