What Azure Functions are and How You Can Use Them to Run Small Applications Effortlessly

July 16, 2018

Serverless computing is the word of the day, and Microsoft Azure Functions acts a modern serverless architecture delivering event driven cloud computing and configured to comply with application development. Through Azure Functions, you can author and execute snippets of code in the cloud without the hassle of managing the web servers or containers. So that's why we say it has serverless architecture. It lets you run small pieces of code and the developer doesn't have to worry about the infrastructure of the platform on which it is executed.

This way, Function-as-a-Service becomes a generic term for serverless and eliminates infrastructure considerations for the developer. Of course, servers are there, but the user doesn't have to bother with managing or provisioning them. They can even be scaled according to demand. To build the apps themselves, the developers can use their own choice of programming language, giving them the freedom to work optimally.

Features of Azure Functions

Microsoft Azure gives you the added benefit of both IaaS and PaaS, with the convenience of cloud infrastructure as the highlight. Some of the features Azure Functions are highlighted below.

Intuitive, browser-based user interface - You can write code in the easy-to-use web interface or use your favorite development tool to build and debug. Azure Functions has an intuitive, browser-based user interface to respond to events generated by Event Hubs, HTTP Requests, Timers, Azure Queues, Table Storage, Blob Storage, etc.

Variety of programming languages - The developer has the freedom to choose the programming language of his/her choice. The service accepts a variety of programming languages like C#, F#, Node.js, Python, PHP or Java. You can utilize the programming model for a number of activities such as building HTTP-based API, communicating with other servers or orchestrating complex workflows.

Capabilities for implementing code - Being event driven, the application platform has capabilities to implement code triggered by events occurring in any third-party service or on-premise system.

Compute-on-demand - This delivery model ensures that computing resources are available to the users as per their demand. It is the user's enterprise or cloud service provider that usually maintains these resources. The user doesn't have to worry about provisioning or managing them anywhere, but rather, focus on their business logic.

Supports Continuous Deployment and Integration - Even though Azure Functions is serverless architecture, it still supports Continuous Deployment and Continuous Integration because developers still need them. This is done through through GitHub, Microsoft Visual Studio Team Services and other development tools, such as Xcode, Eclipse and IntelliJ IDEA.

During deployment, the code will be deployed in the subslot first, and not in the principal slot. This is an advantage if the integration tests fail. These are the build steps in the Azure Function deployment:

- Build the C# solution code (as an example)

- Run unit tests

- Publish the artifacts for the release

Once you deploy the Azure Functions in a DevOps pipeline, here is how it will work:

- Performing Continuous Integration

- Running unit tests

- Provisioning and Configuring Azure Infrastructure

- Performing Integration tests

- Deploying the Azure Function App

Easy integration with Azure and 3rd-party services - Azure Functions offers integration with a number of Azure services apart from several third-party services (like Azure Notification Hubs, Azure Event Grid, Azure Event Hub, Azure Service Bus, Azure CosmosDB, Azure Storage, etc.) in many of the services where you need to run code snippets.

Portable Runtime - Microsoft has made Functions Runtime portable, so it is easier to build and deploy serverless applications on any public cloud or your own internal network. This way, you can ensure your serverless code will run anywhere. As the runtime is portable, you can run the Functions anywhere - other clouds, in the data center and in Azure too.

Implement custom features - It is easier to implement custom features with this platform because runtime, templates, UI and underlying WebJobs SDK are all open source projects.

Pay as you use model - You need to pay only for the time the code is run, giving you the freedom to scale up and down as per requirement. This is calculated by calculating the amount of time the functions run in a particular billing cycle. And this way, you don't have to worry about resource management.

Some of the companies that use Functions successfully include Zeiss, Quest, Navitime, Fujifilm and Plexure.

How Azure Functions Works

How Azure Functions Works

To develop and create functions, you need to have an active Azure subscription and an Azure Storage account. The developer creates a function where there will be a logical unit for deployment, management and resource sharing. Each independent unit is like a microservice.

The required code will be deployed in the cloud and performs individual actions. Here are some examples:

- Process pre-configured actions

- Process individual files

- Manage & process queued messages

- Handle API calls

Azure Functions has always supported a variety of programming languages and Java was added to the list recently. Other languages developers can use to implement events are C#, F#, Node.js, Python and PHP. Functions also accommodates scripting options like Bash, Batch and Powershell too.

Developers can save time deploying apps by uploading pre-compiled executables because they don't have to write the code from scratch.

It is possible to trigger Azure Functions in a number of ways. Here are the options to include a pre-defined time by:

- Leveraging CRON expressions,

- Changes in Azure Storage Blob containers,

- Changes to Azure Queues,

- Messages from Service Bus and

- HTTP triggers

These triggers define how the function initiates code execution. Hence, a single function should have only one trigger.

Azure Functions has a simple procedure not just to trigger code based on the data, but also to access and process that data. The data is connected to the code with input and output bindings.

Functions bindings also provide a great advantage. It helps the developers to simply interact with the data sources and services and not worry about the flow of data 'to and from' the function.

Azure Functions Input Output

Bindings make it easy for the developers because they don't have to know in-depth about the services they are interacting with, so they can easily swap in and out of services. The triggers and bindings in the Function help the developer avoid hardcoding the service in Azure Functions.

The function receives the data as input parameters. Developers can send data like queueing message for instance, with the help of the return value of the function, a collector object or an out parameter.

Azure Functions has bindings and these help the developers to interact with the data and services without worrying about data flow. They can configure the triggers and bindings in the Azure portal in the function.json file. The process of configuration is done either through the advanced editor or through the portal's user interface. The events themselves are monitored, so during each event trigger, the functions are used to process the events.

It is also possible to scale up and down as per demand. The cloud provider performs the scaling and the provisioning of resources automatically.

Azure Functions Monitor Events

Azure Functions has readymade templates containing a set of pre-defined functions that showcase a working scenario. This acts as a repository where you can proceed to perform more complex functions. Here are some of them:

HTTPTrigger - Functions support HTTP requests. You can customize the trigger to respond to webhooks. Webhooks themselves come in several versions to handle different providers like Github and Slack.

TimerTrigger - As the name suggests, this particular feature lets you run events on a schedule. This is provided automatically in all development environments, without the need for installing the package manually or registering the extension.

GitHub Webhook - Installing Webhooks can help you build or set up GitHub Apps. This would help in the integrations that subscribe to certain events on GitHub. Other functions include: update an external issue tracker, update a backup mirror, trigger CI builds and deploy to your production server.

Generic Webhook - This is almost like a messaging format through which you can chat with the application of your choice. The template listens for messages and posts updates specific by JSON payload.

CosmosDBTrigger - Functions supports trigger, input binding and output bindings for Azure Cosmos DB. Once CosmosDBTrigger listens for updates and updates across various partitions, the change feed would publish inserts and updates, but not deletions.

BlobTrigger - With the help of this template, you can trigger a function when a blob is created/updated. WebJobs SDK monitors the log files constantly to check for new or changed blobs.

QueueTrigger - QueueTrigger helps to trigger a function when a new update comes up on the queue. The queue message is the input to the function, and might even resend the message for a retry loop.

EventHubTrigger - SendEvents() emits messages to the event hub. Trigger() will listen on the event and emit a new one with an incremented counter. Functions provides support for trigger and output bindings for Event Hubs.

ServiceBusQueueTrigger - Through the service bus queue trigger, the developer can execute the code in the function every time the service bus received a message.

ServiceBusTopicTrigger - When the function is triggered with service bus topic message, the developer can publish and push messages to the topic.

Each Azure Function has a Git endpoint to help test your functions locally. Calling of the endpoint on the local server is dependent on the function you want to run and uses HTTP requests. Provisioning with Git endpoint and services like Visual Studio Team Services, GitHub and BitBucket also aids in hassle-free and undisturbed local development. Azure Functions integrates with a number of third-party services, on-premises services and several Azure services to trigger the functions or serve as input/output for the code.

These are the integrations supported by Azure Functions: Azure Cosmos DB, Azure Event Hubs, Azure Event Grid, Azure Mobile Apps (tables), Azure Notification Hubs, Azure Service Bus (queues and topics), Azure Storage (blob, queues, and tables), GitHub (webhooks), On-premises (using Service Bus), Twilio (SMS messages).

It is possible to monitor and troubleshoot functions with the logging capabilities provided within this compute-on demand, event driven experience.

Implementation of Azure Functions for Small Applications

Azure Functions makes the app development process more productive, and lets you launch serverless applications on Microsoft Azure. It helps in processing data, coordinating with different systems for IoT, integrating various processes and systems and building simple APIs and microservices. Azure Functions is perfect for tasks like image processing, processing orders, file maintenance and to run scheduled tasks.

With the fully managed compute platform, Azure Functions provides high-level reliability and security for your apps. Whenever you need to scale your app during peak seasons and high demand hours, this platform would manage it easily, and you don't have to worry about capacity planning concerns.

As it is scale based and on demand, you need to pay only for those resources that you use. Hence, developers can make full use of Azure Functions to build HTTP based end points that can be seamlessly accessed by a varied selection of applications, mobile and IoT devices.

Let's look at a couple of examples.

Example 1: Mobile app backend

Example 1: Mobile app backend
Image Credit: Microsoft Azure

The function processes a HTTP API call received from the mobile app. It then stores the processed output data for use in an analytics dashboard in Azure Cosmos DB (a globally distributed multi-model database). Once the data transfer happens, another function is triggered wherein notifications about the data are sent to the mobile app using Notifications Hub.

Example 2: Web application backend

Example 2: Web application backend
Image Credit: Microsoft Azure

The request from the web app is observed and queued. Azure Functions recognizes the event, triggers the function that corresponds to processing this request. The resulting output is then sent to the CosmosDB storage.

Conclusion

Azure Functions is designed to accelerate the process of application development and make it quick and simple. This is a far cry from traditional application development as it requires complex IT infrastructure that was often unwieldy, expensive and time consuming. Cloud computing improved things a bit, but with serverless computing, the latest buzzword in the world of app development, there was a sea of change in the way things moved.

Through Azure Functions serverless architecture, it is possible to stop worrying about all the infrastructure considerations you once faced, and just focus on creating and uploading proper code. Defining the triggers and events that will execute the code can follow this. The triggers could be from anywhere - another user's application, Notification Hubs, other cloud services etc. Azure Functions through EventHub and WebHook focus on specific, short tasks, alongside its integration with Azure Cosmos DB, Azure Mobile Apps and Azure Service Bus.

The highlight of Azure Functions is that you can write the code in easy to use web interfaces and build and debug them locally on your machine of choice. And it lets you leverage the same function code on other targets - IoT Edge, Azure Stack, Cloud Service, etc.

Interested in using Microsoft Azure to run your applications? We'll be happy to assist you!

Free Whitepaper: How Much Does it Cost to Develop a Mobile App?