What is Azure Functions
Main features of Azure functions are
- Easily running small pieces of code in the cloud.
- Forget about the infrastructure and write the code for the problem using language of your choice.
- Pay per usage and trust azure to scale as need basis.
- Open Source
- Dependency Management using Nuget and NPM
Templates provided by Azure Functions
- EventHub Trigger
- HTTP Trigger
- Blob Trigger, Queue Trigger
- ServiceBusQueue Trigger, ServiceBusTopic Trigger
- Timer Trigger
- Generic Webhook, Github Webhook
Scenarios where Azure Functions are very useful
Consider a scenario like log files are stored in a blob storage for a production system. The company wants to make use of these logs for datamining purpose. These logs are in unstructured manner, so it needs to be processed whenever it is available on blob storage and push this data to either SQL Azure / Excel for generating reports. With this we can easily know the different types of errors, the frequency of error happened etc
Without functions we need to use some kind of polling mechanism to see whether any new file is added to the storage or not and then create some kind of Webjob/Worker role to process these logs. Again developing a code required an editor like VS and after development needs to deploy. When you want to scale website also needs to be scaled for webjobs. Using Worker role in this scenario is very costly because provisioning the VM and scaling the instances are too cost. This is where Azure functions comes into the picture.
Azure functions are event driven and it can be scaled automatically by azure. No separate IDE is required to write the code and deploy. Price is also cheap with compare to web jobs and worker roles. The following diagram depicts how azure functions help us in processing log files which are stored in Azure blob storage.
- Log Files are stored to Azure blob storage
- An event will be raised When something is added to blob storage
- Inside that blob trigger we will write the code for
- Parsing each log file
- Push the processed data to SQL Azure and Excel etc
With this the job of function is over, then the processed data is used for Generating reports, graphs, charts etc.
Another scenario is like website allows users to upload the videos, images, profiles etc. Company wants to get metadata information out of this for further processing. This is another good scenario where functions are very helpful. Whenever some image/video is uploaded to the blob, the blob trigger will be invoke and using Microsoft Cognitive Services like Face API, Emotion API, Video API we can extract different metadata information and push this data to any data store for further processing.
We can use Functions as Httphandlers and inspect each web request and do further processing Whether forward that request to further processing or stop here itself based on the context information of HTTPWebRequest.
These different scenarios where Functions are very useful.
Azure functions portal does not currently provide a mechanism to add nuget packages. Alternative is upload a project.json with the necessary references.
} } } }
You can deploy Project.json using either FTP/KUDU/VSOnline
Using SCM (Kudu) endpoint
- Navigate to: https://<function_app_name>.scm.azurewebsites.net
- Click on Debug Console > CMD
- Navigate to D:\home\site\wwwroot\<function_name>
- Drag-and-drop your Project.json file into the folder
How to include references to external Assemblies
#r “Assembly Name”
So you need to add a reference to external Assemblies and import the namespaces required.
It contains metadata for the function. A function can have any number of input and output bindings but can only have a single trigger binding.
Ex: When item is added to Queue, insert the record to DocumentDB
Azure Functions integrate with so many Azure and third party services, One of those is DocumentDB, similarly we have Github and Service Bus OnPrem etc.
Right now azure functions provide 2 kinds of pricing plans
- App Service Plan: When your subscription has app service for other applications you can run the functions on the same plan without any cost.
- Dynamic Hosting Plan: In this case azure takes care of resource management and you need to pay per usage. This plan provides a new container for execution of the code dynamically and charged only for the time taken to execute the code, number of times the function is executed and the memory used by your code. Azure functions are metered to Gigabyte seconds(GB-s)
Testing Azure Functions
Consider a scenario like you have created HttpTrigger, how to test this?
curl -G https://<Your Function App>.azurewebsites.net/api/<Your Function Name>?code=<your access code>
We can event test with Postman and CRESTClient extensions
Azure Portal provides a Run button where you can provide a request body but cannot provide a query string parameters. Azure portal provides Logs window where we can see the logging information.
Compiling Azure Functions Locally
Local development is an area where Microsoft is actively looking to enhance the capabilities of Functions. But we can do the limited local compilation and right now the user experience is not that much great. For more information please refer: https://azure.microsoft.com/en-us/documentation/articles/functions-run-local/
Azure Functions Vs Azure Webjobs
|Write the code in console and deploy through visual studio.||Write code directly in portal, easily to deploy and test.|
|Runs as a background process in the context of App services.||Runs using Appservice/dynamic Hosting plan.|
|Scale the whole web.||New container will be created dynamically when required and have more possibilities with dynamic service plan.|
|If you have existing App service plan there is no additional cost.||Metered to nearest 100ms (GB-s)|
|VM is always required.||Server less computing and VM is not mandatory.|
|Can run locally, Deploy it easily to cloud.||Limited local compilation and not in matured state.|
|Host is customizable||Not supporting bringing your own host and customizing the own host.|
In short we can say like Azure functions are “Webjobs SDK as a service”. It is very light weight and can be scaled dynamically by azure. It is well integrated with so many azure and third party services and provided so many languages support. Pricing is also lucrative.