Azure Functions are Microsoft’s answer to Amazon’s Lambdas or Google’s Cloud Functions (AKA “serverless” architecture). They give you a way to deploy small pieces of code and let Azure handle the underlying server. I’ve never used them before, so I thought I would give them a try beyond “Hello, World” by getting them to work with Couchbase Server.

Getting Set Up to Develop Azure Functions

But it only works for the preview version at this time. You don’t have to use these tools to develop Azure Functions, but it made the process simpler for me.

Once I had this tooling in place, all I had to was to File > New > Project. Then under Cloud, select Azure Functions.

Once you do this, you’ll have an empty-looking project with a couple of JSON files. Right-click on the project, add the item, and select Azure Function.

Next, you’ll need to select what kind of Azure Function you want to create. I chose HttpTrigger. I also chose Anonymous to keep this post simple, but depending on your use case, you may want to require an authentication token. After you do this, a very simple shell of a function will be generated (as a C# class). You can execute this function locally (indeed, that is what the local.settings.json file is for) so you can test it out without deploying to Azure yet.

Writing a GET Function

First, I decided that I wanted two Azure Functions: one to “get” a piece of data by ID, and one to “set” a new piece of given data. I started by defining the shape of my data with a simple C# POCO:

At this point, most of this code should be familiar if you’ve used Couchbase Server and the Couchbase .NET SDK before. I’m connecting to a single node cluster and then connecting a bucket that has a password set (I’m using Couchbase Server 4.6).

But, the important thing to point out here is the use of Configuration.AppSettings. In the local.settings.json file, I’ve added these Couchbase settings to the Value section:

When running Azure Functions locally, this file is used for configuration. I have Couchbase Server running locally with a bucket called “azurefunctions”. Anything in “Values” can be accessed via Configuration.AppSettings.

Deploying to Azure

Before deploying the Azure Functions, I’ll need to create a Couchbase Cluster on Azure. This is very easy to do, thanks to Ben Lackey’s great work on the Azure Marketplace. Once that’s deployed, deploying the Azure Functions are also easy, thanks to Visual Studio.

Deploying Couchbase Server to Azure

Here is a short video walking you through the process of creating a Couchbase Server cluster on Azure.

For my example, I followed that video closely. Here is step 1, where I configure the username, password, and resource group.

For the second step, I only created a single node cluster on the smallest, cheapest VM (DS1 v2). I created 0 Sync Gateway nodes since I’m not using Sync Gateway for this example.

Step 3 is just a summary, and Step 4 is a confirmation. It will take three to five minutes for the Couchbase Cluster to start up in Azure.

Deploying Azure Functions to Azure

Now, Couchbase Server is running. So let’s deploy the Azure Functions that will interact with it.

To begin, right-click the project in Visual Studio and select Publish. You’ll need to create a new publish profile the first time you do this, but that’s easy.

Give your functions an app name, select a subscription, select a resource group (you can create a new one, or use the same group that you created above for Couchbase), select a service plan, and finally a storage account. You can create new ones when necessary.

Click Create and these items will start to be created in Azure (it may take a minute or two).

Trying Out the Azure Functions

Finally, remember that the Azure Functions need to know the URI, bucket name, and password in order to connect to Couchbase Server. That information is in local.settings.json, but that file is not used for actual Azure deployments.

In the Azure portal, navigate to the Azure function (I called mine cbazurefunctions), and then select Application Settings. Under App settings, enter those three settings: couchbaseUri, couchbaseBucketName, and couchbaseBucketPassword.