Getting Started With Azure Functions
Getting Started With Azure Functions
Move over, AWS Lambda! Azure Functions went open for general availability last month. See how Azure's journey into serverless architecture works.
Join the DZone community and get the full member experience.Join For Free
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# or Node.js, developing with code you're familiar with just makes coding more fun. It's way easy to integrate with DevOps. We will be seeing an example of this in this tutorial. More importantly, you have to pay only when the code actually runs. So, let’s get started.
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 — and 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 will explain the importance and capabilities of Microsoft Azure Functions.
Quick High-Level Overview
Azure Functions is a solution for running small pieces of the code, or 'functions,' seamlessly in the cloud. Azure Functions can make development more productive, as the only area of concern is to code for handling a specific use-case or problem in an application, without actually worrying about the 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 time the code runs.
Key features like choice of coding language, a pay-as-you-go 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 a smart cloud technology to work with.
Here, let's assume that you have Azure Portal Account. If not, create your free Azure account.
Once you’re done with creating an 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, to go https://functions.azure.com and create an Azure Function. It looks like the screen below.
The second way, which I mostly prefer and will use in this tutorial, is to start 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 it and start from there.
Click on New Function to start with.
There is another dropdown for Scenario, namely — Core, API and 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 your template is selected, you will land on to the below page. As highlighted in below image, the Develop section consists of Function URL & GitHub Secret key (for integrating with GitHub) and Code (where the desired code needs to be written in). It will be populated with some code, which would log the comments and would be seen in the Log window, as seen below
Assuming you have your GitHub account, go to GitHub and log onto your account.
Navigate to Settings => Webhook & Services tab.
Enter the Payload URL and Secret key, with the 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 a webhook will be added, as shown below (always verify the URL to be on the safe side).
Enter the comment to an issue listed at GitHub.
The same should get logged into 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 the Integrate section in the Azure Functions window. Here, under Outputs, click on 'New Output' => Azure DocumentDB Document.
Click on the 'New' link. You will be listed with the existing DocumentDB account associated with your Azure account, if any. You can go with selecting existing DocumentDB account or create a new one, as shown in the second image below.
Once account selection is done, the final window will appear. Click on Save, and navigate to the Develop section in your 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 (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).
Enter a new comment at the GitHub Issue thread.
Verify the same at our DocumentDB Document Explorer.
Whoa! How look how seamlessly we created the new Azure Function, integrated it with GitHub, and then easily extended the Functions output to Azure DocumentDB (NoSQL). All in less than 15 mins ! (This article though took longer time ;) )
Still, for a more technical information about Microsoft Azure Functions, visit the developer reference.
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 microservices.
I 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.
I hope you 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 me at meKasamShaikh@outlook.com.
Knowledge grows by sharing! Don't forget to share this article. If it helped you, it will also help many others, too!
Thank You! Happy Azure Coding!
Published at DZone with permission of Kasam Shaikh . See the original article here.
Opinions expressed by DZone contributors are their own.