Sign in during Public Preview to get the Team plan free, plus an early-adopter discount when we launch. Sign in
This guide shows you how to provision a Cosmos DB Account, a database and a container against Locally. As with the other guides we're going to use the Azure CLI, but the same resources can be provisioned with HashiCorp Terraform, Pulumi or Bicep too.
az) installed.Firstly, we need to launch Locally which we can do from a terminal by running:
$
locally build
Once Locally has started, the Locally Dashboard will open automatically:
Next we can create the Resource Group to hold the account:
$
locally run az group create -n sample-resources -l berlin
There's two things to note here:
locally run.locally run when you do.With the Resource Group in place, we can create the Cosmos DB Account. Unlike most resources, the location is given as a --locations entry rather than --location, because a Cosmos DB Account can span several regions:
$
locally run az cosmosdb create --name samplecosmos1 --resource-group sample-resources --locations regionName=berlin
Note
The interesting part of the response is the documentEndpoint, which is where the data plane lives:
{
"note": "some fields skipped for brevity",
"documentEndpoint": "https://samplecosmos1.cosmonaut.locally:5665/",
"kind": "GlobalDocumentDB",
"location": "berlin",
"name": "samplecosmos1",
"provisioningState": "Succeeded",
"resourceGroup": "sample-resources",
"type": "Microsoft.DocumentDB/databaseAccounts"
}
That cosmonaut.locally hostname is served by Locally's own DNS server and points at the Cosmos DB Emulator running on your machine. Tools run through locally run reach it as they would Azure, and apps built on the Azure SDKs need a few lines of setup.
We can retrieve the account again at any point with:
$
locally run az cosmosdb show --name samplecosmos1 --resource-group sample-resources --query "{name:name, kind:kind, documentEndpoint:documentEndpoint, provisioningState:provisioningState}"
An account on its own doesn't hold anything, so next we'll create a database within it:
$
locally run az cosmosdb sql database create --account-name samplecosmos1 --resource-group sample-resources --name appdb
{
"name": "appdb",
"type": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
}
Then a container inside that database. A container needs a partition key, which Cosmos uses to distribute documents - here we'll partition orders by the customer they belong to:
$
locally run az cosmosdb sql container create --account-name samplecosmos1 --resource-group sample-resources --database-name appdb --name orders --partition-key-path /customerId
{
"name": "orders",
"pk": [
"/customerId"
]
}
We can confirm both exist:
$
locally run az cosmosdb sql database list --account-name samplecosmos1 --resource-group sample-resources --query "[].{Name:name}" -o table
Name
------
appdb
$
locally run az cosmosdb sql container list --account-name samplecosmos1 --resource-group sample-resources --database-name appdb --query "[].{Name:name, PartitionKey:resource.partitionKey.paths[0]}" -o table
Name PartitionKey
------ --------------
orders /customerId
To read and write documents, an application needs the account's endpoint and key. The endpoint is the documentEndpoint above, and the keys come from:
$
locally run az cosmosdb keys list --name samplecosmos1 --resource-group sample-resources --query primaryMasterKey -o tsv
Hand that pair to the Azure Cosmos DB SDKs and your application reads and writes against Locally.
Note
We can see the Cosmos DB Account in the Locally Dashboard too:
Finally, we can tidy up. To remove the Resource Group and everything within it:
$
locally run az group delete -n sample-resources --yes
There's nothing billable to clean up, since everything ran on your machine, but it's still worth checking your teardown scripts work here before you run them against Azure.
Whilst this guide used the Azure CLI, Cosmos DB works the same way through any of the tooling that Locally supports - Microsoft.DocumentDB/databaseAccounts and its child resources in HashiCorp Terraform or OpenTofu, Pulumi, Bicep or an ARM Template all provision against Locally in the same way, with only the location changed.
The Cosmos DB Emulator covers what the data plane supports. To keep the account key out of your app's settings, store it in a Key Vault.
Should you encounter any issues, please take a look at the troubleshooting section.
Sign in during Public Preview to get the Team plan free, plus an early-adopter discount when we launch. Sign in
Your Azure infrastructure, running on your machine. Deploy in seconds, break things freely, and ship to Azure when you're ready.