Azure Functions
Azure Functions
What is serverless?
- AWS: AWS Lambda
- Microsoft Azure: Azure Functions
- GCP: Google Cloud Functions
- IBM: IBM Cloud Code Engine
Microsoft Azure has a range of serverless offerings at the moment AKS, Azure Functions etc. Most pioneer serverless computing or FaaS offering from Microsoft Azure is Azure Functions. Initially Azure launched WebJobs to solve the problem of event triggered computation. Later Azure utilized the concept of same and came up with serverless computation named Azure Functions.
Here we will discuss about Microsoft Azure Functions.
What is Azure Functions?
Azure Functions is a cloud service available on-demand that provides all the continually updated infrastructure and resources needed to run your applications. You focus on the pieces of code that matter most to you, and Functions handles the rest. Functions provides serverless compute for Azure. You can use Functions to build web APIs, respond to database changes, process IoT streams, manage message queues, and more.
Azure Functions is a serverless solution is "compute on-demand" offering that allows user to implement code as Function which will runs on dynamically allocated resources responding to critical events.
Azure takes care of scalability of Functions. When demand increases, more application instances are added automatically to the service and when requests fall, all extra resources and application instances drop off automatically. This feature has an issue of cold start. Cold start is the process when a new instance handles its first request. Even though Azure Functions launches fast, it still takes time to launch an instance and to make all the appropriate service connections.
For more details about cold start please check out Understanding serverless cold start and Cold Starts in Azure Functions.
Some of the reasons of using Functions:
-
Azure functions are easier to write and deploy. Also there are templates
of different functions available in different programming languages.
- Azure functions are fast to execute as there is less resource utilization, startup time, initialization time.
- Azure functions are compute-on-demand and that is scalable. When demand of execution increases, more resources are allocated automatically to the service and when requests fall, all extra resources and application instances drop off automatically.
- Azure functions support multiple programming languages including C#, F#, Java, JavaScript, PowerShell, TypeScript, and Python.
-
Azure Functions is serverless. Hence there is no hassle of managing
infrastructure.
- Azure Functions can be build, tested, and deployed in Azure portal, Azure CLI and Visual Studio.
- Azure Functions can be executed and debugged in local as well. Also recently Microsoft recently announced KEDA. This provides a new way of running Azure Functions on Kubernetes with KEDA. If you’re running container-hosted Functions on your own infrastructure or another public cloud, you can run them in Kubernetes, using KEDA to support launching new instances as required.
Common use cases of Azure Functions:
Azure Functions can be used in different use cases and to design a complete serverless solution of complex use cases. But it can not definitely replace an entire website. Best suited use cases are scheduled tasks, small APIs, backend event triggered computing etc.
Here are some common use cases are:
- HTTP triggered web API
- Processing file uploads
- Respond to database change feed
- Scheduled Tasks
- Logic to respond to events and messages
- Real time data processing
- IoT data stream
Hosting plans for Azure Functions:
-
Consumption plan: This is the default hosting plan. In this plan
user pays only when their functions are running.
-
App Service plan: Here function runs with App Service Plans. This
is suitable for long running jobs.
- Premium plan: Automatically scales based on demand using pre-warmed workers which run applications with no delay after being idle, runs on more powerful instances, and connects to virtual networks. This plan can be used to avoid cold start based upon the use cases.
Triggers and Bindings
Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.Binding:
Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as input bindings, output bindings, or both. Data from bindings is provided to the function as parameters.So each function must have one trigger and can have numbers of bindings depends upon the requirement. Binding can be of two types
- Input
- Output
Hands on with Azure Functions
Now let's
see how we can create Azure Functions and deploy a function app there. There
are four ways of how we can create Azure Functions App which acts as
containers for Functions- Azure Portal
- Azure CLI and Azure Function Core Tool
- ARM template
- Visual Studio and Visual Studio Code
Previously with Functions 1.x, it was possible to deploy multiple Functions of different language in single Azure Function App. But to provide better runtime experience from Functions 2.x, Azure allows to deploy multiple Functions of single programming language in a Functions App.
For simplicity I will create a Functions App using Azure portal. To do so first login to Azure portal and one should have at least free tier subscription. Now search Function App and click create. Once the "Create Function App" blade opens below basic information needs to be provided:
- Subscription: Select your subscription
- Resource Group: Either select an existing Resource Group or create a new Resource Group by clicking "Create new" link
- Function App name: Provide an unique name for Function App name.
- Publish: Select how you want to publish Functions as Code or Docker container. For our demo, I am using code.
- Runtime stack: Please runtime stack as per your requirement. For us, I have used Node.js
- Version: Select version of the runtime stack
-
Region: Select in which region you want to host Function App.
Next in Hosting section provide below information
- Operating system: Select whether you want to use Linux or Windows.
-
Plan: Select appropriate hosting plan for Function App. Default
plan is Consumption.
- Enable Application Insights: Select whether you want to enable Application Insights
- Application Insights: Select an existing or create new Application Insights
- Region: Region is selected same as Function App.
Next provide suitable Tags for Function App and click Review+create for validation. Once validation is successful, click create to create Function App.
Once the deployment is completed, Resource Manager will notify about the same. Also we need to create a storage account as that is required to deploy Functions in Function App. But before we proceed with inspecting Function App, I will also share how we can create same using Azure CLI. For that either you can install Azure CLI in local or use Azure Cloud Shell Bash. I will be using Windows 10 system. I have already logged in to my Azure account and used below PowerShell script to create Function App using Azure CLI.
$ResourceGroup = "Function-RG" $Location = "eastus" $StorageAccName = "pracfuncstracc" $FunctionAppName = "my-first-functionapp" az group create -n $ResourceGroup -l $Location az storage account create -n $StorageAccName -g $ResourceGroup -l $Location --sku Standard_LRS az functionapp create -n $FunctionAppName -g $ResourceGroup -s $StorageAccName -c $Location --runtime node --runtime-version 10 --os-type Linux --functions-version 3
Once Above script executes successfully, Function App named "my-first-functionapp" will be created in Resource Group named "Function-RG" and a storage account named "pracfuncstracc".
Now either you create Function App through portal or Azure CLI, Function App
will be visible in portal.
Let's try to access the URL of the Function App.
Woo Hoo! Default page has been rendered.
Wait! Did you notice any difference between both the approach? 😊
There is one extra resource has been provisioned in Azure CLI, a storage account and same storage account has been linked to Function App while creating Function App. What is the significance of the same? Why we did not create the same through portal? 😊We will explore that later while deploying a Function to Function App. Function App actually acts as a container for one or more Functions.
Now let's create a Function.
Create first Function
- VS code is installed
- Node.js is installed
- Azure Core Tools package is installed
- Azure Functions extension for VS code is installed
Click "Create New Project" in Azure Functions extension panel in VS code to create a Functions project and scaffold a Function using one of the supported language. It will prompt to select directory for project, language, template(triggers for function), name and authorization level for the Function.
Steps and selection for our project:
- Select folder for Function project - FirstFunction
- Language - JavaScript
- Template - HTTP trigger
- Name - HttpTriggeredFunction
- Authorization level - Anonymous
After all the above steps VS code will scaffold a Function project with first HTTP trigger Function using JavaScript. Here is the structure of the Function project
Each Function has it's own files inside parent directory of the Function project. In our case HttpTriggeredFunction is present inside FirstFunction (project directory). Each Function has separate index.js and function.json. Parent project directory contains shared host.json, local.settings.json, package.json and proxy.json. Let's explore all the files and their significance.
- index.js contains the JavaScript implementation of the exported async function which will be executed during event trigger which HTTP request for our Function.
First argument of the function is always context object. The runtime
uses the context object to pass data to and from your function and the
runtime. Used to read and set data from bindings and for writing to logs,
the context object is always the first parameter passed to a function.
-
function.json contains information about trigger and both type bindings,
i.e., input and output binding. We can add as many input and output
binding as we want with details like type, direction(in or out), name
etc.
There are different ways of accessing trigger and bindings in
implementation. Azure documentation provides extensive information about
Bindings
.
-
The host.json file contains global configuration for all Functions which
is used while running Function locally or in Azure. Details of the
host.json can be found in
Azure documentation.
- The local.settings.json contains configurations for Function running in local. For hosted Functions these are maintained in Application Settings and Connection strings.
- The proxy.json allow user to define proxy for different Functions.
Run Function locally
Publish Function to Azure
Function runtime settings
Application settings
- APPINSIGHTS_INSTRUMENTATIONKEY
- AzureWebJobsStorage
- FUNCTIONS_EXTENSION_VERSION
- FUNCTIONS_WORKER_RUNTIME
- WEBSITE_RUN_FROM_PACKAGE

Comments
Post a Comment