I am trying to create a customer manageed key for Azure storage account. I'm currently doing this in a module. Here is the error "Message="The client with object id 'ffa9675d-eef0-4be3-b04b-e7fbc06d4507' does not have authorization to perform action 'Microsoft.Authorization/roleAssignments/write' over scope '/subscriptions/d3977b5d-03d3-4b73-97cb-e97ccdd6eced/resourceGroups/ak_default_rg/providers/Microsoft.KeyVault/vaults/akakptgcakpe/providers/Microsoft.Authorization/roleAssignments/d329030f-f916-435d-9ef4-9b9137064e18' or the scope is invalid. If access was recently granted, please refresh your credentials."
# Add locals block for tags.
locals {
  /* Common tags to be assigned to all resources */
  common_tags = {
    Cost_Status = "critical"
    Created_By  = "ak"
    Managed_By  = "Terraform"
    Project     = "StorageEncyptNet"
  }
}
# Add storage account.
resource "azurerm_storage_account" "storeacc" {
  name                     = "${var.sa_prefix}testsa${var.sa_cname}${var.sa_suffix}"
  location                 = var.sa_location
  resource_group_name      = data.terraform_remote_state.resourcegroup.outputs.resourcegroupname
  account_tier             = var.sa_accounttier
  account_replication_type = var.sa_accountreplicationtype
  identity {
    type = "SystemAssigned"
  }
  tags = local.common_tags
}
# Add access policy to key vault for storage account.
resource "azurerm_key_vault_access_policy" "kvacp_storage" {
  key_vault_id = var.sa_keyvaultid
  tenant_id    = data.azurerm_client_config.current.tenant_id
  object_id    = azurerm_storage_account.storeacc.identity.0.principal_id
  secret_permissions = var.sa_secretpermissions_sto
  key_permissions    = var.sa_keypermissions_sto
  # storage_permissions     = ["Backup", "Delete" ,"DeleteSAS", "Get" ,"GetSAS", "List" ,"ListSAS", "Purge", "Recover" ,"RegenerateKey", "Restore", "Set" ,"SetSAS", "Update"]
}
# Add cuetomer managed key.
# resource "azurerm_storage_account_customer_managed_key" "cmk" {
#   # key_vault_id       = var.sa_keyvaultid
#   key_name           = var.sa_keyvaultname
#   storage_account_id = azurerm_storage_account.storeacc.id
# }
resource "azurerm_storage_account_customer_managed_key" "cmk" {
  key_name           = var.sa_keyvaultname
  key_vault_id       = var.sa_keyvaultid
  storage_account_id = azurerm_storage_account.storeacc.id
  key_version        = "current"
  depends_on = [ azurerm_key_vault_access_policy.kvacp_storage,
                  azurerm_storage_account.storeacc]
}
resource "azurerm_role_assignment" "kvrsa" {
  scope                = var.sa_keyvaultid
  role_definition_name = "Key Vault Crypto Service Encryption User"
  principal_id         = azurerm_storage_account.storeacc.identity.0.principal_id
}
Create a customer managed key
 
                        
The error you're encountering indicates a permissions issue with the Azure service principal (or the user) executing the Terraform script. The error mentioned indicates that the Azure service principal used to run the Terraform script does not have sufficient permissions to assign roles over the specified scope, which in this case is a Key Vault. This operation is necessary to allow the storage account to access the Key Vault for encryption keys.
To resolve this issue, ensure that the service principal has the Role Based Access Control Administrator or Owner role with enough permissions to perform role assignments on the Key Vault. Specifically, it needs
Microsoft.Authorization/roleAssignments/writepermission.My terraform configuration:
Output: