Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github Acronyms

LinkedIn

Effortless instant (theoretically) infinite capacity to take several different actions

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

Overview

Why?

Microsoft Functions provides a free amount of “serverless” computing time each month in the Microsoft Azure cloud.

This tutorial aims to have you creating a useful Azure Function in Python after installing the “Azure Functions Core Tools (v4.x)”.

Use the Azurite V3 extension local storage emulator.

  1. Press F1, search for Azurite: Start, and run it.
  2. Confirm Azurite is running by checking the bottom bar.

You may be already familiar with my Quickly (below)

There are several ways to create and deploy Azure functions. For production, use an automated deploy mechanism to allow Azure Functions to get the latest version of Terraform or other IaC code from your version control system.

Functions are designed to bind to (integrate with) many other Azure (and outside) services:*

az-funcs-integrations-1464x1000

  1. Notification Hubs
  2. Event Grid
  3. Event Hubs
  4. Cosmos DB
  5. Twilio is external to Azure to send emails and mobile texts.
  6. Service bus to Topics and Queues.
  7. Storage: Almost all Azure Functions need to access Queues, Blobs, Table Storage via a Storage Account. REMEMBER: Any time a blob is uploaded to a Blob Storage container, a corresponding row in Table Storage is created.

Manual Portal GUI

Let’s jump right in:

  1. Open blade Function Apps in recents or Search.

    https://portal.azure.com/#browse/Microsoft.Web%2Fsites/kind/functionapp

  2. Click “+ Add” or “Create Function App”

    Function App Name Conventions

  3. Function names need to be all lower case and globally unique because it is a prefix to $MY_FUNC_APP_NAME.azurewebsites.net. Since the text is public to the world, it should be “work safe”:

    • “210515” is the current date
    • “java” (the runtime stack/language, see below)
    • “func”
    • 1234 is the result of $RANDOM

    Publish:

    PROTIP: Code or Docker Container, which cannot be selected if you’re selecting the Consumption plan.

    Run-time Stacks

  4. Runtime Stack (in CLI), with links to Developer Reference docs:
    • .NET (C# and F#) = “dotnet”
    • Node.js (JavaScript) = “node”
    • Python = “python”
    • Java
    • PowerShell Core = “powershell”
    • Custom Handler

    CAUTION: Not every version of a language is supporte. See https://docs.microsoft.com/en-us/azure/azure-functions/supported-languages#languages-by-runtime-version

  5. Version (of Runtime stack) is auto-selected of the latest version. “LTS” is for Long Term Service which is the most stable.

  6. Region = Location.

    QUESTION: There is no geo-redundant Function?

  7. Hosting:

    Storage account: One can be created automatically if you ignore the selection. Alterntely, click “create” for another modal dialog.

  8. Operating System: Windows is default. Select Linux

    NOTE: Later on you’re told “Editing functions in the Azure portal is not supported for Linux Consumption Function Apps.”

    Plan:

    REMEMBER: “Functions Premium” provides VNet connectivity, unlimited execution duration, and auto-scaling.

    “Consumption (Serverless)”, is pay-per-use pricing, preferred during low usage development. But no vNet service and no use of Docker containers.

    “App service plan” is used when vNet is needed with manual scaling. It has flat-rate predictable pricing with traditional Web Apps, API Apps, and Mobile Apps.

    Monitoring

    REMEMBER: Default “Application Insights” is “Yes”.

    PROTIP: Click on “No” during development because Insights uses up storage, which costs money.

    NOTE: Application Insights “(New)” is for a specific Region/Location.

  9. “Review + create”, then “Create”. Wait for the blue “Go to resource” to appear.

  10. Click “Add a function” link.

Individual Function Apps

  1. Get in the Portal Function Apps</a>
  2. Click a particular Function App Name

    There is no searching for the “Functions” blade within Function Apps.

  3. ”+ Add” pops up documentation because much occurs locally on your laptop.
def create_this_fapp(armSkuNameToFind):
    """ STATUS: INCOMPLETE DRAFT
    Based on https://www.perplexity.ai/search/python-code-to-create-an-azure-rv77D5o3Sfq_PfkS0Frjlg#0
    """
    app = func.FunctionApp()
    # import azure.functions as func
    @app.function_name(name="HttpExample")
    @app.route(route="HttpExample")
    def main(req: func.HttpRequest) -> func.HttpResponse:
        name = req.params.get("name")
        if not name:
            try:
                req_body = req.get_json()
            except ValueError:
                req_body = {}
            name = req_body.get("name")
            my_az_svc_region = get_cheapest_az_region(name)
        if name:
            return func.HttpResponse(f"Hello, {name}!")
        else:
            return func.HttpResponse("Please pass a name on the query string or in the request body.", status_code=400)

Triggers

A Function can be triggered (initiated) several ways.

Schedule triggers occur on a specific date/time.

  1. At the upper-right:

    Function Templates

  2. Select a template: blob (for run …

    • HTTP trigger - whenever it receives an HTTP request, responding based on data in the body or query string
    • Timer trigger - on a specified schedule
    • Azure Queue Storage trigger - whenever a message is added to a specified Azure Storage queue
    • Azure Service Bus Queue trigger - whenever a message is added to a specified Service Bus queue
    • Azure Service Bus Topic trigger - whenever a message is added to the specified Service Bus topic
    • Azure Blob Storage trigger - whenever a blob is added to a specified container
    • Azure Event Hub trigger - whenever an event hub receives a new event
    • Azure Cosmos DB trigger - whenever documents change in a document collection
    • IoT Hub (Event Hub) - whenever an IoT Hub receives a new event from IoT Hub (Event Hub)

    • SendGrid - sends a confirmation e-mail when a new item is added to a particular queue
    • Azure Event Grid trigger - whenever an event grid receives a new event

    • Kafka output - send messages to a specified Kafka topic
    • Kafka trigger - whenever a message is added to a specified Kafka topic
    • RabbitMQ trigger - whenever a message is added to a specified RabbitMQ queue
    • SignalR negotiate HTTP trigger - An HTTP triggered function that SignalR clients will call to begin connection negotiation

Local Setup (Azure Quickly)

  1. Run my az-local-setup.sh to install utilities on your laptop.

    https://github.com/wilsonmar/azure-quickly described at: wilsonmar.github.io/azure-quickly

  2. Initialize:

    func init MyFunctionProj --source-control

    If you get an error response such as this:

    node:events:342
       throw er; // Unhandled 'error' event
    

    QUESTION? reinstall Node and

    nvm install 8.0.0

    FunctionApp folder

    A sample from the Microsoft class

    https://github.com/MicrosoftLearning/AZ-204-DevelopingSolutionsforMicrosoftAzure

  3. Initialize a dotnet function:

    func init --worker-runtime dotnet --force

    A FunctionApp folder contains:

    *.gitignore

    • <a target=”_blank” href=”https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json”host.json</a>
    • local.settings.json
    • SharedCode
    • bin
    • various functions

    • C:\myfunctions\myMyFunctionProj.vscode\extensions.json

    Inside each individual function folder:

  4. Install extensions:

    func extensions install

    Durable functions

    Durable functions are stateful functions (in a stateless environment).

    “Orchestrator” functions are stateful. They define function Workflows. Calls other functions synchronously or asychronously.

    “Activity” functions are stateless. They are orchestrated.

    “Entity” functions are stateful. They read and update small pieces of state.

    “Client” functions are stateless. They send messages to trigger Orchestrator and Entity functions.

    The function manages state, checkpoints, and restarts.

    • Whenever function awaits, Checkpoint progress to Azure Storage

    Templates of Durables:

    • Durable Functions Entity HTTP starter - whenever it receives an HTTP request to execute an orchestrator function.
    • Durable Functions HTTP starter - whenever it receives an HTTP request to execute an orchestrator function.
    • Durable Functions activity - whenever an Activity is called by an orchestrator function.
    • Durable Functions entity (class) - A C# entity that stores state and represented by a class.
    • Durable Functions entity (function) - A C# entity that stores state and represented by a function.
    • Durable Functions orchestrator - An orchestrator function that invokes activity functions in a sequence.

    Chaining sequence:

    public static async Task<object> Run(DurableOrchestrationContext ctx){
      try {
         varx = await ctx.CallActivityAsync<object>("F1");
         vary = await ctx.CallActivityAsync<object>("F2", x);
         varz = await ctx.CallActivityAsync<object>("F3", y);
         return await ctx.CallActivityAsync<object>("F4", z);
      } catch (Exception) {
         # error handling/compensation goes here
      }
    }
    

    Fan out/Fan in parallel execution:

    public static async Task<object> Run(DurableOrchestrationContext ctx){
         var parallelTasks = new List<Task<int>>();
         // get a list of N work items to process in parallel
         object[] workBatch = await ctx.CallActivityAsync&L;object[]>("F1");
         for (int i = 0; i < workBatch.Length; i++) {
            Task<int> task = ctx.CallActivityAsync<int>("F2", workBatch[i]);
            parallelTasks.Add(task);
         }
         await Task.WhenAll(parallelTasks);
         # Delegate all N outputs and send result to F3:
         = prallelTasks.Sum(t => t.Result);
         x.CallActivityAsync("F3", sum);
    }
    

    Async HTTP APIs response:

    Human interaction:

  5. Development environment:

  6. Click “Function App” in the left menu.

    Notice there is an App “Service Plan”.

    https://aka.ms/AA4ul9b Community Library

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python

  7. PROTIP: Function Name cannot contain blanks.

    https://go.microsoft.com/fwlink/?linkid=2141857

  8. “Code + Test “

    Pull down the function.json file. It stores bindings for the C# script function. The binding encapsulates what triggers your Function and the data associated with it. In this case, any new blob added to the uploads/ path will trigger the function (blobTrigger) and the input (in) binding will populate the Function input parameters myBlob and name (as seen in the first line of the run.csx C# script).

    There are also output bindings. You want to store the blob information into a table and use an output binding to achieve that.

    In readme:

    For a BlobTrigger to work, you provide a path which dictates where the blobs are located inside your container, and can also help restrict the types of blobs you wish to return. For instance, you can set the path to samples/{name}.png to restrict the trigger to only the samples path and only blobs with “.png” at the end of their name.

  9. Add output JSON code and Save.

    run.csx files

  10. In the e run.csx file

    public static Upload Run(Stream myBlob, string name, ILogger log)
    {
     log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    
     return new Upload() {
         PartitionKey = "Uploads", 
         RowKey = Guid.NewGuid().ToString(), 
         Name = name,
         Length = myBlob.Length 
     };
    }
     
    public class Upload
    {
     public string PartitionKey { get; set; }
     public string RowKey { get; set; }
     public string Name { get; set; }
     public long Length { get; set; }
    }  

Azure Table

A Azure Table stores the records of every blob that is added to the uploads container.

Test the function by uploading files to blob storage and observing the entities that are created in Azure Tables.

Service Bus

az servicebus namespace create --name "${MY_SVC_BUS_NAMESPACE}" \
    --resource-group "${MY_RG}"

Service Bus Queue


Next

  1. PROTIP: Rather than accepting “functions47c313e5” or something else, come up with a convention, such as: “CXR1-dr-WUS-v01” for West US region.

    PROTIP: Include the region in your Function app name so you are less confused.

  2. Select your region, such as “West US” and

  3. Click the blue “Create + get started” button, then wait.

    Timer Scenarios

  4. Click the Timer sample to get started.

    We’ll cover the Data processing and Webhook + API later.

  5. Click JavaScript if you want the possibility for cross-platform code.

    C# only runs on Azure.

  6. Click “Create this function”.

    If you time-out here you’ll return to the Azure Resources Dashboard.

Run function project locally

Press F5 to run your function app.

The runtime will output a URL for any HTTP functions, which can be copied and run in your browser’s address bar.

To stop debugging, press Shift + F5.

Deploy Function code to Azure

Click the Deploy to Function App… () icon in the Azure: Functions panel.


Video Learning Courses

https://www.udemy.com/share/10dftt3@NJNXtnKIMmszWXCjzLXrWlE2lCGLtDwrykf0LjYkdTr_NSMNcvifOY3_dfsFHg==/ part of “Python SDK for Azure Bootcamp” on Udemy by Jose Portilla.

VIDEO: Intro to Azure Functions - What they are and how to create and deploy them by IAmTimCorey

Jeff Hollan joins Scott Hanselman:

by Adam Marczak - Azure for Everyone

Azure Services

Blog Storage

AI

https://www.youtube.com/watch?v=Sq8Cq7RZM2o Azure AI Foundry Overview by John Savill’s Technical Training

Vision, Language, Search, QnA API

  • https://learn.microsoft.com/en-us/samples/azure-samples/cognitive-services-python-sdk-samples/cognitive-services-python-sdk-samples/

  • https://learn.microsoft.com/en-us/credentials/certifications/azure-ai-engineer/?practice-assessment-type=certification

  • https://learn.microsoft.com/en-us/samples/azure-samples/cognitive-services-python-sdk-samples/cognitive-services-python-sdk-samples/

Secrity Purview

SC-200: Microsoft Certified: Security Operations Analyst Associate https://learn.microsoft.com/en-us/credentials/certifications/security-operations-analyst/?practice-assessment-type=certification Investigate, search for, and mitigate threats using Microsoft Sentinel, Microsoft Defender for Cloud, and Microsoft 365 Defender.

SC-401: Administering Information Security in Microsoft 365 Microsoft Certified: Information Security Administrator Associate https://learn.microsoft.com/en-us/credentials/certifications/information-security-administrator/?practice-assessment-type=certification

.NET

Social

Yochay Kiriaty (@yochayk)

Christopher Anderson @crandycodes PM

Random Notes

If you need to customize the JobHost, then you’re better off staying with an AppService WebJob.

Multi-tenancy “made it viable to serve small and medium businesses with world-class software”.

More on Serverless

This is one of a series on Serverless computing