Using Microsoft Azure Functions with Cosmos DB as an Input Binding
Tags: azure, cosmosdb, node, cloud
Published: 2019-07-16 01:06:38

In this article, I will provide an overview of Microsoft Azure, Azure Functions, and Cosmos DB, along with a tutorial on how to create an Azure Function that is triggered by HTTP and utilizes Cosmos DB as an input binding. 

## What is Microsoft Azure?
Microsoft Azure is a, "cloud computing service created by Microsoft for building, testing, deploying, and managing applications and services through Microsoft-managed data centers." It provides users with the ability to scale their infrastructure and applications to a global level, increase security, and improve productivity. A user's experience on Azure can be customized in a way that best fits their needs.

### What is Azure Functions?
Azure Functions is Microsoft's take on Functions as a Service. Google's FaaS offering is known as Google Cloud Functions and Amazon's is known as Lambda Functions. Azure Functions allows developers to create stateless, event-driven applications, that can take advantage of all the benefits cloud has to offer. They can be triggered in several ways such as by HTTP, timer, or whenever a change occurs within Cosmos DB. Azure Functions also helps developers create applications faster through the use of input and output bindings. Bindings provide a mechanism for attaching additional resources to a function. More detailed information on bindings can be found [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings) 

### What is Cosmos DB?
Cosmos DB is "Microsoft's globally distributed, multi-model database service." It provides high availability, and the ability to scale throughput and storage across multiple regions. It features extremely low latency, and supports SQL and NoSQL APIs. 

## Creating an HTTP Triggered Azure Function that Utilizes Cosmos DB as an Input Binding
I'm creating this tutorial with the assumption that you have already created a Microsoft Azure account. If not, you can click [here](https://azure.microsoft.com/en-us/free/) to sign up for a free account that provides you with $200 in credits for 30 days.

### Creating Entries in Cosmos DB
First, navigate to the [Azure Portal](https://portal.azure.com/?quickstart=true#blade/Microsoft_Azure_Resources/QuickstartCenterBlade) and click Azure Cosmos DB located under the Favorites tab on the left hand side of the screen. 

![Azure Portal with Cosmos DB Highlighted](https://thepracticaldev.s3.amazonaws.com/i/uivb877pxfrwn3mi9w91.png)

Click Add to create a new Cosmos DB account 

![Create new Cosmos DB account](https://thepracticaldev.s3.amazonaws.com/i/cm7neyi977ix71ae24m4.png)

Now we can specify the details for our account. First specify a resource group name. I decided to name mine **test-resource-group**. Next provide an account name. I chose to name my account **tutorial-account**. Finally, select a location that works best for you. Once that is complete, select Review + Create. Then click Create on the next screen. Now we will have to wait for a few minutes for our Cosmos DB deployment to be created. 

![Specify Cosmos Account Details](https://thepracticaldev.s3.amazonaws.com/i/m2d369vtlrjdmyra1hgm.png)

Navigate back to the Cosmos DB page, and we should be able to see our newly created account 

![Cosmos DB account](https://thepracticaldev.s3.amazonaws.com/i/pxomihpn30bdmd4xpwxk.png)

Click Overview and Add Container 

![Cosmos DB Overview](https://thepracticaldev.s3.amazonaws.com/i/323izbalp1ztcddiaw5b.png)

Specify a Database id, Container id, a Partition Key, and then select OK. I set my Database id to be **tutorial-db**, my Container id to be **tutorial-container**, and my Partition Key to be **/facts** 

![Specify Container Information](https://thepracticaldev.s3.amazonaws.com/i/gt4kct71ufsefjkzo0z5.png)

From here, we can access our new database. Select **tutorial-db**, then **tutorial-container**, then **Items**. Finally, click **New Item**. 

![New Item](https://thepracticaldev.s3.amazonaws.com/i/r1ysn8yk58cwggh6cj6k.png)

We now should be at a page that looks like this 

![Blank New Item](https://thepracticaldev.s3.amazonaws.com/i/ipv9ncjztix3lb7b5owd.png)

Now we can create the JSON files that we will use. In our first file, we will set the id to be **hard_truths** and create a new key called **facts** with a value of: **Tabs are better than spaces**. Then click Save. (Additional key-value pairs will be added, but they will be different for everyone and can be ignored.) Our first file should look like this 

![Tabs > Spaces](https://thepracticaldev.s3.amazonaws.com/i/14mztvhgyg5j4w8v4mgi.png)

Create a second JSON file with an id of **colors** and a key called **facts** with a value of: **Red, Blue, and Green are colors**. The second file should look like this 

![Colors](https://thepracticaldev.s3.amazonaws.com/i/8o3vafh2h46huemq6age.png)

### Creating an Azure Function
Now navigate to the Function App page by clicking Function App in the left hand side menu 

![Function App Page](https://thepracticaldev.s3.amazonaws.com/i/6wpvy0a76lhj8w0mv44z.png)

Click Add to begin configuring our new Function App 

![Create Function App](https://thepracticaldev.s3.amazonaws.com/i/cfstdlzouqz0zwwez5up.png)

Create an App Name, Location, and Runtime Stack. I named my App **factsmachine**, set my Location as **East US**, and selected **NodeJS** as my Runtime Stack. Select Create and wait for a few minutes while your Function App is deployed. **Note: App names must be unique!** 

![Function App Details](https://thepracticaldev.s3.amazonaws.com/i/wet9z3fahcucrfq7w7hv.png)

Navigate to the new Function App and select New Function 

![New Function](https://thepracticaldev.s3.amazonaws.com/i/xm6sbcd681v0g09e8mvd.png)

We'll now follow the New Function Quickstart. Select In Portal for the development environment 

![Function Development Environment](https://thepracticaldev.s3.amazonaws.com/i/qdqno51m6sypmw8hxbnh.png)

Select Webhook + API for Create a Function and click create. 

![Webhook](https://thepracticaldev.s3.amazonaws.com/i/3fyf65ptv8ktzxs4d4th.png)

A new function with the name **HttpTrigger1** is created and contains the following code 

![Default Function Code](https://thepracticaldev.s3.amazonaws.com/i/spaeddi8nuthudaxy96j.png)

### Integrating Cosmos DB with Azure Functions
Select **Integrate**, then select **New Input** and select **Azure Cosmos DB** Click Select and Install the Azure Cosmos DB dependencies if they aren't already installed. 

![Function Integration](https://thepracticaldev.s3.amazonaws.com/i/7wli2ot8i6m4fdnw3hop.png)

To configure Cosmos DB as our input binding, we need to first specify a document parameter name. By default it's **inputDocument**. Next we specify the Collection Name. This corresponds to the name of the container we created for our Cosmos DB file entires which is **tutorial-container**. We now need to set our SQL Query. This will allow us to filter the files in our database by id. When we run our function, we will provide the id as an HTTP request parameter. Our SQL Query will be **SELECT * FROM c WHERE c.id = {id}** Our Database name will be **tutorial-db** and we can specify our Azure Cosmos DB account collection by clicking new and selecting our database from the dropdown list in the popup. Finally, we specify our Partition Key to be **/facts** Then click Save 

![Function Input](https://thepracticaldev.s3.amazonaws.com/i/sup6eilpynse7roy35cp.png)

Navigate back to the index.js file for our function and edit the code to be 

![Function Code](https://thepracticaldev.s3.amazonaws.com/i/wupp1wqo3h1n1i6ao9ak.png)

We access the document stored in Cosmos DB by using **context.bindings.{document parameter name}**. Since the information we care about is the value of the **facts** key, we use **context.bindings.inputDocument[0].facts** to get this information. 

Now we can finally test our function! Click Test on the right hand side of the screen and open the Test tab. 

Our HTTP method is **GET** our Query parameter is **id** with a value of **hard_truths**. Click Save and run and we can see that our output is: **Our fact: Tabs are better than spaces** 

![Test First Fact](https://thepracticaldev.s3.amazonaws.com/i/o7ofz4yn6x5axfsn2e1x.png)

If we change our id value to be **colors** we can see that our output changes to **Our fact: Red, Blue and Green are colors** 

![Test Second Fact](https://thepracticaldev.s3.amazonaws.com/i/9oz3y11ldkhjriqa9jqj.png)

## Congratulations!
That's it! You've now created an instance of Cosmos DB with two JSON files, and you've created an Azure Function that's triggered by an HTTP request that can get a file from Cosmos DB by an id parameter that's provided in the HTTP request!