Road To AZ-204 - Implement Azure Functions

Intro

 
This article's intention is to explain the main skills measured in this sub-topic of the AZ-204 Certification. Azure Functions and Azure Durable functions are the main components that will have their fundamentals explained here alongside a practical example.
 
This certification is very extensive and this article approaches only the main topics, make sure you know deep down those components before taking the exam. Another great tip is doing exam simulators before the official exam in order to validate your knowledge.
 

What is the Certification AZ-204 - Developing Solutions for Microsoft Azure?

 
The AZ-204 - Developing Solutions for Microsoft Azure certification measures designing, building, testing, and maintaining skills of an application and/or service in the Microsoft Azure Cloud environment. It approaches, among others, the following components,
  • Azure Virtual Machines;
  • Docker;
  • Azure Containers;
  • Service Web App;
  • Azure Functions;
  • Cosmos DB;
  • Azure Storage;
  • Azure AD;
  • Azure Key Vault;
  • Azure Managed Identities;
  • Azure Redis Cache;
  • Azure Logic App;
  • Azure Event Grid;
  • Azure Event Hub;
  • Azure Notification Hub;
  • Azure Service Bus;
  • Azure Queue Storage.
 
Target Audience
 
Any IT professional willing to improve his knowledge in Microsoft Azure is encouraged to take this certification, it is a great way to measure your skills within trending technologies. But, some groups of professionals are more keen to take maximum advantage of it,
  • Azure Developers, with at least 1 year of experience with Microsoft Azure;
  • Experienced Software Developers, looking for an Architect position in a hybrid environment;
  • Software Developers, working to move applications to the cloud environment.
Skills Measured
 
According to today's date, the skills that are measured in the exam are split as follows,
  • Develop Azure compute solutions (25-30%)
  • Develop for Azure storage (10-15%)
    • Develop solutions that use Cosmos DB storage
    • Develop solutions that use blob storage
  • Implement Azure security (15-20%)
    • Implement user authentication and authorization
    • Implement secure cloud solutions
  • Monitor, troubleshoot, and optimize Azure solutions (10-15%)
    • Integrate caching and content delivery within solutions
    • Instrument solutions to support monitoring and logging
  • Connect to and consume Azure services and third-party services (25- 30%)
    • Develop an App Service Logic App
    • Implement API Management
    • Develop event-based solutions
    • Develop message-based solutions
Updated skills can be found in the AZ - 204 Official Measured Skills Website.
 

Benefits of Getting Certified

 
The main benefit here is having a worldwide recognized certification that proves that you have knowledge of this topic. Among intrinsic and extrinsic benefits, we have:
  • Higher growth potential, as certifications are a big plus;
  • Discounts and deals in Microsoft products and partners, like PluralSight and UpWork;
  • MCP Newsletters, with trending technologies;
  • Higher exposure on LinkedIn, as recruiters usually search for specific certifications;
  • Higher salary, you will be more valuable to your company;
  • Unique happiness when getting the result and you were approved, knowing that all your efforts were worth it;
 

Main skills Measured by this Topic

 

What are Azure Functions?

 
Azure Functions is part of the serverless services offered by Azure and as a serverless service, you do not need to handle the server infrastructure. With Azure Functions your unique concern is about developing your code, setting the bindings, and configuring input and outputs.
 
Azure Functions is an open-source service and integrates with many other Azure Services, for example, Azure DevOps to create CI-CD for your Azure functions in order to automate your build and deployment processes or Azure Storage to store your Azure Function output. Azure Functions also supports NPM and Nuget libraries.
 
Azure Functions can be developed using C#, Java, JavaScript, Python, and PowerShell. It has 3 different payment plans, as follows, 
  • Consumption plan, you only pay for the used time. Good for occasional uses;
  • In the premium plan, you have a dedicated machine so you pay if you are running your Azure Functions or not,  as your machine will be always up so you avoid Cold Start. Good for high usage;
  • App Service plan, run your Azure Functions in your existing App Service Plan infrastructure with no additional costs. Good if you have an App Service Plan and do not need to scale your Azure Functions. 

What are Azure Function Bindings?

 
When you connect your Azure Function to another resource, in order to trigger your function or retrieving and sending data, is called a Binding. Azure Functions support a wide variety of bindings options to be used, from HTTP Webhooks to SignaIR, and you can use your bindings in your Azure Function in three different forms, as follows:
  • Input Binding, where the data from your resource is going to be your Azure Function input parameter;
  • Output Binding, where your Azure Function will write the output data in your resource;
  • Trigger Binding, where an event in your resource is going to trigger your Azure Function. 

What are Azure Function Triggers?

 
When an event occurs that makes your Azure Function run it's called a trigger. An Azure Function must have always one trigger that is supposed to start your Azure Function every time that this event is fired.
 
Azure Function has a wide variety of triggers to be used, from a new message in a Service Bus queue to a call to an API Endpoint, and those triggers are organized in three different forms, as follows: 
  • Data operations, those triggers can be used with a wide variety of data providers and are fired according to when your data has changed;
  • Timers, those triggers are fired according to pre-configured time intervals;
  • Webhooks, those triggers are fired according to a call to an API Endpoint request.  

What are Azure Durable Functions?

 
Azure Durable Functions are an extension of the Azure Functions where they have the ability to have stateful workflows running in a serverless architecture. By running in serverless architecture, while Azure handles the Azure Function state, checkpoints, and restarts, your only worry is to provide your code and select the Azure Durable
Function type and Azure Durable pattern that best fits with your solution.
 
Azure Durable Functions is also an open-source project and has the price calculation exactly like Azure Functions. 
 

Azure Durable Functions Types 

 
Azure Durable functions are long-running functions used to process data following a designed stateful procedural workflow. It has four different types, each type representing one role, where they interact among themselves in order to represent your workflow, as follows:
 
Client Type
 
Client Functions are the type of Durable Function used to interact, by triggering or calling other Durable Functions, their main features are as follows,
  • Have a Durable Client Output Binding;
  • Used to trigger Orchestration and Entity Functions, by using the Orchestration Client Binding or Entity Client Binding;
  • Can interact with running Orchestration and Entity Functions;
Orchestrator Type
 
Azure Durable Orchestrator Functions are used to design the workflows with its units of work, they must have the same output result no matter how many times it was executed. Its main features are as follows,
  • Has code constraints, in order to keep it being deterministic;
  • Used to design complex workflows;
  • Are executed when prompted by its Orchestration Trigger;
  • Are durable and reliable.
 
Activity Type
 
Azure Durable Functions of the Activity type are the functions used to organize tasks inside the Durable Orchestration Functions. Each activity must represent one task from your workflow inside your Durable Orchestration Function. Its main features are as follows,
  • Can only be triggered from an Orchestration Function;
  • Are the unit of work from the Orchestration Functions;
  • Can be executed sequentially, in parallel, or both;
  • Are great at handling input and outputs.
Entity Type
 
Azure Durable Functions of the Entity type are usually a small piece of code used to manipulate states, like a class with its methods manipulating its properties, whereas they may interact with another Entity Function, an Orchestration Function, and Client Functions. Its main features are as follows,
  • Used to update Entities states;
  • Has an internal identity, consisted of the pair Entity Name and Entity Key;
  • May have or not a specific state;
  • Are executed when prompted by its Entity Trigger;
  • Its operations are executed serially, one after another, to prevent conflicts;
 

Azure Durable Functions Patterns

 
https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview?tabs=csharp#application-patterns
  • Function Chaining Pattern, chaining one function output into another function's input;
  • Fan-out/Fan-in Pattern, executing multiple functions in parallelism and then waiting for them to finish;
  • Async HTTP API Calls Pattern, used with usually 2 endpoints whereas one start the long-running function and the other provides details about its status; 
  • Monitor Pattern, works as a recurring process used to monitor status until having specific conditions met;
  • Human Interaction Pattern, workflow waits for human interaction;
  • Aggregator Pattern used to aggregate data from multiple source inputs into a single entity. 
Azure Durable Functions can be developed using C#, JavaScript, Python, F#, and PowerShell.
 

Creating an Azure Function 

 
There are many different ways to create your Azure Function but we will be approaching the 2 most used ways, through Visual Studio and Azure Portal.
 

Creating Azure Function through Azure Portal

 
From your Azure Portal, go to Resource and then Function App. Click on the Create button.
 
Road To AZ-204 - Implement Azure Functions
 
From the basics menu, input your Subscription, Resource Group, technology stack, and the Azure Region. 
 
Road To AZ-204 - Implement Azure Functions
 
From the hosting menu, input your storage account, operation system, and your plan.
 
Road To AZ-204 - Implement Azure Functions
 
Monitoring and Tags menus will not be approached here so click on the Review + create button, after a few seconds Azure will finish its validation and you will click on the create button. After a few seconds for Azure to deploy your Azure Function, you will be ready to overview your Azure Function.
 
Road To AZ-204 - Implement Azure Functions
 
Access your Azure Function URL
 
Road To AZ-204 - Implement Azure Functions
 
Now let's add logic to our Azure Function. From your Azure Function, go to the Functions menu and then on Functions click on Add.
 
Road To AZ-204 - Implement Azure Functions
 
From the templates menu, pick the HTTP trigger, name the function, select the authorization level, and create. After a few seconds, you will be redirected to your HttpTrigger already with some code on it 
 
Road To AZ-204 - Implement Azure Functions
 
Click on Test/Run to see the result from your Azure Function 
 
Road To AZ-204 - Implement Azure Functions
 
You could also click on Get Function URL and test your HTTP trigger end-to-end. Copy your URL and test it on a browser, here I had added my name as a query string in order to have this result.
 
Road To AZ-204 - Implement Azure Functions
 

Creating Azure Function through Visual Studio using C#

 
To use Azure projects with your Visual Studio you must have the Azure Development workload installed.
 
Add a new project of type Azure Functions, name your project as AzureFunctionVisualStudio
 
Road To AZ-204 - Implement Azure Functions
 
Select the HTTP Trigger alongside with your storage account and authorization level. Then click on the create button.
 
Road To AZ-204 - Implement Azure Functions
 
Your project structure and function file must look like this. I had done some small changes to customize the Function.
 
Road To AZ-204 - Implement Azure Functions
 
If you push F5 you will be able to test your Azure Function
 
Road To AZ-204 - Implement Azure Functions
 
To publish on Azure and deploy your Azure Function, right-click on your project and then click on Publish. 
 
Road To AZ-204 - Implement Azure Functions
 
Select Azure and then Azure Functions (Windows). Click on Create a new Azure Function.
 
Road To AZ-204 - Implement Azure Functions
 
Select your subscription, resource group, plan type, Azure location, Azure storage and click on create.
 
Road To AZ-204 - Implement Azure Functions
 
After success validation from Azure, click on finish. You will be prompted with your publish profile, validate your data, and click on Publish.
 
Road To AZ-204 - Implement Azure Functions
 
After successful deployment, validate your Azure Function through Azure Portal
 
Road To AZ-204 - Implement Azure Functions
 
Copy your Function Url and test it through your browser. I have added a personalized name as queryString here.
 
Road To AZ-204 - Implement Azure Functions

Implementing Input and Output Bindings for a Function

 
As you can see, the previous exercise has already made use of Input and Output Bindings. The input binding is of type httpTrigger and the output type is of type HTTP.
 
Road To AZ-204 - Implement Azure Functions
 

Implementing Output Bindings with Azure Storage for a Function

 
Inside your Azure Function, go to Integration and then Add output
 
Road To AZ-204 - Implement Azure Functions
 
Select your Binding Type with its configuration. Here we will be writing to a Blob Storage named sampleOutputBlob.
 
Road To AZ-204 - Implement Azure Functions
 
After successful deployment, you will see your outputs updated
 
Road To AZ-204 - Implement Azure Functions
 
It is time to update your code to save the output into the blob. You only need one new line to configure this output, and the final run.ps1 file looks like this, 
  1. using namespace System.Net  
  2.   
  3. # Input bindings are passed in via param block.  
  4. param($Request, $TriggerMetadata)  
  5.   
  6. # Write to the Azure Functions log stream.  
  7. Write-Host "PowerShell HTTP trigger function processed a request."  
  8.   
  9. # Interact with query parameters or the body of the request.  
  10. $name = $Request.Query.Name  
  11. if (-not $name) {  
  12.     $name = $Request.Body.Name  
  13. }  
  14.   
  15. $body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."  
  16.   
  17. if ($name) {  
  18.     $body = "Hello, $name. This HTTP triggered function executed successfully."  
  19. }  
  20.   
  21. # Associate values to output bindings by calling 'Push-OutputBinding'.  
  22. Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{  
  23.     StatusCode = [HttpStatusCode]::OK  
  24.     Body = $body  
  25. })  
  26.   
  27. Push-OutputBinding -Name sampleOutputBlob -Value $body  
Road To AZ-204 - Implement Azure Functions
 
Execute your function to test your new output. Then, go check the blob data
 
Road To AZ-204 - Implement Azure Functions
 

Implementing Function Triggers by Using Webhooks

 
The names here must be confusing sometimes but using a Webhook is the same as using an HTTP Trigger just like we used above. It is exactly the same trigger but it is called sometimes by Webhook and some other times by HTTP Trigger.
 

Implementing Function Triggers by Using Data Operations

 
Now let's create another trigger to be fired when a Blob file is created. We will be reading blobs from the outcontainer and writing it into secondoutcontainer.
 
From your Azure Function, under Functions then add a new Azure Blob storage trigger.
 
Road To AZ-204 - Implement Azure Functions
 
Road To AZ-204 - Implement Azure Functions
Input the blob name, storage account and path. After creating the function, check on Integration to add a new Output.
 
Road To AZ-204 - Implement Azure Functions
Road To AZ-204 - Implement Azure Functions
 
Your run.ps1 must look like this
  1. # Input bindings are passed in via param block.  
  2. param([byte[]] $InputBlob, $TriggerMetadata)  
  3.   
  4. # Write out the blob name and size to the information log.  
  5. Write-Host "PowerShell Blob trigger function Processed blob! Name: $($TriggerMetadata.Name) Size: $($InputBlob.Length) bytes"  
  6. Push-OutputBinding -Name outputBlob -Value $($TriggerMetadata.Name)  
Checking result
 
Road To AZ-204 - Implement Azure Functions
 

Implementing Function Triggers by Using Timers


Let's create a new timer trigger that will write another blob file with the time on it. This output container will be named outtimercontainer.
 
From your Azure Function, under Functions then add a new Timer trigger. 
 
Road To AZ-204 - Implement Azure Functions
 
Set your scheduler for running every each 2 minutes
  1. 0 */2 * * * *  
After configuring your output, adjusting your run.ps1 files must look like
  1. # Input bindings are passed in via param block.  
  2. param($Timer)  
  3.   
  4. # Get the current universal time in the default string format.  
  5. $currentUTCtime = (Get-Date).ToUniversalTime()  
  6.   
  7. # The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.  
  8. if ($Timer.IsPastDue) {  
  9.     Write-Host "PowerShell timer is running late!"  
  10. }  
  11.   
  12. # Write an information log with the current time.  
  13. Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"  
  14.   
  15. Push-OutputBinding -Name outputBlob -Value $currentUTCtime  
Checking result
 
Road To AZ-204 - Implement Azure Functions
 

Implementing Azure Durable Functions with Visual Studio and C#

 
In this practical sample, we are going to apply the 4 types of Durable Functions in the following scenario which consists of calling one rest API and count the number of chars in its output. It is triggered by an HTTP Trigger.
 
Pre-requisites
Create the Project of type Azure Functions
 
Road To AZ-204 - Implement Azure Functions
 
Here I will name it as DurableFunctionsSample. 
 
Road To AZ-204 - Implement Azure Functions
 
Let's start with an empty project and use the Storage Emulator as a storage account
 
Road To AZ-204 - Implement Azure Functions
 
Here is the result from the project creation
 
Road To AZ-204 - Implement Azure Functions
 
Right-click on the project and then Add and then New Azure Function. Here I will name it as OrchestrationSample.
 
Road To AZ-204 - Implement Azure Functions
 
Road To AZ-204 - Implement Azure Functions
 
Visual Studio will create an Orchestration Function class with code already on it. So, push F5 and check the output.
  1. public static class OrchestrationSample  
  2.    {  
  3.        [FunctionName("OrchestrationSample")]  
  4.        public static async Task<List<string>> RunOrchestrator(  
  5.            [OrchestrationTrigger] IDurableOrchestrationContext context)  
  6.        {  
  7.            var outputs = new List<string>();  
  8.   
  9.            // Replace "hello" with the name of your Durable Activity Function.  
  10.            outputs.Add(await context.CallActivityAsync<string>("OrchestrationSample_Hello""Tokyo"));  
  11.            outputs.Add(await context.CallActivityAsync<string>("OrchestrationSample_Hello""Seattle"));  
  12.            outputs.Add(await context.CallActivityAsync<string>("OrchestrationSample_Hello""London"));  
  13.   
  14.            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]  
  15.            return outputs;  
  16.        }  
  17.   
  18.        [FunctionName("OrchestrationSample_Hello")]  
  19.        public static string SayHello([ActivityTrigger] string name, ILogger log)  
  20.        {  
  21.            log.LogInformation($"Saying hello to {name}.");  
  22.            return $"Hello {name}!";  
  23.        }  
  24.   
  25.        [FunctionName("OrchestrationSample_HttpStart")]  
  26.        public static async Task<HttpResponseMessage> HttpStart(  
  27.            [HttpTrigger(AuthorizationLevel.Anonymous, "get""post")] HttpRequestMessage req,  
  28.            [DurableClient] IDurableOrchestrationClient starter,  
  29.            ILogger log)  
  30.        {  
  31.            // Function input comes from the request content.  
  32.            string instanceId = await starter.StartNewAsync("OrchestrationSample"null);  
  33.   
  34.            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");  
  35.   
  36.            return starter.CreateCheckStatusResponse(req, instanceId);  
  37.        }  
  38.    }  
Road To AZ-204 - Implement Azure Functions
 
Calling the endpoint
 
Road To AZ-204 - Implement Azure Functions
Road To AZ-204 - Implement Azure Functions 
 

Customizing the Azure Durable Function

 
Now we are customizing this durable function in order to have our scenario applied. The Azure Durable Function must be triggered by an HTTP Trigger and must return the chars count from a rest API.
 
Client Function Result
 
The client function will be updated to receive the endpoint URI as a query string input and send it to the Orchestrator Function. 
  1. [FunctionName("OrchestrationSample_HttpStart")]  
  2.      public static async Task<HttpResponseMessage> HttpStart(  
  3.          [HttpTrigger(AuthorizationLevel.Anonymous, "get""post")] HttpRequestMessage req,  
  4.          [DurableClient] IDurableOrchestrationClient starter, ILogger log)  
  5.      {  
  6.          var qs = req.RequestUri.ParseQueryString();  
  7.          object endpoint = qs.Get("endpointUri");  
  8.   
  9.          string instanceId = await starter.StartNewAsync("OrchestrationSample", endpoint);  
  10.          log.LogInformation($"Started orchestration with ID = '{instanceId}'.");  
  11.          log.LogInformation($"Endpoint = '{endpoint}'.");  
  12.   
  13.          return starter.CreateCheckStatusResponse(req, instanceId);  
  14.      }  
Orchestration Function Result
  1. [FunctionName("OrchestrationSample")]  
  2.         public static async Task<string> RunOrchestrator(  
  3.             [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)  
  4.         {  
  5.             string endpoint = context.GetInput<object>().ToString();  
  6.             var request = new DurableHttpRequest(HttpMethod.Get, new Uri(endpoint));  
  7.             DurableHttpResponse endpointResponse = await context.CallHttpAsync(request);  
  8.             if (endpointResponse.StatusCode != HttpStatusCode.OK)  
  9.             {  
  10.                 throw new ArgumentException($"Failed to contact endpoint: {endpointResponse.StatusCode}: {endpointResponse.Content}");  
  11.             }  
  12.             log.LogInformation($"Information retrieved from endpoint = {endpointResponse.Content}.");  
  13.   
  14.             string[] words = endpointResponse.Content.Split(" ");  
  15.             log.LogInformation($"Words count = {words.Count()}.");  
  16.   
  17.             var entityId = new EntityId("OrchestrationSample_Counter""charCounter");  
  18.             context.SignalEntity(entityId, "reset");  
  19.   
  20.             foreach (string word in words)  
  21.             {  
  22.                 context.SignalEntity(entityId, "Add", word);  
  23.             }  
  24.   
  25.             await context.CallActivityAsync("OrchestrationSample_LogBlob", endpoint.Replace("/""bar"));  
  26.             int count = await context.CallEntityAsync<int>(entityId, "Get");  
  27.   
  28.             return $"Endpoint: {endpoint} has the total of {count} chars";  
  29.         }  
Activity Function Result
 
The activity function will be dumb here, only logging the endpoint name into a blob.
  1. [FunctionName("OrchestrationSample_LogBlob")]  
  2.      public static async Task LogBlobAsync([ActivityTrigger] string name, [Blob("sample-blob/{name}", FileAccess.Write)] CloudBlockBlob blobStream, ILogger log)  
  3.      {  
  4.          await blobStream.UploadTextAsync(DateTime.UtcNow.ToString());  
  5.          log.LogInformation($"Blob Created {DateTime.UtcNow}.");  
  6.      }  
Entity Function Result 
  1. [FunctionName("OrchestrationSample_Counter")]  
  2.       public static void Counter([EntityTrigger] IDurableEntityContext ctx, ILogger log)  
  3.       {  
  4.           log.LogInformation($"Entity operation= {ctx.OperationName}.");  
  5.   
  6.           switch (ctx.OperationName.ToLowerInvariant())  
  7.           {  
  8.               case "add":  
  9.                   var sum = ctx.GetInput<string>().Length - ctx.GetInput<string>().Count(char.IsWhiteSpace);  
  10.                   ctx.SetState(ctx.GetState<int>() + sum);  
  11.                   break;  
  12.               case "reset":  
  13.                   ctx.SetState(0);  
  14.                   break;  
  15.               case "get":  
  16.                   ctx.Return(ctx.GetState<int>());  
  17.                   break;  
  18.           }  
  19.       }  

Testing the Azure Durable Function local

 
I distributed a lot of logging here intentionally in order to understand better how the Orchestration works. 
 
Road To AZ-204 - Implement Azure Functions 
Road To AZ-204 - Implement Azure Functions
 
Road To AZ-204 - Implement Azure Functions
 
Publish to Azure
 
Right-click on your project click on Publish, select Azure, and then Azure Function App ( Windows )
 
Road To AZ-204 - Implement Azure Functions
 
Create a new Azure Function
 
Road To AZ-204 - Implement Azure Functions
 
Road To AZ-204 - Implement Azure Functions
 
After Azure validates and creates your Azure Function then click on Finish and you will be redirected to your publish profile. Now publish it.
 
Road To AZ-204 - Implement Azure Functions
 
After success deployment, validate your Azure Function from Azure Portal
 
Road To AZ-204 - Implement Azure Functions
 
Test the Azure Durable Function from Azure 
 
Call URL: https://durablefunctionssampletva.azurewebsites.net/api/OrchestrationSample_HttpStart?endpointUri=https://reqres.in/api/users?page=3 
 
Road To AZ-204 - Implement Azure Functions
 
Blob created 
 
Road To AZ-204 - Implement Azure Functions 
External References