How to Give Your Account Access to Azure Cosmos DB Data with the Azure CLI
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
- bash / zsh
- PowerShell
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>
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>
| Part | What 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:
az ad signed-in-user show --query id --output tsv
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
- bash / zsh
- PowerShell
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"
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:
{
"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"
}

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:
- bash / zsh
- PowerShell
az cosmosdb sql role assignment list \
--resource-group rg-maf-servicedesk \
--account-name cosmos-howto-4224 \
--query "[].{role:roleDefinitionId, scope:scope}" \
--output table
az cosmosdb sql role assignment list `
--resource-group rg-maf-servicedesk `
--account-name cosmos-howto-4224 `
--query "[].{role:roleDefinitionId, scope:scope}" `
--output table
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
| Mistake | What happens | Do this instead |
|---|---|---|
| Adding a role in the account's Access control (IAM) | That's an Azure role, the control plane, and data reads still fail | Assign a data role with az cosmosdb sql role assignment create |
Scoping every assignment to / | The identity can read every container in the account | Scope it to the database or container it needs |
| Giving Data Contributor to something that only reads | It can also change and delete items | Give it Data Reader, …0001 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| 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 container | Check 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
- Connect to Azure Cosmos DB for NoSQL using role-based access control and Microsoft Entra ID — control plane and data plane, scopes and the CLI commands.
- Data plane security reference: built-in roles — what each built-in role allows.
- az cosmosdb sql role assignment — the command's reference.
