100% found this document useful (1 vote)
105 views34 pages

Module 02 Implement Azure Functions 1

Uploaded by

Xulfee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
105 views34 pages

Module 02 Implement Azure Functions 1

Uploaded by

Xulfee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

Module 02: Implement

Azure Functions

© Copyright Microsoft Corporation. All rights reserved.


Explore Azure Functions

Module
Agenda Develop Azure Functions

© Copyright Microsoft Corporation. All rights reserved.


Lesson 1: Explore Azure Functions

© Copyright Microsoft Corporation. All rights reserved.


Introduction

© Copyright Microsoft Corporation. All rights reserved.


Discover Azure Functions (1 / 4)

Overview
• 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.
• Azure Functions supports triggers, which are ways to start execution of your code, and bindings, which
are ways to simplify coding for input and output data.

© Copyright Microsoft Corporation. All rights reserved.


Discover Azure Functions (2 / 4)

© Copyright Microsoft Corporation. All rights reserved.


Discover Azure Functions (3 / 4)

Compare Azure Functions and Azure Logic Apps


Durable Functions Logic Apps
Development Code-first (imperative) Designer-first (declarative)

About a dozen built-in binding types, write Large collection of connectors, Enterprise Integration
Connectivity
code for custom bindings Pack for B2B scenarios, build custom connectors
Each activity is an Azure function; write
Actions Large collection of ready-made actions
code for activity functions

Monitoring Azure Application Insights Azure portal, Azure Monitor logs

Management REST API, Visual Studio Azure portal, REST API, PowerShell, Visual Studio

Execution context Can run locally or in the cloud Runs only in the cloud

© Copyright Microsoft Corporation. All rights reserved.


Discover Azure Functions (4 / 4)
Compare Functions and WebJobs
Functions WebJobs with WebJobs SDK
Serverless app model with automatic scaling Yes No

Develop and test in browser Yes No

Pay-per-use pricing Yes No

Integration with Logic Apps Yes No


Timer
Timer
Azure Storage queues and blobs
Azure Storage queues and blobs
Azure Service Bus queues and topics
Azure Service Bus queues and topics
Trigger events Azure Cosmos DB
Azure Cosmos DB
Azure Event Hubs
Azure Event Hubs
HTTP/WebHook (GitHub Slack)
File system
Azure Event Grid

© Copyright Microsoft Corporation. All rights reserved.


Compare Azure Functions hosting options (1 / 3)

When you create a function app in Azure, The hosting plan you choose dictates the
you must choose a hosting plan for your following behaviors:
app. • How your function app is scaled.
There are three basic hosting plans available for • The resources available to each function app
Azure Functions: instance.
• Consumption plan. • Support for advanced functionality, such as
• Functions Premium plan. Azure Virtual Network connectivity.

• App service (Dedicated) plan.

© Copyright Microsoft Corporation. All rights reserved.


Compare Azure Functions hosting options (2 / 3)

The following is a summary of the benefits of the three main hosting plans for Functions:

Consumption plan Functions Premium plan App service plan


The default hosting plan. Automatically scales based on Run your functions within an App
Scales automatically and you only demand using pre-warmed Service plan at regular App Service
pay when your functions are workers which run applications plan rates. Best for long-running
running. Instances of the with no delay after being idle, scenarios where Durable Functions
Functions host are dynamically runs on more powerful instances, can't be used.
added and removed based on the and connects to virtual networks.
number of incoming events.

There are two other hosting options which provide the highest amount of control and isolation in which
to run your function apps: ASE and Kubernetes.

© Copyright Microsoft Corporation. All rights reserved.


Compare Azure Functions hosting options (3 / 3)

Always on Storage account requirements


• If you run on an App Service plan, you should • On any plan, a function app requires a general
enable the Always on setting so that your Azure Storage account, which supports Azure
function app runs correctly. Blob, Queue, Files, and Table storage.
• On an App Service plan, the functions runtime • This is because Functions relies on Azure
goes idle after a few minutes of inactivity, so Storage for operations such as managing
only HTTP triggers will "wake up" your triggers and logging function executions, but
functions. some storage accounts do not support queues
and tables.

© Copyright Microsoft Corporation. All rights reserved.


Scale Azure Functions (1 / 3)

Overview
In the Consumption and Premium plans, Azure
Functions scales CPU and memory resources by
adding additional instances of the Functions host.

Runtime scaling
Azure Functions uses a component called the
scale controller to monitor the rate of events and
determine whether to scale out or scale in. The
scale controller uses heuristics for each trigger
type.

© Copyright Microsoft Corporation. All rights reserved.


Scale Azure Functions (2 / 3)

Scaling behaviors
Scaling can vary on a number of factors, and scale differently based on the trigger and language
selected. There are a few intricacies of scaling behaviors to be aware of:
• Maximum instances: A single function app only scales out to a maximum of 200 instances. A
single instance may process more than one message or request at a time though, so there isn't a
set limit on number of concurrent executions.
• New instance rate: For HTTP triggers, new instances are allocated, at most, once per second. For
non-HTTP triggers, new instances are allocated, at most, once every 30 seconds. Scaling is faster
when running in a Premium plan.

© Copyright Microsoft Corporation. All rights reserved.


Scale Azure Functions (3 / 3)

Limit scale out Azure Functions scaling in an App


You may wish to restrict the maximum number of service plan
instances an app used to scale out.
Using an App Service plan, you can manually scale
This is most common for cases where a out by adding more VM instances. You can also
downstream component like a database has enable autoscale, though autoscale will be slower
limited throughput. than the elastic scale of the Premium plan.

© Copyright Microsoft Corporation. All rights reserved.


Summary and knowledge check

© Copyright Microsoft Corporation. All rights reserved.


Lesson 2: Develop Azure Functions

© Copyright Microsoft Corporation. All rights reserved.


Introduction

© Copyright Microsoft Corporation. All rights reserved.


Explore Azure Functions development (1 / 4)

Overview
A function contains two important pieces: {
"disabled":false,
• Your code, which can be written in a variety of "bindings":[
languages // ... bindings here
• Some config, the function.json file. {
"type": "bindingType",
The function.json file defines the function's trigger, "direction": "in",
bindings, and other configuration settings. Every "name": "myParamName",
function has one and only one trigger. The runtime // ... more depending on binding
uses this config file to determine the events to }
monitor and how to pass data into and return data ]
}
from a function execution.

© Copyright Microsoft Corporation. All rights reserved.


Explore Azure Functions development (2 / 4)

Function app
A function app provides an execution context in Azure in which your functions run.
• It is the unit of deployment and management for your functions.
• A function app is comprised of one or more individual functions that are managed, deployed, and
scaled together.
• All of the functions in a function app share the same pricing plan, deployment method, and runtime
version.
• Think of a function app as a way to organize and collectively manage your functions.

© Copyright Microsoft Corporation. All rights reserved.


Explore Azure Functions development (3 / 4)

Folder structure FunctionsProject


| - MyFirstFunction
All code for a function app is located in a root | | - index.js
project folder that contains a host configuration | | - function.json
file. | - MySecondFunction
The host.json file contains runtime-specific | | - index.js
configurations and is in the root folder of the | | - function.json
function app. | - SharedCode

Specific folder structures required by the function | | - myFirstHelperFunction.js

app depend on language. To the right is an


| | - mySecondHelperFunction.js
| - node_modules
example of the folder structure of a JavaScript
| - host.json
project. | - package.json
| - extensions.csproj

© Copyright Microsoft Corporation. All rights reserved.


Explore Azure Functions development (4 / 4)

Local development environments


Functions makes it easy to use your favorite code editor and development tools to create and test
functions on your local computer.
• Your local functions can connect to live Azure services, and you can debug them on your local
computer using the full Functions runtime.
• The way in which you develop functions on your local computer depends on your language and
tooling preferences.

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (1 / 7)

Overview
• Triggers are what cause a function to run. A trigger defines how a function is invoked and a function
must have exactly one trigger.
• 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.
• You can mix and match different bindings to suit your needs.
• Triggers and bindings let you avoid hardcoding access to other services.

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (2 / 7)

Trigger and binding definitions {


"dataType": "binary",
Triggers and bindings are defined differently depending "type": "httpTrigger",
on the development language. "name": "req",
"direction": "in"
C# class library - decorating methods and parameters }
with C# attributes
Java - decorating methods and parameters with Java
annotations
JavaScript/PowerShell/Python/TypeScript - updating
function.json schema

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (3 / 7)

Binding direction
All triggers and bindings have a direction property in the function.json file:
• For triggers, the direction is always in
• Input and output bindings use in and out
• Some bindings support a special direction inout. If you use inout, only the Advanced editor is
available via the Integrate tab in the portal.

When you use attributes in a class library to configure triggers and bindings, the direction is provided in an
attribute constructor or inferred from the parameter type.

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (4 / 7)

Azure Functions trigger and binding "bindings": [


example
{
"type": "queueTrigger",
You want to write a new row to Azure Table storage "direction": "in",
whenever a new message appears in Azure Queue "name": "order",
"queueName": "myqueue-items",
storage.
"connection": "STORAGE_ACCT_SETTING"
This scenario can be implemented using an Azure },
Queue storage trigger and an Azure Table storage {
output binding. "type": "table",
"direction": "out",
"name": "$return",
"tableName": "outTable",
"connection": "STORAGE_ACCT_SETTING"
}

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (5 / 7)
...
C# script example public static Person Run(JObject order, ILogger log)
{
Here's C# script code that works with this return new Person() {
trigger and binding. PartitionKey = "Orders",
RowKey = Guid.NewGuid().ToString(),
Name = order["Name"].ToString(),
MobileNumber = order["MobileNumber"].ToString() };
}
public class Person
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
public string MobileNumber { get; set; }
}

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (6 / 7)
JavaScript example
The same function.json file can be used with a JavaScript function:

module.exports = function (context, order) {


order.PartitionKey = "Orders";
order.RowKey = generateRandomId();

context.done(null, order);
};

function generateRandomId() {
return Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
}

© Copyright Microsoft Corporation. All rights reserved.


Create triggers and bindings (7 / 7)
Class library example
public static class QueueTriggerTableOutput
{
[FunctionName("QueueTriggerTableOutput")]
[return: Table("outTable", Connection = "CONNECTION")]
public static Person Run(
[QueueTrigger("myqueue-items", Connection = "CONNECTION")]JObject order,
ILogger log)
{
return new Person() {
PartitionKey = "Orders",
RowKey = Guid.NewGuid().ToString(),
Name = order["Name"].ToString(),
MobileNumber = order["MobileNumber"].ToString() };
}
...

© Copyright Microsoft Corporation. All rights reserved.


Connect functions to Azure services (1 / 2)

Overview Connection values


• Your function project references connection • When the connection name resolves to a single
information by name from its configuration exact value, the runtime identifies the value as a
provider. It does not directly accept the connection string, which typically includes a
connection details, allowing them to be secret. The details of a connection string are
changed across environments. defined by the service to which you wish to
connect.
• The default configuration provider uses
environment variables. These might be set • However, a connection name can also refer to a
by Application Settings when running in the collection of multiple configuration items.
Azure Functions service, or from the local Environment variables can be treated as a
settings file when developing locally. collection by using a shared prefix that ends in
double underscores __.

© Copyright Microsoft Corporation. All rights reserved.


Connect functions to Azure services (2 / 2)

Configure an identity-based connection Grant permission to the identity


• Some connections in Azure Functions are • Whatever identity is being used must have
configured to use an identity instead of a permissions to perform the intended actions.
secret. Support depends on the extension
• This is typically done by assigning a role in
using the connection.
Azure RBAC or specifying the identity in an
• In some cases, a connection string may still be access policy, depending on the service to
required in Functions even though the service which you are connecting.
to which you are connecting supports identity-
based connections.

© Copyright Microsoft Corporation. All rights reserved.


Exercise: Create an Azure Function by using Visual Studio Code

Task1 : Create your local Task 2: Run the function Task 3: Sign in to Azure
project locally

Task 4 : Publish the project to Task 5: Run the function in Task 6: Clean up resources
Azure Azure

© Copyright Microsoft Corporation. All rights reserved.


Summary and knowledge check

• Explain the key components of a function and


how they are structured
• Create triggers and bindings to control when a
function runs and where the output is directed
• Connect a function to services in Azure
• Create a function by using Visual Studio Code
and the Azure Functions Core Tools

© Copyright Microsoft Corporation. All rights reserved.


Lab 02: Implement task
processing logic by using
Lab 04: Constructing
Azure Functions
a polyglot data
solution
http://aka.ms/az204labs 45
minutes

http://aka.ms/az204labs

© Copyright Microsoft Corporation. All rights reserved.


© Copyright Microsoft Corporation. All rights reserved.

You might also like