tobias wright

software engineer

< Home

Getting Locally run Azure Functions going with Azure CosmosDB Emulator


If you are anything like me, I just really love getting things running locally. Not only that, I like to get things running locally, then getting it up to server almost as easily.

Problem: I want to connect Locally run Azure Function to the Azure CosmosDB Emulator so I can do all my developing on my ‘puter

Tools: Azure Function CLI, Azure CosmoDB emulator


Step 1: Create your function.

My function is straight forward. I’m using a httptrigger function, and have data coming in from a form. Then saving that to CosmosDB. The important part here is the property outputDocument – which we define in functions.json.

Step 2: Add the settings to the function.json

Below are the settings you want to add to the functions.json

Let’s go line by line:

Key Value
type: This is what you are saving to. documentDB, this is what cosmosDB use to be called
name: This where we define the variable that we can use in the function
databaseName, collectionName: This the schema of the CosmosDB, which is a NoSQL database. A more indepth explanation can be found here.
createIfNotExists: I like to set this as true, it saves a little bit of time and manually effort once you get this up to the cloud. If set to true, it’ll check see if the db and collection exist, The default is false. I’’m not sure why, it’s just a small piece of defensive coding in my opinon and should always be set to true
connection This is important. This is the variable we’ll assign the connection string. This is thing that makes it super easy to get this up to the cloud. We’ll look where to define this in the next step.
direction basically means which direction this add-in is going, in or out the function

Here’s my whole functions.json for context:

Step 3: Setup connection string variable

This is where we set up the connection to the emulator. This is where the magic happens.

In your local.settings.json add this line to the Values object:

To connect things in Azure you need the endpoing and the account key. In this instance we know that the emulator, according to the documention, once running is always this

You’ll also notice that the key is crmdb, the same as in the function.json

Here’s my whole local.settings file for context.

And that’s basically it. Once this piece is in, then you should be good to go.