27
2018
Azure Functions – Learn more about it
Azure Functions is a solution, for easily running small pieces of code or “functions” in the cloud. You can write, only the code you need for the problem at hand, without worrying about the whole application or the infrastructure to run it. Functions can make development even more productive.
Azure functions consist of code and events. Developer can write the code to provide the solution for the problem and then specify what events will trigger, to run that code. What happens next, is handled by Azure functions. Azure Functions lets you develop serverless applications on Microsoft Azure.
Why do we need Azure Functions?
If you need fine-grained control of your cloud solution from infrastructure level, you can go for a virtual machine, where you can configure performance and environment as you need. It is as same as having your own server but far more scalable. Infrastructure as a Service (SaaS) solution offers the highest level of control but you need to spend a lot of time configuring the environment and maintaining it.
Other solution is Platform as a Service (PaaS) implementation, where we have less control over the environment and much easier to scale. Azure will take care of the infrastructure and we can focus on developing the solution on that. Could Services offer Web Roles for the web applications such as Websites or Web APIs and Worker Roles for background tasks. App Service offers Web Apps and Web Jobs, where things are made easier for developing the solution, by offering new features for rapid application development. These have less control than the Cloud service, where you can remote to VM’s. Azure functions are provided as a part of this App Service.
Let’s take a solution, where the system wait for a new text file copied to a cloud location and read and write some of the content in it, to a database. If developing this solution on an ordinary cloud service we have to develop it, all the way from monitoring file location, reading the file content until writing it to the database. This includes a lot of code, that does not directly involve the business logic but only to run the service. However, with Azure functions, a trigger can be created for the file location and write the code to store content to the database.
Features of Azure Functions
Azure Functions provide numerous features for developers, that stands out from the other offerings, which developers can use to create solutions.
Pay Per Use Pricing Model – This model allows you to pay only for the time you execute your code. However, you have two options when it comes to pricing;
- Consumption Plan – You are billed based on the resource consumption and the number of executions. Resource consumption is the average memory usage in gigabytes, for function execution time in seconds. You are entitled to free 1 million requests and 400,000 GB-s of resource consumption per month. So the billing will only be on the amount exceeding the free limit. Charges will be as follows,
- For execution time – $0.000016 Per GB-s
- For number of executions – $0.20 per million executions.
Also remember, that memory will be calculated to the nearest 128MB and time is calculated to the nearest 100ms. These charges are for Azure functions only. Any other Azure resource used by your function, will be calculated separately.
- App Service plan – Your function apps run on dedicated VMs on Basic, Standard and Premium accounts. Dedicated VMs are allocated to your App Service apps, which means the functions’ host is always running.
For more information about hosting plans, see Azure Functions hosting plan comparison. Full pricing details are available on the Functions Pricing page.
Multiple Languages Support – You can write Azure functions in multiple languages such as C#, F#, Node.js Python and PHP.
Support for Dependency Packages – Azure Functions support NuGet and NPM packages.
Support for Integrations – Azure Functions support integrating with several Azure offerings and SaaS offerings. For example, Azure DocumentDB, Azure Event Hubs, Azure Notification Hubs, Azure Service Bus Queues and Topics and many others. It even supports integration with non Azure offerings.
Integrated Security – OAuth providers such as Azure Active Directory, Facebook, Google, Twitter and Microsoft Accounts.
Flexible Development & Deployments – You can use the portal to code your Azure Functions and deploy them directly or use continuous integration through GitHub, Visual Studio Team Services
Open-source – The functions’ runtime is open-source and is available on GitHub.
What Functions can do
Azure Functions are a great solution for processing data, integrating systems, working with the internet-of-things (IoT), and building simple APIs and microservices. Consider Functions for tasks like image or order processing, file maintenance or for any tasks that you want to run on a schedule.
Timer-based processing – Azure Functions supports an event, based on a timer, using Cron job syntax. For example, execute code that runs every 15 minutes and clean up a database table based on custom business logic.
Source: https://azure.microsoft.com/en-us/services/functions/
Azure service event processing – Azure Functions supports triggering an event based on an activity in an Azure service. For example, it allows executing serverless code, that reads newly discovered test log files, in an Azure Blob storage container, and transform this into a row in an Azure SQL Database table.
Source: https://azure.microsoft.com/en-us/services/functions/
SaaS event processing – Azure Functions supports triggers, based on activity in a SaaS. For example, you can save a file in OneDrive, which triggers a function that uses the Microsoft Graph API to modify the spreadsheet and creates additional charts.
Source: https://azure.microsoft.com/en-us/services/functions/
Serverless web application architectures – Azure Functions can power a single-page app. The app calls functions, using the WebHook URL, saves data and decides what data to display. Or, do simple customizations, such as changing ad targeting by calling a function and passing it user profile.
Source: https://azure.microsoft.com/en-us/services/functions/
Serverless mobile back ends – A mobile back-end can be a set of HTTP APIs, that are called from a mobile client, using the WebHook URL. For example, a mobile application can capture an image, and then call an Azure Function to get an access token for uploading this to blob storage. A second Azure Function is triggered by the blob upload and resizes the image, to be mobile-friendly.
Source: https://azure.microsoft.com/en-us/services/functions/
Real-time stream processing – For example, Internet of Things (IoT) devices send messages to Azure Stream Analytics, which then calls an Azure Function to transform the message. This function processes the data and creates a new record in an Azure SQL database.
Source: https://azure.microsoft.com/en-us/services/functions/
Real-time bot messaging – You can use Azure Functions, to customize the behavior of a bot, using a WebHook. For example, create an Azure Function, that processes a message using Cortana Analytics and call this function using Microsoft Bot Framework.
Source: https://azure.microsoft.com/en-us/services/functions/
Types of Triggers
There are several types of triggers, that you can use to start the execution of Azure Functions.
- HTTP Triggers
- Timer Triggers
- CosmosDB Trigger
- Storage Blob Trigger
- Storage Queue Trigger
- Event Hub Trigger
- Service Bus Triggers
How to create Azure Functions
You can create functions in four ways,
- Azure Portal – You must have a function app to host the execution of your functions. A function app, lets you group functions as a logic unit for easier management, deployment, and sharing of resources. (Refer Azure documentation- sample)
- Azure CLI – You need to have an active Azure subscription. (Refer Azure documentation – sample)
- Java/Maven – You need .NET Core latest version, Java Developer Kit, version 1.8, Azure CLI, Apache Maven version 3.0 or above, Node.js, version 8.6 or higher. (Refer Azure documentation – sample)
- Visual Studio – You need Visual Studio 2017 version 15.3, including the Azure development workload. (Refer Azure documentation – sample)
Best Practices when writing Azure Functions
Avoid long running functions – Large, long-running functions, can cause unexpected timeout issues. Importing dependencies can also cause increased load times, which result in unexpected timeouts. Single module loaded by your code, may load its own additional modules. Whenever possible, refactor large functions into smaller function sets, that work together and return responses fast.
Cross function communication – When integrating multiple functions, it is generally a best practice to use storage queues for cross functional communication. The main reason being the storage queues are cheaper and much easier in provision.
Write functions to be stateless – Functions should be stateless and idempotent, if possible. Associate any required state information with your data.
Write defensive functions – Design your functions with the ability to continue from a previous fail point during the next execution. Take advantage of defensive measures, already provided in the Azure Functions platform.
Don’t mix test and production code in the same function app – Functions within a function app share resources. For example, memory is shared. If you’re using a function app in production, don’t add test-related functions and resources to it. It can cause unexpected overhead during production code execution.
Use async code, but avoid blocking calls – Asynchronous programming is a recommended best practice. However, always avoid referencing the Result property or calling Wait method on a Task instance since this approach can lead to thread exhaustion.
References:
- https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring
- https://azure.microsoft.com/en-us/services/functions/
- https://github.com/Azure/Azure-Functions
- https://wpdevkvk.wordpress.com/2017/04/01/azure-functions-going-serverless-on-azure-platform/
- https://channel9.msdn.com/Blogs/Azure/Azure-Serverless-end-to-end-with-Functions-Logic-Apps-and-Event-Grid?term=azure%20functions
- https://channel9.msdn.com/Blogs/MVP-Azure/Azure-Functions-and-Serverless-architectures?term=azure%20functions
Authors
- Shan Perera
- Suranga Gunawardana
- Amila Basnayaka