Review App Service managed identity use

Reduce stored long-lived credentials when accessing supported Azure resources.

Description

If your app connects to an Azure service that supports managed identity, it can authenticate without storing a credential in code or settings. Configure sign-in for the app’s users separately.

Potential impact

Long-lived keys in app settings can be exposed and must be replaced wherever the app uses them.

Remediation

In the identity block, choose SystemAssigned or UserAssigned. For UserAssigned, include the managed identity’s Azure resource ID in identity_ids. Grant it only the permissions needed on the target, then switch the app’s connection to managed identity. Test the required operation with the identity before removing old secrets. Revoke credentials once no other workload needs them.

Examples

The AzureRM 3.x azurerm_app_service example only adds a system-assigned identity. Current Linux and Windows Web Apps also support the identity block. An app with no downstream resource access does not need an identity for this purpose.

Before

hcl
resource "azurerm_app_service" "example" {
  name                = "example-app-service"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  app_service_plan_id = azurerm_app_service_plan.example.id
}

After

hcl
resource "azurerm_app_service" "example" {
  name                = "example-app-service"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  app_service_plan_id = azurerm_app_service_plan.example.id

  identity {
    type = "SystemAssigned"
  }
}

References