The latest buzz word is serverless applications. Azure Functions are Microsoft’s offering in this space. As with most products that are new on the cloud Azure Functions are still evolving and therefore can be challenging to develop. Documentation is still being worked on at the time I am writing this so here are some things that I have learned while implementing them.
There is a lot to cover here so I am going to break this topic into a few posts:
- Creating and Binding
- Settings and References
- Coding Concerns
Creating A New Function
The first thing you are going to need to do is create a Function App. This is a App Services product that serves as a container for your individual functions. The easiest way I’ve found to start is to go to the main add (+) button on the Azure Portal and then do a search for Function App.
Click on Function App and then the Create button when the Function App blade comes up. Fill in your app name remembering that this a container and not your actual function. As with other Azure features you need to supply a subscription, resource group and location. Additionally for a Function App you need to supply a hosting plan and storage account. If you want to take full benefit of Function Apps scaling and pricing leave the default Consumption Plan. This way you only pay for what you use. If you chose App Service Plan your function will will pay for it whether it is actually processing or not.
Once you click Create the Function App will start to deploy. At this point you will start to create your first function in the Function App. Once you find your Function App in the list of App Services it will open the blade shown below. It offers a quick start page, but I quickly found that didn’t give me options I needed beyond a simple “Hello World” function. Instead press the New Function link at the left. You will be offered a list of trigger based templates which I will cover in the next section.
Triggers define the event source that will cause your function to be executed. While there are many different triggers and there are more being added every day, the most common ones are included under the core scenarios. In my experience the most useful are timer, queue, and blob triggered functions.
Queues and blobs require a connection to a storage account be defined. Fortunately this is created with a couple of clicks and can be shared between triggers and bindings as well as between functions. Once you have that you simply enter the name of the queue or blob container and you are off to the races.
When it comes to timer dependent functions, the main topic you will have to become familiar with is chron scheduling definitions. If you come from a Unix background or have been working with more recent timer based WebJobs this won’t be anything new. Otherwise the simplest way to remember is that each time increment is defined by a division statement.
In the case of queue triggers the parameter that is automatically added to the Run method signature will be the contents of the queue message as a string. Similarly most trigger types have a parameter that passes values from the triggering event.
Input and Output Bindings
Some of the function templates include an output binding. If none of these fit your needs or you just prefer to have full control you can add a binding via the Integration tab. The input and output binding definitions end up in the same function.json file as the trigger bindings.
The one gripe I have with these bindings is that they connect to a specific entity at the beginning of your function. I would find it preferable to bind to the parent container of whatever source you are binding to and have a set of standard commands available for normal CRUD operations.
Let’s say that you want to load an external configuration file from blob storage when your function starts. The path shown below specifies the container and the blob name. The default format show a variable “name” as the blob name. This needs to be a variable that is available and populated when the function starts or an exception will be thrown. As for your storage account specify it by clicking the “new” link next to the dropdown and pick the storage account from those that you have available. If you specified a storage account while defining your trigger and it is the same as your binding it can be reused.
The convenient thing about blob bindings is that they are bound as strings and so for most scenarios you don’t have to do anything else to leverage them in your function. You will have to add a string parameter to the function’s Run method that matches the name in the blob parameter name text box.
That should give you a starting point for getting the shell of your Azure Function created. In the next two posts I will add settings, assembly references and some tips for coding your function.