Azure function
Similar to AWS Lambda, Azure function or azure function app, is a small piece of code that runs from a serverless service.
It supports many programming languages including C#, Java, Python, JavaScript, TypeScript, Powershell, etc.
It can be triggered to run by calling a Web Hook (a http binding address), Event Hubs, Queue Storage, a blob storage new file, or simply timer (on schedule). Once triggered it can take different inputs such as cosmos db, or storage queue, etc. and outputs to blob storage, event hubs, etc.
A simply example
[FunctionName("BlobTriggerCShsarp")] //The [] indicates attributes of the function, attribute = 'FunctionName', the property = 'BlobTriggerCShsarp'
public static void Run(
//BlobTrigger attribute indicates a new file trigger for a container. {name} expression maps to new file full path, file content goes to the Stream object.
[BlobTrigger("my-blob-container/{name}")]Stream myBlob,
[Blob("output-container/{name}")]Stream outBlob, //the output blob container
string name //reference to the 'name' expression in the trigger so it can be used later in the function body
)
{
int length = myBlob.length
//do something and output, here simply copy the new file from myblob to outBlob
myBlob.CopyTo(outBlob)
}
Create a Function App from portal
it can be based on windows/linux
It needs a consumption plan for the serverless service
It needs a Runtime Stack, originally only .Net Node.js, Java and PowerShell. It should have supported Python now.
It always needs a storage account to store your function code as well!
Once created, it is shown as "App Service" under your resource group. Just click into it.
On the side menu bar, it shows a list of your functions, note the App Service contains multiple functions.
On the right panel, it shows the platform features of the App service, which has function app settting, networking, API, plan, etc. In the Function App Setting (the main one), you can set daily usage quota, runtime version, read/write mode.
New Funtion
From the portal you can click New Function and there will be an editor for that, although a better way is doing it through VS Code.
On creating function, the first thing is selecting the trigger, which can be HTTP trigger, Timer trigger, Queue Storage trigger, Service Bus Queue trigger. After selecting the trigger, it automatically generates a template code. Below is an example with HTTP trigger:
#r "Newtonsoft.Json" //reference ot the Json library
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitves;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request");
string name = req.Quey["Name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsysnc(); // block until the request is read
dynamic data = JsonCovert.DeserializeObject(requestBody);
name = name ?? data?.name; //??checks if name is null, if not use name, otherwise data?.name. The ?. checks if data is null, if not, use data.name otherwise null.
return name !=null ? (ActionResult)new OkObjectResult($"hello, {name}"): new BadRequestObjectResult("Missing name");
}
From the portal there is Test tab, which is like Postman that you can send a get/post request with parameters.
From the Manage under a function, you can get the key for access the function url.
From the Monitor, view function run log, or use Application Insights.
Host Configuration
Host.json file in the Function App setting, contains global configs, e.g.
{
"http":{
"routePrefix":"api",
"maxOutstandingRequests": 200,
"maxConcurrentRequests": 100,
"dynamicThrottlesEnabled": true
}
}
Develop Function App in VS Code
Make sure Azure Accounts and Azure Functions add ons are installed. They are for connecting to Azure and compile Function codes.
Go to an empty project folder, hit F1 to and type function to bring up all the Azure Function options. Select Create Function. Then it initializes the project folder. Then select language, e.g. C# or Python, and the trigger, e.g. BlobTrigger.
Now it creates the scaffold of the projects, with host.json, local.setting.json, and program.cs files.
using System;
...
namespace Company.Function
{
public static class ProcessMyBlob
{
[FunctionName("Hello")]
public static void run([BlobTrigger("container/{name}", Connection="storage_account")]Stream myBlob, string name, ILogger log)
{
log.LogInformation("helloworld")
}
}
}
Note the container, storage account names are configed in the local.setting.json file.
Make some changes, e.g. the output, etc
Right click the function app and Deploy
Done