Christian Reddington

Ideas and Technology :: This blog represents my own views

An Introduction to Azure Functions

September 12, 2016 in #Azure Functions #Azure #Cloud #Dev | | | Share on Google+

If you have been keeping up to date with the latest and greatest in Azure Services (yes, I know there are quite a few!), you may have heard of a new service called Azure Functions.

Azure Functions is an event-driven Platform as a Service capability, helping you to execute code upon the occurrence of a particular event. It is currently in preview, though already has potential.

If you read my recent blog on Logic Apps, you may be thinking that this sounds somewhat familiar to Logic Apps. I agree, though there is a subtle difference. Quoting a blog post from chilberto, Azure Functions is code being triggered by an event, whereas LogicApps is a workflow being triggered by an event.

If you consider using Service Bus in a cloud architecture, you may have also previously considered, or already implemented a worker role to de-queue and process messages off the bus. How about Azure Functions instead? Let's give it a go.

First, let's create the Azure Function in the Azure Portal.

Create an Azure Function in the Azure Portal

  • There is a point to note here around the App Service Plan type. You can see in the previous screenshot that I selected "Dynamic". Dynamic refers to the "Serverless" compute approach of Azure Functions. There is some further detail on the Azure Function Scalability Documentation pages about this. From a cost perspective, if you have a sporadically running function, then you may want to look at the Dynamic plans.

  • The cost of the Azure Function is calculated slightly differently and is worth a quick read on the referenced documentation page.

  • The Azure Functions platform itself will deal with the scalability (scaling out and scaling in) as required, based upon the number of hits of the configured triggers.

  • Health Warning: This feature is still in preview. Documentation, functionality and pricing could still change!

Now that we've covered some of the background points let's jump into our newly created Azure Function. Once we navigate to the resource, we will be greeted by the quick start page as shown below. Let's start off by clicking the "New Function" button, just under the "Search my functions" box.

New Azure Functions Page

For this example, we are going to create an HTTP Trigger that submits a message to a Service Bus Queue. We are going to create a separate Function that reads from that queue and outputs the original input into the Function Trace Output.

For our Service Bus Message Producer, let's start off by selecting the HTTP Trigger template, as shown in the below screenshot. Once selected, scroll down to the bottom of the page, and complete the configuration (Naming the Function, and choosing an appropriate authorization level; for now, I aim to be lazy and go with anonymous).

Azure Functions Template Gallery

Congratulations, you have just created a new Azure Function. By default, you will see the associated code for that newly created function. Underneath the name on the left-hand panel, you will see four tabs (Develop, Integrate, Manage and Monitor). Select the Integrate tab.

Congratulations, you have created a new Azure Function

You will notice that you already have a Trigger and an Output for your Function. We will not want to use the HTTP Output, so instead, let's delete that.

Delete the HTTP (res) Output

Instead, click on the "New Output" link from the above screenshot. You can then select "Azure Service Bus" from the available options, and click "Select", underneath the list of templates.

Create a new Azure Service Bus Output

The screen will present the configuration options for the Azure Service Bus Output. You can see that I have pre-populated the Message Type as "Service Bus Queue", and the Queue Name as "messagesqueue". In a later step, I have also updated the access rights to be "Send".

Begin configuring your Azure Service Bus Output on your Azure Function

However, there is one more step - We need to configure the connection to the Service Bus Instance. Let's click the "new" link, next to the Service Bus Connection input box. Once we have done that, we will see another blade appear. The list will either populate Service Bus connections already configured to this Function App instance, or we will need to create a new one.

Create a new Azure Service Bus connection string in your Azure Function

Once clicking "Add a connection string", we simply need to enter a Connection Name (local to the Function App) and provide the connection string details. (Note, this is a connection string at the Namespace level and is obtainable from the Azure Management portal).

Configuring your Azure Service Bus connection string in your Azure Function

You can now see that we have our ServiceBus Connection field populated with the newly created configuration.

Final configuration of your Azure Service Bus Output in your Azure Function

Now, let's click Save and enter the below code into the "Develop" tab.

Using System.Net;

public static void Run(HttpRequestMessage req, TraceWriter log, out string outputSbMsg) { log.Info($"C# HTTP Trigger, to Service Bus - Executed!"); string message = $"Service Bus queue message created at: {DateTime.Now}"; outputSbMsg = message; }

There are a few pieces to note;

  • We are connecting the input req and the output outputSbMsg to the integration points that we configured previously.
  • In other words, once the Azure Function endpoint has been hit - It will push a string onto the Azure Service Bus queue, stating the DateTime of the newly created message.

Develop Tab for ServiceBusMessageProducer function

In the above screenshot, you can see in the Logs section that we have an output of "C# Http Trigger, to Service Bus - Executed!". I have tested the HTTP Endpoint, meaning we have successfully added a message to the bus. Now we need to handle consumption.

There is a fundamental point that we have avoided. These are called Azure Functions, meaning, we should split our logic into functions. One Azure function should complete one piece of functionality. Let's create a New Function - "ServiceBusMessageConsumer". This time, we will create a ServiceBusQueueTrigger - C# Function.

Azure Functions Template Gallery

Scrolling passed the template gallery; we will see a view, allowing us to configure our Azure Service Bus trigger. I have pre-populated the:

  • Function name: ServiceBusMessageConsumer
  • Queue name: messagequeue (Important to check that this is the same as your producer queue name)
  • Access rights: Listen

The Service Bus Connection was automatically pre-populated with the connection string that we established earlier in the blog.

Create a new Service Bus Trigger to consume from our Azure Service Bus queue

Once you have clicked create, the page directs you to the "Develop" tab. We will not do anything fancy at this stage, and will simply let our function log the occurrence of a detected Service Bus Queue message.

Create a new Service Bus Output

After hitting the HTTP producer endpoint a few times, we can see in the below image that the consumer function has begun processing the messages on the Service Bus.

Create a new Service Bus Output

We have focused on a trivial example - But, it demonstrates the example of decoupling components in Azure. Additionally, this same approach also works with Azure Service Bus Topics, so could be suitable for message routing within a solution.

Please drop me a Tweet, I'd love to hear what type of things you are creating with Azure Functions!

September 12, 2016 in #Azure Functions #Azure #Cloud #Dev | | | Share on Google+