0 ratings0% found this document useful (0 votes) 89 views10 pagesAzure Function Scenarios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
‘i124, 9:95 AM ‘Azure Functions Scenarios | Microsoft Leam
Azure Functions scenarios
Article + 05/24/2023
We often build systems to react to a series of critical events. Whether you're building a web
API, responding to database changes, processing event streams or messages, Azure
Functions can be used to implement them.
In many cases, a function integrates with an array of cloud services to provide feature-rich
implementations. The following are a common (but by no means exhaustive) set of
scenarios for Azure Functions.
Select your development language at the top of the article.
Process file uploads
There are several ways to use functions to process files into or out of a blob storage
container. To learn more about options for triggering on a blob container, see Working
with blobs in the best practices documentation.
For example, in a retail solution, a partner system can submit product catalog information
as files into blob storage. You can use a blob triggered function to validate, transform, and
process the files into the main system as they're uploaded.
nitps:leam microsot.comlon-us/azurefazureunctonsunctons-scenarios Pplots=progranvming-language-csharpprocess-fle-uploads
sH0‘11124, 9:35 AM ‘Azure Functions Scenarios | Microsoft Leam
fo
maa
mO
ma)
Catalog data files
saved to blob storage A function validates ..and sends it to
and processes the the comme Q.
data... system
The following tutorials use an Event Grid trigger to process files in a blob container:
For example, using the blob trigger with an event subscription on blob containers:
7
[FunctionName("ProcessCatalogbata”)]
public static async Task Run([BlobTrigger("catalog-uploads/{nane}", Source =
BlobTriggerSource.EventGrid, Connection = "")]Strean
myCatalogdata, string name, ILogger log)
{
Log. Loginformation($"c# Blob trigger function Processed blob\n Name: {name}
\n Size: {myCatalogbata.Length} Bytes");
using (var reader = new StreanReader(myCatalogbata) )
{
var catalogEntry = await reader.ReadLineAsync();
while(catalogentry !=null)
{
// Process the catalog entry
Move
catalogentry = await reader.ReadLineAsync();
+
,
hntps:leam mierosof. conver-usiazurefazure-unctions/tunctions-scenaviospivets=programming-language-csharpprocess-fle-uploads 2110sits, 896 am Azure Funcions Scenarios | Microsoft eam
‘© Upload and analyze a file with Azure Functions and Blob Storage
‘© Automate resizing uploaded images using Event Grid
* Trigger Azure Functions on blob containers using an event subscription
Real-time stream and event processing
So much telemetry is generated and collected from cloud applications, loT devices, and
networking devices. Azure Functions can process that data in near real-time as the hot
path, then store it in Azure Cosmos DB for use in an analytics dashboard
Your functions can also use low-latency event triggers, like Event Grid, and real-time
outputs like SignalR to process data in near-real-time,
App or device
producing data
x)
a
Afunction processes .and sendlsit to dashboar, &
the data ‘Cosmos DB Visualizations
8 Ct
— *
i=
=
Lt
Event Hubs ingests
telemetry data
For example, using the event hubs trigger to read from an event hub and the output
binding to write to an event hub after debatching and transforming the events:
ct
[FunctionName("ProcessorFunction")]
public static async Task Run(
[EventHubTrigger(
"xInput_EH_Nane’
Connection
ConsumerGroup
[EventHub(
"xoutput_eH_Namex",
Connection = "OutputEventHubConnectionString")]
TasyncCollector outputMessages,
PartitionContext partitionContext,
nputEventHubConnectionString",
%Input_EH_ConsumerGroup%")] EventData[] inputMessages,
hntps:leam mierosof. conver-us/azurefazure-unctions/tunctions-scenaviospivets=programming-language-csharppracess-fle-uploads 3it0‘11124, 9:35 AM ‘Azure Functions Scenarios | Microsoft Leam
ILogger log)
var debatcher = new Debatcher(1og) 5
var debatchedMessages = await debatcher.Debatch(inputMessages,,
partitionContext.PartitionId) ;
var xformer = new Transformer(1og) ;
await xformer.Transform(debatchedMessages, partitionContext.Partitiontd,
outputMessages)
3
‘* Streaming at scale with Azure Event Hubs, Functions and Azure SQL
‘© Streaming at scale with Azure Event Hubs, Functions and Cosmos DB
* Streaming at scale with Azure Event Hubs with Kafka producer, Functions with Katka
trigger and Cosmos DB
‘* Streaming at scale with Azure loT Hub, Functions and Azure SQL
* Azure Event Hubs trigger for Azure Functions
‘* Apache Kafka trigger for Azure Functions
Machine learning and Al
Besides data processing, Azure Functions can be used to infer on models.
For example, a function that calls a TensorFlow model or submits it to Azure Al services can
process and classify a stream of images.
Functions can also connect to other services to help process data and perform other Al-
related tasks, like text summarization
hntps:leam mierosof. conver-usiazurefazure-unctions/tunctions-scenaviospivets=programming-language-csharpprocess-fle-uploads
40‘11124, 9:35 AM
Classification
requests
Queue
‘Azure Functions Scenarios | Microsoft Leam
Classification
results
+ >— |___—">
Azure Functions
fe :
‘* Sample: Text summarization using Al Cognitive Language Service
Run scheduled tasks
Functions enables you to run your code based on a cron schedule that you define.
Check out how to Create a function in the Azure portal that runs on a schedule.
A financial services customer database, for example, might be analyzed for duplicate
entries every 15 minutes to avoid multiple communications going out to the same
customer.
hntps:leam mierosof. conver-usiazurefazure-unctions/tunctions-scenaviospivets=programming-language-csharpprocess-fle-uploads
510‘11124, 9:35 AM ‘Azure Functions Scenarios | Microsoft Leam
ey
A function cleans a database
every 15 minutes...
..deduplicating en: Q
based on business loys:
ce
[FunctionName("TimerTriggercsharp")]
public static void Run([TimerTrigger("@ */15 * * * *")]TimerInfo myTiner,
TLogger log)
{
if (nyTimer. TsPastDue)
{
og. Loginformation( “Timer is running late!");
>
log. Loginformation($"C# Timer trigger function executed at:
{DateTime.Now}");
// Perform the database deduplication
‘* Timer trigger for Azure Functions
Build a scalable web API
‘An HTTP triggered function defines an HTTP endpoint. These endpoints run function code
that can connect to other services directly or by using binding extensions. You can
compose the endpoints into a web-based API.
You can also use an HTTP triggered function endpoint as a webhook integration, such as
GitHub webhooks. In this way, you can create functions that process data from GitHub
events. To learn more, see Monitor GitHub events by using a webhook with Azure
Functions.
hntps:leam mierosof. conver-usiazurefazure-unctons/tunctions-scenariospivets=programming-anguage-csharpiipracess-fle-uploads e110‘11124, 9:35 AM ‘Azure Functions Scenarios | Microsoft Leam
7 —~ Pp — 3
HTTP Functions with
endpoint HTTP trigger
For examples, see the following:
o
[FunctionNane("InsertNane")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, “post")] HttpRequest req,
[CosmosD8(
databaseName: “ny-database",
collectionName: "ny-container",
ConnectionStringsetting =
“CosmosbbConnectionstring") ]TAsyncCollector documentsOut,
‘ILogger log)
{
string requestBody = await new StreamReader(req.Body) .ReadToEndAsync();
dynamic data = JsonConvert .Deserializedbject (requestBody) ;
string name = data?.name;
if (name
{
null)
return new BadRequestObjectResult("Please pass a name in the request
body json");
}
// Add a ISON document to the output container.
await documentsout.Addasync(new
{
// create a random 10
id = System.Guid.NewGuid(). ToString(),
name = nane
ns
return new OkResult();
* Article: Create serverless APIs in Visual Studio using Azure Functions and API
Management integration
* Training: Expose multiple function apps as a consistent API by using Azure API
Management
hntps:leam mierosof. conver-usiazurefazure-unctions/tunctions-scenaviospivets=programming-language-csharpprocess-fle-uploads 70sive, 9.5m ‘Azure Functions Scenarios | Microsoft Learn
‘* Sample: Web application with a C# API and Azure SQL DB on Static Web Apps and
Functions
© Azure Functions HTTP trigger
Build a serverless workflow
Functions is often the compute component in a serverless workflow topology, such as a
Logic Apps workflow. You can also create long-running orchestrations using the Durable
Functions extension, For more information, see Durable Functions overview.
PA
b> hy 2G PLPOH
Cn >
Q
CZ ln o. b
Get status
Flexible automated long-running Http-based async long-
process monitoring running APIs Fiunaniinteraaion
* Tutorial: Create a function to integrate with Azure Logic Apps
© Quickstart: Create your first durable function in Azure using C#
‘* Training: Deploy serverless APIs with Azure Functions, Logic Apps, and Azure SQL
Database
Respond to database changes
There are processes where you might need to log, audit, or perform some other operation
when stored data changes. Functions triggers provide a good way to get notified of data
changes to initial such an operation
hntps:leam mierosof. conver-us/azurefazure-unctions/tunctions-scenaviospivets=programming-language-csharppracess-fle-uploads ato‘11124, 9:35 AM ‘Azure Functions Scenarios | Microsoft Leam
> By —
Database Functions Notifica' q
change trigger
Consider the following examples:
© Article: Connect Azure Functions to Azure Cosmos DB using Visual Studio Code
* Article: Connect Azure Functions to Azure SQL Database using Visual Studio Code
* Article: Use Azure Functions to clean-up an Azure SQL Database
Create reliable message systems
You can use Functions with Azure messaging services to create advanced event-driven
messaging solutions.
For example, you can use triggers on Azure Storage queues as a way to chain together a
series of function executions. Or use service bus queues and triggers for an online ordering
system.
—| _l th %
Es 11 oo
Cel |
Request made Request queued A function processes
ina web app in Service Bus the request...
ends 0
Cosmos ws
The following article shows how to write output to a storage queue.
¢ Article: Connect Azure Functions to Azure Storage using Visual Studio Code
‘© Article: Create a function triggered by Azure Queue storage (Azure portal)
And these articles show how to trigger from an Azure Service Bus queue or topic.
Azure Service Bus trigger for Azure Functions
hntps:ileam mierosof. conver-us/azurefazure-unctons/functions-scenariospivets=programming-anguage-csharppracess-fle-uploads 910‘i124, 9:95 AM ‘Azure Functions Scenarios | Microsoft Leam
Next steps
[-ceting rtd with Are Function
nitps:leam microsot.comlon-us/azurefazureunctonsunctons-scenarios Pplots=progranvming-language-csharpprocess-fle-uploads 10110