Introduction to Azure Functions.
An event-based serverless compute experience to accelerate your development. Scale based on demand and pay only for the resources you consume.
3. What is Azure Functions
• Run small piece of code (functions) in cloud
• Supported languages – C#, Python, Node.js, PHP
• Serverless Code architecture
• Event driven on-demand compute
• Triggered by events occurring in Azure, 3rd Party systems, OnPrem
• Scale based on demand
• Integrated security
• Leverage existing Azure and other SaaS services
• Pay per-use pricing model
4. Application of Azure Functions
Azure functions enables you to:
1. Process data based on triggers
2. Working with IOT data
3. Build Simple APIs
4. Microservices
5. Any scheduled operations
5. Azure Function Integrations
Azure Cosmos DB
Azure Event Hubs
Azure Mobile Apps
Azure Notification Hubs
Azure Service bus (Queues and Topics)
Azure Storage (Tables, Blobs and Queues)
Github (Webooks)
On Premises (using Service Bus)
Twilio (using SMS)
6. Azure Functions - Pricing
Factors determining the Pricing are:
1. Executions – Total number of executions for all functions per month. (0.20$ per million executions)
2. Resource Consumption – Measured in Gigabyte Seconds (GB-s) (Average memory size in GB X
Execution time in seconds) (0.000016 /GB-s)
Storage account is created with each functions App. Any usage is charged at standard storage
rates.
Azure functions Pricing calculator - https://azure.microsoft.com/en-us/pricing/details/functions/
7. Consumption Plan
Consumption Plan – Pay for the time your code runs
Dynamic scaling based on the number of incoming requests (Scale out based on load)
Pricing based on Resource consumption and Executions
Free grant of 1 million requests and 400,000 GB-s
Max execution time of 5 mins for a function
Max memory limit of 1.5 GB per instance of function host.
8. App Service Plan
With App Service plan there is no additional cost for running Azure Functions
Azure functions run on Dedicated VMs (Basic, Standard, Premium) allocated to your App
Service.
Azure functions host is always running
When to use App Service Plan:
Function to run continuously or near continuously
Need more memory than provided by the consumption plan
Run longer than maximum execution time provided in the consumption plan
Under utilized VMs available for consumption.
VM decouples the both runtime and memory size.
Scale out using Auto scale / different App service plan.
9. Azure Integration Options
Azure provides support for gluing disparate systems
Define input, actions, conditions and output.
Runs on triggers or schedules
Some of choices available in Azure are:
◦ Microsoft Flow
◦ Azure Logic Apps
◦ Azure Functions
◦ App Service Web Jobs
10. Azure Functions vs Web Jobs
WebJobs Core SDK
WebJobs Extensions
WebJobs Scripts Runtime
Triggers
Inputs /
Outputs
Language
Support
Azure Functions Runtime
11. Azure Functions – Triggers & Bindings
• Triggers are ways to start executing your Azure function code
• Bindings are declarative way to bind data to the Azure function
• All triggers have input data
• Data coming from services become input values for function code
• To output data to another service use the return value of the method.
13. Azure Function Code
Write function in language of choice
Save the code and configuration files in the same folder
Configuration is saved in file function.json
function.json file defines the function bindings and other configuration data
14. Azure Function App
Grouping of multiple Azure functions
All the functions share the same pricing plan, deployment
and runtime version
Functions can be written in different languages
Runtime can invoke functions multiple times in parallel.
Max concurrency depends on the type of trigger and
resource used by other functions in the function App.
Azure functions pulse can be used to monitor the average
execution time.
15. Azure Functions CSX for C#
CSX allows to run less boilerplate code and focus on the function
Refer any assemblies and namespaces directly
Parameters are bind to the CSX function based on the name property specified in the
functions.json configuration
Types of parameter supported depends on each binding
Use the Tracewrite for any logging
Reference external assemblies - #r "AssemblyName“
Reuse .csx code using the load call. (#load "mylogger.csx“)
16. Azure Functions demo
Basic Azure Function Example
Timer Triggered Function Example
Github Webhook Function Example
Adding message to Azure Queue using Functions
Demo Workflow – Github -> Webhook -> Service Bus -> Blob Storage -> Send Grid
17. Develop and Debug locally
Support for developing and debugging Azure functions locally using CLI tools
Need to have installed the Azure Function Core Tools
Create a new Azure function App ( func init MyFunctionProj )
Create a new Azure function ( func new --language C# --template HttpTrigger --name
MyHttpTrigger )
Run functions locally ( func host start )
Publishing function App to Azure (func azure functionapp publish <FunctionAppName> )
18. Azure Functions Runtime
Azure functions runtime enables to run Azure functions on-premises.
Enables identical development experience as the cloud service.
Run locally before committing to the cloud.
Azure functions runtime consist of the following:
Azure Functions Runtime Management Role – Host for management of functions on-premises
Azure Functions Worker Role – Windows container used to execute the Azure functions
Needs Windows Server 2016 or Windows 10 Creators Update with access to a SQL
Server instance.
Azure Functions is a Worker Role, which is a job that runs in the background. Worker roles in App Services are implemented with WebJobs, a feature of Web Apps, because they run on the same tenant of a deployed Web App. That’s why you can consider Azure Functions like an Azure WebJobs SDK as a Service, because you write a Web Job in a PaaS manner, without having to consider how a WebJob works or how it is structured. Azure Functions obtains the two models that it is able to run from Azure WebJobs: continuous running and triggered running.
Integrated security - Protect HTTP-triggered functions with OAuth providers such as Azure Active Directory, Facebook, Google, Twitter, and Microsoft Account
Bring your own dependencies - Functions supports NuGet and NPM, so you can use your favorite libraries.
Simplified integration - Easily leverage Azure services and software-as-a-service (SaaS) offerings.
Open-source - The Functions runtime is open-source and available on GitHub
Blog Trigger – Process blobs when they are added to containers
Event hub trigger – Respond to events delivered to Azure Event hub
Generic webhook – Process webhoot Http requests from any service supports webhooks
Github webhook – Responds to events that occur on github repositories
HttpTrigger – Trigger an execution of function by using Http request
Queue Trigger – Responds to messages that arrive in Azure storage queue.
Service Bus Queue / Topic Trigger – Enables to take action for messages delivered on the Azure service bus queue.
Timer Trigger – Execute cleanup or batch operation on a predefined schedule.
Azure functions host runs the Azure functions in either mode.
Plan cannot be changed after creation.
Function App requires an Azure Storage account that supports Blob, Queue, and Table storage.
Internally Azure Functions uses Azure Storage for operations such as managing triggers and logging function executions.
Some storage accounts do not support queues and tables, such as blob-only storage accounts (including premium storage) and general-purpose storage accounts with ZRS replication.
This plan scales automatically and you are charged for compute resources only when your functions are running. On a Consumption plan, a function can run for a maximum of five minutes.
When using the Consumption hosting plan, function code files are stored on Azure Files shares on the main storage account. When you delete the main storage account, this content is deleted and cannot be recovered.+
When using a blob trigger on a Consumption plan, there can be up to a 10-minute delay in processing new blobs if a Function App has gone idle. Once the Function App is running, blobs are processed immediately.
Azure Functions uses a component called the scale controller to monitor the rate of events and determine whether to scale out or scale down. The scale controller uses heuristics for each trigger type. For example, when using an Azure Queue Storage trigger, it scales based on the queue length and the age of the oldest queue message.
The unit of scale is the function app. When scaled out, more resources are allocated to run multiple instances of the Azure Functions host. Conversely, as compute demand is reduced, function host instances are removed. The number of instances is eventually scaled down to zero when no functions are running within a function app
If you run on an App Service Plan, you should enable the Always On setting so that your Function App runs correctly. On an App Service Plan, the functions runtime will go idle after a few minutes of inactivity, so only HTTP triggers will actually "wake up" your functions. This is similar to how WebJobs must have Always On enabled.+
App Service plans represent a set of features and capacity that you can share across your apps. App Service plans give you the flexibility to allocate specific apps to a set of resources and further optimize your Azure resource utilization.
App Service plans define:
1. Region (West US, East US, etc.)
2. Scale count (one, two, three instances, etc.)
3. Instance size (Small, Medium, Large)
4. SKU (Free, Shared, Basic, Standard, Premium)
Azure WebSites or Web Apps
Azure Mobile Services or Mobile Apps
Azure API Apps
Azure Logic Apps
Whether to use Functions or WebJobs ultimately depends on what you're already doing with App Service. If you have an App Service app for which you want to run code snippets, and you want to manage them together in the same DevOps environment, you should use WebJobs. If you want to run code snippets for other Azure services or even 3rd-party apps, or if you want to manage your integration code snippets separately from your App Service apps, or if you want to call your code snippets from a Logic app, you should take advantage of all the improvements in Functions.
The runtime uses function.json to determine the events to monitor and how to pass data into and return data from function execution.
Disabled – True to disable the running of the function
Bindings – Each bindings has some common settings and bindings specific settings.
Type – Binding type Ex – queueTrigger
Direction – in or out – whether receving data into function or sending out
Name – Name used for the bound data in the binding. For C# it is arugment name and for JS it is the key value pair
Think of a function app as a way to organize and collectively manage your functions.
host.json file contains some runtime-specific configuration and sits in the root folder of the function app.
1. Reusing .csx code using #load
2. Environment variables
3. Using NuGet packages with project.json
4. Referencing custom assemblies using bin folder
5. Referencing External Assemblies # r AssemblyName
Azure Functions Management Role -
Provides a host for the management of your Functions on-premise. This role performs the following tasks:
Hosting of the Azure Functions Management Portal, which is the the same one you see in the Azure portal. This lets you develop your functions in the same way as you would in the Azure portal. Distributing functions across multiple Functions workers.
Providing a publishing endpoint so that you can publish your functions direct from Microsoft Visual Studio.
Azure Functions Worker Role –
Worker roles are deployed in the Windows Container and this where the function code executes
You can deploy multiple Worker Roles throughout your organization and is a key way in which customers can make use of spare compute power.