How can I run Azure CLI commands against my local CosmosDB Emulator?

Viewed 753

Ideally I would like to be able to create a script like a .azcli which I could run locally against the CosmosDB emulator and then use the same script in my devops pipeline to upsert resources to Azure.

Here is my current CLI commands.

deploy-cosmos.azcli

az cosmosdb sql database create --name MyDb
                                --throughput 400 
    # This is not correct ->    --account-nameAccountEndpoint=https://localhost:8081/
                                --resource-group my-rg


az cosmosdb collection create --collection-name Users
                              --db-name MyDb

                              [--default-ttl]
                              [--indexing-policy]
                              [--key]
                              [--name]
                              [--partition-key-path]
                              [--resource-group-name]
                              [--subscription]
                              [--throughput]
                              [--url-connection]

I can't find any mentions of the CosmosDB emulator in the Azure CLI docs and none of the parameters on the CLI commands themselves seem to be able to directly point to the emulator.

https://docs.microsoft.com/en-us/cli/azure/cosmosdb/collection?view=azure-cli-latest

I also haven't had any luck on google either.

I know I can interact with the emulator in c# to do the deployment but I was hoping I could use the CLI since I would prefer to use the same deployment process for my dev environment and test/production environment.

1 Answers

Try:

az cosmosdb database create --key "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" --db-name "<your-db-name>" --url-connection "https://localhost:8081"

You'll get a message about this being deprecated but... these parameters do indeed work, and it results in a database being created.

I also tested with az cosmosdb collection create (you'll need to provide --partition-key-path) and az cosmosdb collection list.

For example: to list the collection name + partition key of each collection (via --query):

az cosmosdb collection list --query "[].{CollectionName:id,PartitionKey:partitionKey}" --key "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" --db-name "testdb" --url-connection "https://localhost:8081"  -o json

Output:

[
  {
    "CollectionName": "stackoverflow",
    "PartitionKey": {
      "kind": "Hash",
      "paths": [
        "/id"
      ]
    }
  },
  {
    "CollectionName": "testcontainer",
    "PartitionKey": {
      "kind": "Hash",
      "paths": [
        "/id"
      ]
    }
  }
]
Related