2

I have a time trigger azure function deployed on portal. It runs daily at 10:00 am. However, there is now a requirement that function should also be invoked and run on some other time dynamically as well.

I know how to set the trigger in function.json file dynamically via Kudu Api using the steps in answer mentioned here. So using those steps, I can set the trigger for the next minute and run the function.

But this isn't real-time, this seems a workaround. Isn't there any direct way to invoke and manually run azure function directly via apis?

Karan Desai
  • 3,012
  • 5
  • 32
  • 66
  • Why not add a trigger based on for instance a message queue, and use that as an extra trigger for your function? – rickvdbosch Oct 25 '17 at 09:32
  • But does hybrid work. My function is time trigger in real sense, in that can I add a queue trigger? Or did I mis understand – Karan Desai Oct 25 '17 at 09:43
  • Not sure if hybrid works. If it doesn't, extract the functional logic into a method and call it from two functions: one that's triggered in time, and one with a MessageQueue trigger. – rickvdbosch Oct 25 '17 at 09:50

2 Answers2

3

Isn't there any direct way to invoke and manually run azure function directly via apis?

We could trigger the deployed Azure function with REST API. I test it with Time Trigger C# Azure function on my side.

Post  https://{FunctionAppName}.azurewebsites.net/admin/functions/{functionName}

Note: I trace it from Azure portal, I don't find any official document mentioned this, if you want to use this API in the product environment, please pay more attention to this.

We need x-functions-key as header. And we could get the function key from the function Application.

enter image description here

enter image description here

We also could use bearer token as authorization, about how to get the authorization for this Rest API please refer to another SO thread.

enter image description here

enter image description here

Updated:

Add the body info.

enter image description here

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Thanks. Very interesting! But unfortunately I am still getting unauthorized 401 even after using proper authorization. – Karan Desai Oct 26 '17 at 11:01
  • Would you mind sharing which way to get authorization? – Tom Sun - MSFT Oct 26 '17 at 13:32
  • I tried both ways. First using x-function-key and second using bearer token. But neither worked out. Is there any other setting I am missing. Are you sending anything in Body (in postman's screenshot, it appears like that)? – Karan Desai Oct 27 '17 at 02:34
  • Thanks. Will ping here after trying this out. – Karan Desai Oct 27 '17 at 02:41
  • Woah it worked!! Thanks a lot sir. It's strange that no documentation or wiki mention this!! You are genius :) – Karan Desai Oct 27 '17 at 02:49
  • 1
    Just one more update: It is `x-functions-key` and not `x-function-key`. I could not edit the answer because it requires more than 6 edited characters to save edit – Karan Desai Oct 27 '17 at 02:55
  • 1
    This approach is now officially documented here: https://learn.microsoft.com/mt-mt/Azure/azure-functions/functions-manually-run-non-http – Slawomir Brzezinski Jan 09 '19 at 11:06
0

For the requirement above, my recommendation would be to create two functions that share the same logic (if CSX, either by importing the common implementation using #load or adding a reference to a common assembly, or by having a common type).

You'd have one function using a timer trigger and another using a different trigger type that enables you to invoke the function on demand without a dependency on any of the Kudu or Admin APIs (e.g. HTTP, queue, SB, etc.), the function entry point (your Run method) would just invoke the common logic you bring in.

Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • The making of another function means few extra bucks, little extra implementation time and extra maintenance effort. When we open any function in portal we have Run button there, so I was curious if the same can be achieved using a custom button in a custom app clicking on which triggers or runs the function. Yes, your recommendation make sense and I will be definitely think in that direction as well. – Karan Desai Oct 26 '17 at 02:55