Creating multiple Azure Enterprise service applications

I am attempting to create multiple enterprise service applications in using a variable object map, although I can create the App registrations, looping through the same data again to create the enterprise application is constantly throwing an error.

variables.tfvars:

app_service_map = {
“sh3-app-sp-uksouth-flow” = {
name = “sh3-app-sp-uksouth-flow”
serviceplan = “s3-plan-sp-uksouth-0002”
iprestrictions = “ip_restriction { ip_address = ‘81.145.174.78’ }”
appreg_name = “sh3-app-sp-uksouth-flow”
}
“sh3-app-sp-uksouth-hook” = {
name = “sh3-app-sp-uksouth-hook”
serviceplan = “s3-plan-sp-uksouth-0002”
iprestrictions = “”
appreg_name = “sh3-app-sp-uksouth-hook”
}
}

serviceprincipal.tf:

data “azuread_client_config” “current” {}

resource “azuread_application” “appr-uksouth” {
for_each = var.app_service_map

display_name = “${each.value.name}”
owners = [data.azuread_client_config.current.object_id]
}

resource “azuread_service_principal” “entapp-uksouth” {
for_each = var.app_service_map

application_id = azuread_application.appr-uksouth[“.${each.value[“name”]}”].application_id
app_role_assignment_required = false
owners = [data.azuread_client_config.current.object_id]

feature_tags {
enterprise = true
}
}

This is the error:

│ Error: Invalid index

│ on serviceprincipal.tf line 19, in resource “azuread_service_principal” “entapp-uksouth”:
│ 19: application_id = azuread_application.appr-uksouth[“.${each.value[“name”]}”].application_id
│ ├────────────────
│ │ azuread_application.appr-uksouth is object with 2 attributes
│ │ each.value[“name”] is “app-sp-uksouth-integration”

│ The given key does not identify an element in this collection value.

I’m assuming it is because I am not referencing the application_id for the service_principal correctly. If I remove the azuread_service_principal section, I can create the azuread_application without issue.

Thanks in advance