I would like to create a User assigned identity with Terraform, that have the read permission on an already existing azure storage account. How can I grant that with Terraform? I am creating the whole infrastructure with the service principal I'm using right now. So, it's unlikely that it's a permission issue.
# My user assigned identity:
resource "azurerm_user_assigned_identity" "user_assigned_identity" {
name = "${var.resource_prefix}useridentity"
location = var.location
resource_group_name = var.resource_group_name
}
# and the role assignment to this identity
resource "azurerm_role_assignment" "example" {
scope = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource-group-name"
role_definition_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
principal_id = azurerm_user_assigned_identity.user_assigned_identity.principal_id
}
But I am having this error:
Error: authorization.RoleAssignmentsClient#Create: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error. Status=403 Code="AuthorizationFailed" Message="The client 'xxxxxxxxxxxxxxxxxxx' with object id 'xxxxxxxxxxxxxxxxxxx' does not have authorization to perform action 'Microsoft.Authorization/roleAssignments/write' over scope '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource-group-name/providers/Microsoft.Authorization/roleAssignments/"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"' or the scope is invalid. If access was recently granted, please refresh your credentials."
Any idea what I'm doing wrong?




