Skip to content

Commit 3203b15

Browse files
Copilotspboyer
andcommitted
Fix Cosmos DB role assignment to use built-in Data Contributor role
Co-authored-by: spboyer <[email protected]>
1 parent 750d7bf commit 3203b15

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
param cosmosAccountName string
2+
param apiPrincipalId string
3+
4+
// Create a role assignment for the API's managed identity to access Cosmos DB
5+
// Using the built-in "Cosmos DB Built-in Data Contributor" role
6+
resource apiCosmosRoleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2024-05-15' = {
7+
name: '${cosmosAccountName}/${guid(apiPrincipalId, cosmosAccountName, '00000000-0000-0000-0000-000000000002')}'
8+
properties: {
9+
roleDefinitionId: '${resourceGroup().id}/providers/Microsoft.DocumentDB/databaseAccounts/${cosmosAccountName}/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002'
10+
principalId: apiPrincipalId
11+
scope: '${resourceGroup().id}/providers/Microsoft.DocumentDB/databaseAccounts/${cosmosAccountName}'
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"metadata": {
5+
"_generator": {
6+
"name": "bicep",
7+
"version": "0.36.1.42791",
8+
"templateHash": "7411539156170877165"
9+
}
10+
},
11+
"parameters": {
12+
"cosmosAccountName": {
13+
"type": "string"
14+
},
15+
"apiPrincipalId": {
16+
"type": "string"
17+
}
18+
},
19+
"resources": [
20+
{
21+
"type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments",
22+
"apiVersion": "2024-05-15",
23+
"name": "[format('{0}/{1}', parameters('cosmosAccountName'), guid(parameters('apiPrincipalId'), parameters('cosmosAccountName'), '00000000-0000-0000-0000-000000000002'))]",
24+
"properties": {
25+
"roleDefinitionId": "[format('{0}/providers/Microsoft.DocumentDB/databaseAccounts/{1}/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002', resourceGroup().id, parameters('cosmosAccountName'))]",
26+
"principalId": "[parameters('apiPrincipalId')]",
27+
"scope": "[format('{0}/providers/Microsoft.DocumentDB/databaseAccounts/{1}', resourceGroup().id, parameters('cosmosAccountName'))]"
28+
}
29+
}
30+
]
31+
}

infra/main.bicep

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,13 @@ module cosmos './app/db-avm.bicep' = {
123123
}
124124
}
125125

126-
// Give the API managed identity access to Cosmos DB
127-
module apiCosmosRoleAssignment 'br/public:avm/ptn/authorization/role-assignment:0.1.0' = {
128-
name: 'api-cosmos-role-assignment'
126+
// Give the API managed identity access to Cosmos DB using built-in Data Contributor role
127+
module apiCosmosRoleAssignment './app/cosmos-role-assignment.bicep' = {
128+
name: 'api-cosmos-role'
129129
scope: rg
130130
params: {
131-
principalId: api.outputs.SERVICE_API_IDENTITY_PRINCIPAL_ID
132-
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')
133-
resourceId: cosmos.outputs.resourceId
131+
cosmosAccountName: cosmos.outputs.accountName
132+
apiPrincipalId: api.outputs.SERVICE_API_IDENTITY_PRINCIPAL_ID
134133
}
135134
}
136135

0 commit comments

Comments
 (0)