Skip to main content

How to Give Your Account Access to Azure Cosmos DB Data with the Azure CLI

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

With key-based authentication turned off, an Azure Cosmos DB account only accepts Microsoft Entra ID identities that have a Cosmos DB data role, and that includes the person who created the account. One Azure CLI command gives your account that role, for the whole account, one database or one container.

Azure Cosmos DB has two kinds of access:

  • Control plane: managing the account, its databases and its containers. Your Azure role, such as Owner or Contributor on the subscription, gives you this.
  • Data plane: reading and writing the items inside the containers. This needs one of Cosmos DB's own roles, and Microsoft's documentation is plain about where to manage them: Managing data plane role-based access control isn't supported in the Azure portal.

So the portal lets you create a container, but not read it, and the Azure CLI gives you the role. The command is az cosmosdb sql role assignment create.

Syntax​

Syntax
az cosmosdb sql role assignment create \
--resource-group <resource-group> \
--account-name <account-name> \
--role-definition-id <role-definition-id> \
--principal-id <principal-id> \
--scope <scope>
PartWhat it is
<resource-group>The resource group that holds the account
<account-name>The Azure Cosmos DB for NoSQL account
<role-definition-id>The role. The two built-in roles have the same IDs in every account: 00000000-0000-0000-0000-000000000001 is Cosmos DB Built-in Data Reader, which only reads, and 00000000-0000-0000-0000-000000000002 is Cosmos DB Built-in Data Contributor, which reads and writes
<principal-id>The object ID of the identity that gets the role: your own account, or an app's managed identity
<scope>What the role covers: / for every database in the account, /dbs/<database> for one database, or /dbs/<database>/colls/<container> for one container

Creating the assignment is itself a management operation: your Azure role on the account needs the Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments/write permission, which the Owner and Contributor roles include.

The Example​

A small notes app keeps its notes in the database notesdb of the account cosmos-howto-4224, which has key-based authentication turned off. The app runs on my computer with my Azure CLI sign-in, so my account needs to read and write notes, and nothing else: Data Contributor, scoped to notesdb.

Step 1: Find Your Object ID​

Every Microsoft Entra ID identity has an object ID. The Azure CLI shows yours:

Terminal
az ad signed-in-user show --query id --output tsv
Output
00000000-0000-0000-0000-000000000000

az ad signed-in-user show reads the account you signed in with, --query id keeps only its object ID, and --output tsv prints it as plain text, without quotes. I replaced mine with zeros. You don't need to copy it: the next command runs this one inside itself.

Step 2: Assign the Role​

Terminal
az cosmosdb sql role assignment create \
--resource-group rg-maf-servicedesk \
--account-name cosmos-howto-4224 \
--role-definition-id 00000000-0000-0000-0000-000000000002 \
--principal-id "$(az ad signed-in-user show --query id --output tsv)" \
--scope "/dbs/notesdb"

Use your own resource group, account and database. "$(...)" runs the command from Step 1 and puts its output, your object ID, in its place, in both bash and PowerShell. The output is the new role assignment; I shortened the long IDs to the part after the account:

Output
{
"id": "…/databaseAccounts/cosmos-howto-4224/sqlRoleAssignments/00000000-0000-0000-0000-000000000000",
"name": "00000000-0000-0000-0000-000000000000",
"principalId": "00000000-0000-0000-0000-000000000000",
"resourceGroup": "rg-maf-servicedesk",
"roleDefinitionId": "…/databaseAccounts/cosmos-howto-4224/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002",
"scope": "…/databaseAccounts/cosmos-howto-4224/dbs/notesdb",
"systemData": null,
"type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments"
}

macOS-style terminal window showing az cosmosdb sql role assignment create with the resource group rg-maf-servicedesk, the account cosmos-howto-4224, the role definition ID ending in 2, the principal ID from az ad signed-in-user show and the scope /dbs/notesdb, and its JSON output with the new assignment's roleDefinitionId ending in sqlRoleDefinitions/00000000-0000-0000-0000-000000000002 and the scope ending in dbs/notesdb

Read it from the bottom: the scope ends in dbs/notesdb, the roleDefinitionId ends in …0002, Data Contributor, and the principalId is your object ID.

Step 3: Check the Assignment​

List the account's data role assignments:

Terminal
az cosmosdb sql role assignment list \
--resource-group rg-maf-servicedesk \
--account-name cosmos-howto-4224 \
--query "[].{role:roleDefinitionId, scope:scope}" \
--output table
Output (the IDs shortened)
Role Scope
-------------------------------------------------------------------------------------------- ------------------------------------------------
…/databaseAccounts/cosmos-howto-4224/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002 …/databaseAccounts/cosmos-howto-4224/dbs/notesdb

One assignment, Data Contributor on notesdb. From here, Data Explorer can show the database's items to you, and an app that signs in as you can read and write them, as How to Connect to Azure Cosmos DB with Microsoft Entra ID in C# does.

Common Mistakes​

MistakeWhat happensDo this instead
Adding a role in the account's Access control (IAM)That's an Azure role, the control plane, and data reads still failAssign a data role with az cosmosdb sql role assignment create
Scoping every assignment to /The identity can read every container in the accountScope it to the database or container it needs
Giving Data Contributor to something that only readsIt can also change and delete itemsGive it Data Reader, …0001

Troubleshooting​

SymptomLikely causeFix
Request blocked by Auth … does not have required RBAC permissions to perform action [Microsoft.DocumentDB/databaseAccounts/readMetadata]The identity has no data role (Fixed)Assign one with the command above
The same error, with a container in on resource [...]The role's scope doesn't include that containerCheck the scope against the database and container in the message

FAQ​

Which role should an app get?​

The smallest one that works: Data Reader if it only reads, Data Contributor if it writes, scoped to its own database or container. Give the app's managed identity its own assignment, with the identity's principal ID as --principal-id.

Does the role also let me manage the account?​

No. Data roles cover the items in containers and the operations on them, such as queries. Microsoft's troubleshooting guide: When you only have data plane access through Microsoft Entra role-based access control (RBAC), you can't perform control plane operations. Creating databases and containers, or changing settings such as the time to live, stays with your Azure role.

Conclusion​

To use the data in an Azure Cosmos DB account with keys turned off, give the identity a data role with az cosmosdb sql role assignment create: the role ID, …0001 for Data Reader or …0002 for Data Contributor, the identity's object ID, and a scope, /, /dbs/<database> or /dbs/<database>/colls/<container>. The portal can't do this, and az cosmosdb sql role assignment list shows what you've assigned.

Additional Resources​