Review customer-managed keys for Azure Storage

Choose encryption keys that meet organizational key-management requirements.

Description

Azure Storage encrypts data with Microsoft-managed keys by default. Use a customer-managed key for a diagnostic-log account when your organization needs control over key access and rotation.

Potential impact

If the account’s managed identity loses access to the key, the log data may become unavailable.

Remediation

Use a supported RSA or RSA-HSM key in a vault with soft delete and purge protection enabled. Plan for key recovery and rotation, and grant the managed identity get, wrapKey and unwrapKey permissions on the key. Manage the association using either the customer_managed_key block or a separate azurerm_storage_account_customer_managed_key resource. Verify the key associated with the deployed account.

Examples

The example uses a user-assigned identity and the separate association resource. On the account, include customer_managed_key in lifecycle.ignore_changes to prevent conflicting updates. Supply the diagnostic log categories to collect.

Before

hcl
resource "azurerm_storage_account" "logs" {
  name                     = "examplediagnosticlogs"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
}

resource "azurerm_monitor_diagnostic_setting" "example" {
  name               = "subscription-diagnostics"
  target_resource_id = data.azurerm_subscription.current.id
  storage_account_id = azurerm_storage_account.logs.id
}

After

hcl
resource "azurerm_storage_account" "logs" {
  name                     = "examplediagnosticlogs"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"

  identity {
    type         = "UserAssigned"
    identity_ids = [azurerm_user_assigned_identity.storage.id]
  }
}

resource "azurerm_storage_account_customer_managed_key" "logs" {
  storage_account_id         = azurerm_storage_account.logs.id
  key_vault_key_id           = azurerm_key_vault_key.storage.id
  user_assigned_identity_id  = azurerm_user_assigned_identity.storage.id
}

resource "azurerm_monitor_diagnostic_setting" "example" {
  name               = "subscription-diagnostics"
  target_resource_id = data.azurerm_subscription.current.id
  storage_account_id = azurerm_storage_account.logs.id
}

References