Skip to content

Instantly share code, notes, and snippets.

@bennage
Last active April 28, 2021 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennage/56633b67562221221605c0678948cc77 to your computer and use it in GitHub Desktop.
Save bennage/56633b67562221221605c0678948cc77 to your computer and use it in GitHub Desktop.
assigning a role for storage access

context

I need to create a managed identity with the Storage Blob Data Contributor for a storage account. This template fails with

Deployment failed. Correlation ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. {
  "error": {
    "code": "PrincipalNotFound",
    "message": "Principal xxxxxxxxxxx does not exist in the directory xxxxx-xxxx-xxxx-xxxx-xxxxxxxx."
  }
}
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"guid": {
"type": "string",
"defaultValue": "[newGuid()]"
}
},
"variables": {
"storageAccountName": "[uniqueString(resourceGroup().id)]",
"managedIdentityName": "[concat('test-identity-', parameters('guid'))]",
"roleAssignmentName": "[guid(concat(resourceGroup().id),variables('managedIdentityName'), 'somerole')]",
"roleDefinitionId": "[concat(resourceGroup().id, '/providers/Microsoft.Authorization/roleDefinitions/', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')]"
},
"resources": [
{
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
"name": "[variables('managedIdentityName')]",
"apiVersion": "2015-08-31-preview",
"location": "[resourceGroup().location]"
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-04-01",
"name": "[variables('storageAccountName')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
},
{
"name": "[concat(variables('storageAccountName'), '/Microsoft.Authorization/', variables('roleAssignmentName'))]",
"type": "Microsoft.Storage/storageAccounts/providers/roleAssignments",
"apiVersion": "2021-04-01-preview",
"dependsOn": [
"[variables('managedIdentityName')]",
"[variables('storageAccountName')]"
],
"properties": {
"roleDefinitionId": "[variables('roleDefinitionId')]",
"principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities',variables('managedIdentityName')), '2018-11-30').principalId]"
}
}
],
"outputs": {
}
}
@bennage
Copy link
Author

bennage commented Apr 28, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment