Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Azure: Function As Service Introduction

Serverless Computing

Azure Function As Service Introduction

Code execution modus operandi where server-side logic is created to run on stateless, event-driven, short-lived compute containers that are run entirely by third parties (MS in our case).

Incidentally, computing without a server does not work without a server; instead, it allows a third party to manage and provide your server execution.

The Azure serverless offering from Microsoft Azure is called Azure Functions.

Some of the basic benefits:

  • Cost benefits/economy –imagine how impactive could it be if you are invoking the execution only when the method is called or executed.
  • Minimising the Vendor lock-in
  • Public cloud presences
  • Vendor managed circumnutating around
    • I. Application
    • II. Runtime
    • III. Operating system
    • IV. Virtualization
    • V. Hardware

Features

Idempotent

Mathematically a function is idempotent if it satisfies f(f(x)) = f(x), which means whenever it is applied twice, it gives the same result as if it were applied once. To ensure that the function remains idempotent is to ascertain that the same input isn’t processed twice. In an asynchronous, highly parallelized environment run by short-lived compute containers, we need to implement extra catches to prevent subsequent steps are not impacted by a particular step of execution. To ensure consistency, we need to store the necessary state information with our data, if no further processing is required, allowing the work to exit beautifully.

Azure functions in particular have some built-in protective devices that you can use. For example, for a storage queue triggered function, a queue message processing will be retired five times in case of failure, after which it will be dumped into a poison-message queue.

Stateless

Azure functions are the best examples of stateless protocols, meaning every time it is invoked, it will forget the details of its previous runs.

Asynchronous

These are all event-driven and asynchronous: I am sending across a request and am not waiting for a response from the other end. It is thus necessary to use non-blocking, awaiting calls in functions.

Scalability

Serverless computing makes it easy to scale the appliance by provisioning more compute resources as required and deallocating whenever the surge goes down. As a result, the developer can now take a break from the fear of failing overflow of user requests, while at the same time descoping resources when the peak time is over.

Execution limitations

  • Limited execution time
  • Startup latency
  • Scalability
  • Pay as you go
  • Reduced operational costs
  • Speedy deployment

Steps to create an azure function

Path:

Click Home → Create a resource → Compute → Function App

Step 1:

Select Subscription: note that all the resources in a given Azure Subscription are billed together

Select a Resource group: a resource group, by definition, is a container of resources that share the same lifecycle, permission and policies.

Function App name: give a proper name, without an ‘_’

Publish as a ‘Code’

Runtime stack: Here you can specify what you wish your source be compiled and exposed as/the runtime you are going to use:

.Net Core/Node.Js/Python/Java/Powershell core

Region: which GEO you are going to use to hoist your FAS

Click Review + Create

Step 2:

Click ‘Create’ to finish the wizard.

Alternately you can switch to ‘Hosting’ tab to validate:

Storage account: Implies the under what storage account under given Azure subscription you are creating the FAS.

Operating System: you can implement the service on Linux/Windows

Plan: This is where you can specify how you wish to pay as you run your FAS. The most easy-to-use one is ‘Consumption’, which ensures ‘pay-as-you-go’.

It will take some time for the Azure app to create and get deployed.

Step 3:

Once deployed, click ‘go to resources’ → Functions (on the left pane) → create new function → select ‘In portal’ → Continue →

  • a. select ‘Webhook + API’ to make your service run every time a HTTP request arrives
  • b. Times: it’s batchable

For now we are going with WebHook + API → create

Step 4:

Select Functions from left hand pane → On the content pane, you will see the following code:

#r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; 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.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; return name != null ? (ActionResult)new OkObjectResult($"Howdie, {name}, How are you?") : new BadRequestObjectResult("Please pass a name on the query string or in the request body"); }

Test it

You can try firing the function in several ways: following example shows how to hop your FAAS URL from POSTMAN:

Copy the FAAS URL and paste it in postman, add a new ‘key’ as ‘Name’ in the ‘Params’ tab and do a Post. It will print ‘Howdie %Your name value%’.

You can test it in the function window itself by pressing ‘run’

Create a new function

You can choose from palette of available templates:

  • HTTP trigger: a function that will be invoked whenever an HTTP request is received, responding based on data in the query parameter or requesting body(as JSON)
  • Timer trigger: this is a batchable which you make to run on specific intervals
  • Azure queue storage: It can be called whenever a message is added to a specific Azure storage queue.
  • Azure service bus: Whenever a message is added to the bus queue, you can execute your work.
  • Azure service bus topic trigger: When a message is added to a specific service bus topic it will be executed.
  • Azure blob storage trigger: When a blob file is added to a specific container.
  • Azure Cosmos DB trigger: a function whenever a document s added/updated in a doc collection.
  • SendGrid: this is a eMail trigger that can call your FAAS

These apart, there a load of other templates that can make your code freely be integrated with pointers like IoT, EventHubs, EventGrid, etc.

Example of Service Bus Queue trigger

Here is a small example that can tell you how to communicate between Logic apps with FAAS.

The following example shows a logic app that can post a message to a channel whenever the message is received as a HTTP request.

a. Create a new Logic app that has a HTTP request as a Trigger.

  • I. use the following example sample payload to generate schema: { “Name”: “Test0001”, “Description”: “This is test” }

b. Use a ‘compose’ action that acts as an outcome to the above.

c. Use ‘Microsoft teams’ → Post a message

  • I. Select Team ID( you might need to login with your subscription user Id and password)
  • II. Select a proper channel
  • III. In the message: you can select the outcome step-b

And as a result, net-net your logic app code looks like:

{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "Compose": { "inputs": "@triggerOutputs()['queries']", "runAfter": {} , "type": "Compose" } , "Post_a_message_(V3)": { "inputs": { "body": { "body": { "content": "<p>@{outputs('Compose')}</p>", "contentType": "html" } } , "host": { "connection": { "name": "@parameters('$connections')['teams']['connectionId']" } } , "method": "post", "path": "/v3/beta/teams/@{encodeURIComponent('**')}/channels/@{encodeURIComponent('*@thread.skype')}/messages" } , "runAfter": { "Compose": [ "Succeeded"] } , "type": "ApiConnection" } } , "contentVersion": "1.0.0.0", "outputs": {} , "parameters": { "$connections": { "defaultValue": {} , "type": "Object" } } , "triggers": { "manual": { "inputs": { "schema": { "properties": { "AcqMethod": { "type": "string" } , "Description": { "type": "string" } , "PartitionKey": { "type": "string" } , "RowKey": { "type": "string" } } , "type": "object" } } , "kind": "Http", "type": "Request" } } } , "parameters": { "$connections": { "value": { "teams": { "connectionId": "/subscriptions/****/providers/Microsoft.Web/connections/teams-1", "connectionName": "teams-1", "id": "/subscriptions/*****/providers/Microsoft.Web/locations/westus/managedApis/teams" } } } } }

d. Coming back to Azure functions, create a new function  select service bus queue trigger. You might be prompted to install the service bus extension, if you are doing it for the first time.

e. You can copy the following code and paste it here:

using System; using System.Threading.Tasks; using System.Net.Http; using System.Text; // Can also fetch from App Settings or environment variable private static string logicAppUri = @ "%Copy and paste the URL from the Logic app URL%"; private static HttpClient httpClient = new HttpClient(); public static async void Run(string myQueueItem, TraceWriter log) { log.Info($ "C# ServiceBus queue trigger function processed message: {myQueueItem}"); var response = await httpClient.PostAsync(logicAppUri, new StringContent(myQueueItem, Encoding.UTF8, "application/json")); log.Info(response.ToString()); }

f. Save and compile the function.

g. You can now test the function from Azure function app itself or post it using Postman.

Further actions: Dynamics 365

We can collaborate various integration to third party systems using FAAS and thereby making easy communication between Dynamics 365 implementation to communicate back and forth, depending on the type of communication/interaction to engage. You can select a FAAS to use when you have an ephemeral system that needs a low frequency-high volume/high frequency-low volume record to exchange. You can expose your available entities from D365 to a third-party system, by routing it through Logic apps and then using oAuth2 to relinquish security.

Function as service introduction
Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients