Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call another function with in an Azure function

I have written 3 functions as follows

  1. create users in db
  2. fetch users from db
  3. process users

In [3] function, I will call [2] function to get users using Azure function url as below:-

https://hdidownload.azurewebsites.net/api/getusers

Is there any other way to call Azure function with in another Azure function without full path as above?

like image 515
Galet Avatar asked Sep 20 '17 07:09

Galet


People also ask

How do I call Azure function from another Azure function?

Three ways of Azure Cross Function Communication In case you need to call another function directly you can choose HTTP protocol, and send your request to the endpoint of the other function or choose to use Durable Functions. In case it is indirect or asynchronous you can choose to use a queue.

Can a function invoke another function?

It is important to understand that each of the functions we write can be used and called from other functions we write. This is one of the most important ways that computer scientists take a large problem and break it down into a group of smaller problems.

Can Azure function have two triggers?

There are no plans to support multiple triggers per Function. You will have to create a Function for each EventHub. If there is common code that may be shared between Functions, you may move them to a helper method that can be called from each Function.

How to call two functions at the same time in Azure Functions?

You can do it directly by calling the 2nd function as a normal C# static method. But in this case you lose benefits of Azure Functions scaling and distributing (for example, based on server load your 2nd function may be called from different part of the world).

How to create Azure function in Azure Functions?

Now Provide a unique name for your Azure Function, Choose the Authorization level as Function, and then finally click on the Add button to create the Azure Function. Now you can able to see below, Our Azure Function is created successfully. Click on the Code + Test link from the left side navigation on the Function page as highlighted below,

How do I call a Logic App from an azure function?

Call logic apps from Azure functions When you want to trigger a logic app from inside an Azure function, the logic app must start with a trigger that provides a callable endpoint. For example, you can start the logic app with the HTTP, Request, Azure Queues, or Event Grid trigger.

Can you call workflows from inside of an azure function?

You can also call logic app workflows from inside an Azure function. Azure Functions provides serverless computing in the cloud and is useful for performing certain tasks, for example: Extend your logic app's behavior with functions in Node.js or C#.


2 Answers

There's nothing built-in in Function Apps to call one HTTP function from other functions without actually making the HTTP call.

For simple use cases I would just stick to calling by full URL.

For more advanced workflows, have a look at Durable Functions, paticularly Function Chaining.

like image 92
Mikhail Shilkov Avatar answered Sep 18 '22 16:09

Mikhail Shilkov


All the previous answers are valid but, as was also mentioned in the comments, earlier this year (Q1/Q2 2018) the concept of Durable Functions has been introduced. In short, Durable Functions:

... lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.

This effectively means that you can also now chain several Functions together. It manages state when it flows from Function A => B => C, if you would require this.

It works by installing the Durable Functions Extension to your Function App. With that, you have some new context bindings available where, for example in C#, you can do something like (pseudo code):

[FunctionName("ExpensiveDurableSequence")] public static async Task<List<string>> Run(     [OrchestrationTrigger] DurableOrchestrationTrigger context) {     var response = new List<Order>();      // Call external function 1      var token = await context.CallActivityAsync<string>("GetDbToken", "i-am-root");      // Call external function 2     response.Add(await context.CallActivityAsync<IEnumerable<Order>>("GetOrdersFromDb", token));      return response; }  [FunctionName("GetDbToken")] public static string GetDbToken([ActivityTrigger] string username) {     // do expensive remote api magic here     return token; }  [FunctionaName("GetOrdersFromDb")] public static IEnumerable<Order> GetOrdersFromDb([ActivityTrigger] string apikey) {     // do expensive db magic here     return orders; } 

Some nice aspects here are:

  • The main sequence chains up two additional functions that are run after one another
  • When an external function is executed, the main sequence is put to sleep; this means that you won't be billed twice once an external function is busy processing

This allows you to both run multiple functions in sequence of each other (e.g. function chaining), or execute multiple functions in parallel and wait for them all to finish (fan-out/fan-in).

Some additional background reference on this:

  • Azure docs: Durable Functions overview
  • Azure Friday Introduction: 14m. video, with Chris Gillum and Scott Hanselman
like image 43
Juliën Avatar answered Sep 17 '22 16:09

Juliën