Getting Started With Azure Functions

Microsoft Azure Functions is a super-cool technology introduced by Microsoft. It brings together the code in an event based programming model. If you need to implement a simple integration between your On-Premise or Cloud resources, without having complete service, a simple Azure function would be the best choice to go for. You can respond to any event with a set of actions needed to be performed, by calling the code from any Azure solution or SaaS product.

With a given choice of using different languages like C#, Node.js , developing the code in the way with your portal experience, makes coding simply fun. It's way easy to integrate with DevOps. We will be seeing the example of this in this tutorial. More importantly, you have to pay only when the code actually runs. So, let’s get started with this cool Azure Functions Tech-dose.

This article provides a quick and smart high-level overview of Microsoft Azure Functions with a practical example of how to start with creating an initial Azure Function using Node.js, integrating it with GitHub. Also, we will learn how to integrate an output for Azure Functions, with other Azure SaaS products, like DocumentDB (NoSQL).

For novice Cloud developers, aspiring Cloud architects, Pre-sales consultants, and all other IT professionals associated with the Cloud computing, especially with Microsoft Azure, this tutorial would explain importance and capabilities of Microsoft Azure Functions, fairly enough.

Quick High-level Overview

Azure Functions is a solution for running small pieces of the code or 'functions' in the Cloud seamlessly. Azure Functions can make development more productive, as the only area of concern is to code for handling specific use-case or problem in an application, without actually worrying about entire application or infrastructure it’s running on. Azure Functions can be developed in different languages, such as C#, Node.js, Python ,or PHP, making it more developer friendly.

Azure scales the code as needed, and you only need to pay for the times the code runs.

Key features like Choice of coding Language, pay as use model, supporting favorite dependency libraries using NuGet, integrated security with OAuth providers, such as Azure AD, Facebook, Google and other social channels, simplified integrations with Azure SaaS products, Continuous Integrations, and an open source runtime makes Microsoft Azure Functions smart Cloud technology to work with.

After praiseful sessions, let’s start with actually experiencing the same.

Here, let's assume that you have Azure Portal Account. If not, create your free Azure account (https://azure.microsoft.com/en-in/free/).

Once you’re done with creating Azure account, let’s start with creating our first Azure Function.

Create Azure Functions

As of now, there are two ways to create Azure Functions.

First way, to go https://functions.azure.com and create the Azure Function. It looks like the screen below.

Azure Function

The second way, which I mostly prefer and would use in this tutorial, is from https://portal.azure.com.

Click on New => Web + Mobile => Function App, as shown in below image.



Enter the desired Function App name and other details. Select the storage account (or create a new one) and click on Create.



Now, you will be taken to the Get Started page where you can select to start based on some existing apps, or start from scratch, or you can import your code if you already have and start from.

Click on New Function to start with.



Now, you will be redirected to the below page with all different Azure Functions templates. As shown below, there is a dropdown for Language that you can pick from (Bash, Batch, C#, F#, JavaScript, PhP, Powershell, Python are the different language options available).

There is another dropdown for Scenario, namely - Core, API & webhooks, Data Processing, Experimental, and Samples, by which you can filter the templates.

Here, we will select 'GitHub Webhook - Node’ template which will be triggered as per the event over GitHub takes place. Give some appropriate name to this function and click Create.

As seen in below image, here, I am giving it name as 'fnLogGitHubIssueActivity' as will be logging details, when there is any comment being entered to Issue section at GitHub.



Once templates is selected, you will land up to the below page. As highlighted in below image, Develop section consists of Function URL & GitHub Secret key (For integrating with GitHub), Code where desired code needs to be written in. It would be populated with some Code which would log the comments and would be seen in Log window, as seen in the below Code Window.



Assuming you have your GitHub account, go to GitHub and logon to your account.

Navigate to Settings => Webhook & Services tab

Enter Payload URL and Secret key, with Function URL and GitHub Secret key from portal’s Functions window, respectively.

As shown in below image, you can select the Events for which we need to trigger the Azure functions. Here, we would select 'Issue comment' as we need to trigger the functions when someone enters the comment to any Issue in GitHub.

Once done, click on Add webhook.


And, webhook will be added, as shown below (always verify the URL for safer side).



Testing

Enter the comment to an issue listed at GitHub.

The same should get logged in to our Azure Function window's ' Log ' section.


Cool! It's working.

You would have noticed that till now, I haven't worked upon any development tool, like Visual Studio for writing code. It's only and only the portal that we have used so far, and that's one amongst the coolest features of using Azure Functions. Simplicity of coding with Azure Portal.

By now, I am sure you would be more familiar with Azure Functions. Let’s extend our tutorial, by integrating Azure Functions output with other Azure SaaS products.

Considering the above tutorial, we can log comments entered in GitHub issue section, in Azure products, like Azure Storage Table, Azure DocumentDB (NoSQL).


In this tutorial, I will log comments entered in GitHub to Azure DocumentDB, using Azure Functions.

To do this, go to Integrate section in Azure Functions window. Here, under Outputs, click on 'New Output' => Azure DocumentDB Document.



Coming to this window, follow the steps shown in below image.

Click on 'New' link. You will be listed with existing DocumentDB account associated with your Azure account, if any. You can go with selecting existing DocumentDB account or create New, as shown in the second image below.

Once account selection is done, the final window would appear. Click on Save, and navigate to Develop section in Window.



DocumentDB Account Listing.



Now in the Code section, add the highlighted code. This code will add comments to the selected DocumentDB document. You can directly copy the below code and paste it (be sure you code correct 'Document Parameter Name' present in Integrate window. Here, as you can see, this name is 'OutputDocument' and the same is added in code section).
  1. context.bindings.outputDocument = {text: "Comments: '"+ data.comment.body + "'“};  
  2.   
  3. };   


Testing

Enter a new comment at GitHub Issue thread.



Verify the same at our DocumentDB Document Explorer.



Whoa! How seamlessly we created the new Azure Function, integrated with GitHub, and then easily extended  the Function output to Azure DocumentDB (NoSQL) Azure SaaS product. Everything in less than 15 mins ! (This article though took longer time ;) )

Still, for more technical information about Microsoft Azure Functions, visit developer reference,
(https://azure.microsoft.com/en-us/documentation/articles/functions-reference/)

Conclusion or Summary

Azure Functions is a super cool solution for processing data, integrating systems, working with the internet-of-things (IoT), along with building simple APIs and micro-services.

Would highly recommend Microsoft Azure Functions for performing tasks like image or order processing, file maintenance, long-running tasks that you want to run in a background thread, or for any tasks that you want to run on a schedule.

Hope you would have enjoyed this tutorial. If you have any questions with respect to the same or any interesting use-case or suggestion, please do comment below or write to meKasamShaikh@outlook.com.
 
Knowledge grows by sharing! Don't forget to share this article. If it would have helped you, it will also help many others too!

Thank You! Happy Azure Coding!