How to create Azure Cosmos DB using Azure CLI?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss how to create an account, database, and container in Azure Cosmos DB using Azure CLI.
How to create Azure Cosmos DB using Azure CLI?
Prerequisites:
Azure CLI installed and configured
Step 1: Create a new resource group for Cosmos DB.
1 2 3 4 |
## Create a resource group az group create \ --name AzureCosmosDBRG \ --location eastus |
Step 2: Create an Azure Cosmos DB account using Azure CLI.
1 2 3 4 5 6 7 8 9 10 |
## Check if Cosmos account name already exists az cosmosdb check-name-exists \ --name cloudaffaire-azure-cosmosdb-account ## returns false ## Create a Cosmos account for SQL API az cosmosdb create \ --name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG |
Step 3: Get details on the Azure Cosmos DB account using Azure CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
## List all Azure Cosmos accounts under a resource group az cosmosdb list \ --resource-group AzureCosmosDBRG ## Get details on Azure CosmosDB account az cosmosdb show \ --name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG ## Returns ## { ## "apiProperties": null, ## "backupPolicy": { ## "periodicModeProperties": { ## "backupIntervalInMinutes": 240, ## "backupRetentionIntervalInHours": 8, ## "backupStorageRedundancy": "Geo" ## }, ## "type": "Periodic" ## }, ## "capabilities": [], ## "connectorOffer": null, ## "consistencyPolicy": { ## "defaultConsistencyLevel": "Session", ## "maxIntervalInSeconds": 5, ## "maxStalenessPrefix": 100 ## }, ## "cors": [], ## "databaseAccountOfferType": "Standard", ## "defaultIdentity": "FirstPartyIdentity", ## "disableKeyBasedMetadataWriteAccess": false, ## "documentEndpoint": "https:// ## "enableAnalyticalStorage": false, ## "enableAutomaticFailover": false, ## "enableCassandraConnector": null, ## "enableFreeTier": false, ## "enableMultipleWriteLocations": false, ## "failoverPolicies": [ ## { ## "failoverPriority": 0, ## "id": " ## "locationName": "East US" ## } ## ], ## "id": "/subscriptions/ ## "identity": { ## "principalId": null, ## "tenantId": null, ## "type": "None", ## "userAssignedIdentities": null ## }, ## "ipRules": [], ## "isVirtualNetworkFilterEnabled": false, ## "keyVaultKeyUri": null, ## "kind": "GlobalDocumentDB", ## "location": "East US", ## "locations": [ ## { ## "documentEndpoint": "https:// ## "failoverPriority": 0, ## "id": " ## "isZoneRedundant": false, ## "locationName": "East US", ## "provisioningState": "Succeeded" ## } ## ], ## "name": " ## "networkAclBypass": "None", ## "networkAclBypassResourceIds": [], ## "privateEndpointConnections": null, ## "provisioningState": "Succeeded", ## "publicNetworkAccess": "Enabled", ## "readLocations": [ ## { ## "documentEndpoint": "https:// ## "failoverPriority": 0, ## "id": " ## "isZoneRedundant": false, ## "locationName": "East US", ## "provisioningState": "Succeeded" ## } ## ], ## "resourceGroup": "AzureCosmosDBRG", ## "systemData": { ## "createdAt": "2022-06-06T14:39:46.5534926Z" ## }, ## "tags": {}, ## "type": "Microsoft.DocumentDB/databaseAccounts", ## "virtualNetworkRules": [], ## "writeLocations": [ ## { ## "documentEndpoint": "https:// ## "failoverPriority": 0, ## "id": " ## "isZoneRedundant": false, ## "locationName": "East US", ## "provisioningState": "Succeeded" ## } ## ] ## } |
Step 4: Get Azure Cosmos DB connection string using Azure CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## Get Azure CosmosDB connection strings az cosmosdb list-connection-strings \ --name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG ## Returns ## { ## "connectionStrings": [ ## { ## "connectionString": "AccountEndpoint=https:// ## "description": "Primary SQL Connection String" ## }, ## { ## "connectionString": "AccountEndpoint=https:// ## "description": "Secondary SQL Connection String" ## }, ## { ## "connectionString": "AccountEndpoint=https:// ## "description": "Primary Read-Only SQL Connection String" ## }, ## { ## "connectionString": "AccountEndpoint=https:// ## "description": "Secondary Read-Only SQL Connection String" ## } ## ] ## } |
Step 5: Get Azure Cosmos DB keys using Azure CLI
1 2 3 4 5 6 7 8 9 10 11 12 |
## List Azure CosmosDB keys az cosmosdb list-keys \ --name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG ## returns ## { ## "primaryMasterKey": " ## "primaryReadonlyMasterKey": " ## "secondaryMasterKey": " ## "secondaryReadonlyMasterKey": " ## } |
Step 6: Create a database of type SQL API in Azure Cosmos DB using Azure CLI.
1 2 3 4 5 |
## Create a SQL API database az cosmosdb sql database create \ --account-name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG \ --name AzureCosmosDBDB |
Step 7: Get details on the Azure Cosmos DB database using Azure CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## List Azure CosmodDB databases az cosmosdb sql database list \ --account-name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG ## Get details on Azure CosmosDB database az cosmosdb sql database show \ --account-name cloudaffaire-azure-cosmosdb-account \ --name AzureCosmosDBDB \ --resource-group AzureCosmosDBRG ## Returns ## { ## "id": "/subscriptions/ ## "location": null, ## "name": "AzureCosmosDBDB", ## "options": null, ## "resource": { ## "_self": "dbs/KeFtBB==/", ## "id": "AzureCosmosDBDB" ## }, ## "resourceGroup": "AzureCosmosDBRG", ## "tags": null, ## "type": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases" ## } |
Step 8: Create a container in Azure Cosmos DB using Azure CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
## Define the index policy for the container, include spatial and composite indexes cat << EOF > indexpolicy.json { "indexingMode": "consistent", "includedPaths": [ {"path": "/*"} ], "excludedPaths": [ { "path": "/headquarters/employees/?"} ], "spatialIndexes": [ {"path": "/*", "types": ["Point"]} ], "compositeIndexes":[ [ { "path":"/name", "order":"ascending" }, { "path":"/age", "order":"descending" } ] ] } EOF ## Create a SQL API container az cosmosdb sql container create \ --account-name cloudaffaire-azure-cosmosdb-account \ --resource-group AzureCosmosDBRG \ --database-name AzureCosmosDBDB \ --name AzureCosmosDBContainer \ --partition-key-path "/zipcode" \ --throughput 400 \ --idx @indexpolicy.json |
Step 9: Get details on Azure Cosmos DB containers using Azure CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
## Get details on Azure Cosmos DB containers az cosmosdb sql container show \ --account-name cloudaffaire-azure-cosmosdb-account \ --database-name AzureCosmosDBDB \ --name AzureCosmosDBContainer \ --resource-group AzureCosmosDBRG ## Returns ## { ## "id": "/subscriptions/ ## "location": null, ## "name": "AzureCosmosDBContainer", ## "options": null, ## "resource": { ## "_conflicts": "conflicts/", ## "_docs": "docs/", ## "_self": "dbs/KeFtCC==/colls/KeKtBM6dggw=/", ## "_sprocs": "sprocs/", ## "_triggers": "triggers/", ## "_udfs": "udfs/", ## "analyticalStorageTtl": null, ## "conflictResolutionPolicy": { ## "conflictResolutionPath": "/_ts", ## "conflictResolutionProcedure": "", ## "mode": "LastWriterWins" ## }, ## "defaultTtl": null, ## "geospatialConfig": { ## "type": "Geography" ## }, ## "id": "AzureCosmosDBContainer", ## "indexingPolicy": { ## "automatic": true, ## "compositeIndexes": [ ## [ ## { ## "order": "ascending", ## "path": "/name" ## }, ## { ## "order": "descending", ## "path": "/age" ## } ## ] ## ], ## "excludedPaths": [ ## { ## "path": "/headquarters/employees/?" ## }, ## { ## "path": "/\"_etag\"/?" ## } ## ], ## "includedPaths": [ ## { ## "indexes": null, ## "path": "/*" ## } ## ], ## "indexingMode": "consistent", ## "spatialIndexes": [ ## { ## "path": "/*", ## "types": [ ## "Point", ## "LineString", ## "Polygon", ## "MultiPolygon" ## ] ## } ## ] ## }, ## "partitionKey": { ## "kind": "Hash", ## "paths": [ ## "/zipcode" ## ], ## "systemKey": null, ## "version": null ## }, ## "statistics": [ ## { ## "documentCount": 0, ## "id": "0", ## "partitionKeys": [], ## "sampledDistinctPartitionKeyCount": 0, ## "sizeInKB": 0 ## } ## ], ## "uniqueKeyPolicy": { ## "uniqueKeys": [] ## } ## }, ## "resourceGroup": "AzureCosmosDBRG", ## "tags": null, ## "type": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers" ## } |
Clean up.
1 2 3 4 5 6 7 |
## Delete temporary index policy file rm -f indexpolicy.json ## Delete the resource group az group delete \ --name AzureCosmosDBRG \ --yes |
Unfortunately, there is no Azure CLI available to perform SQL interaction with Azure Cosmos DB. Hence unable to show you how to connect, insert, update, select and delete data in Azure Cosmos DB. Which I will cover in next post using Python SDK.
Hope you have enjoyed this article. To get more details in Azure Cosmos DB, please refer the below documentation.
https://docs.microsoft.com/en-us/azure/cosmos-db/
Azure Cosmos DB CLI reference
https://docs.microsoft.com/en-us/cli/azure/cosmosdb?view=azure-cli-latest