1

I'm not sure if this is expected behavior and if I can fully rely on this however. I have one Azure Function, containing multiple Javascript functions.

And it appears that I'm able to call these Javascript functions from other Azure functions. Is this expected, and can I depend on this?

I'll illustrate this with an example below

// Azure Function 1 :
testFunctionA = () => {
    console.log("This is the 1st function in another azure function");
};

testFunctionB = () => {
    console.log("This is the 2nd function in another azure function");
};

testFunctionC = () => {
    console.log("This is the 3d function in another azure function");
};


module.exports = {
    testFunctionA
};

// Azure function 2:
module.exports = async function (context, req) {
    context.log("JavaScript HTTP trigger function processed a request.");

    testFunctionA();
    testFunctionB();
    testFunctionC();
};

When invoking the HTTP Trigger Azure Function 2 the output is as following Eventhough both functions are seperate Azure functions:

This is the 1st function in another azure function
index.js:6
This is the 2nd function in another azure function
index.js:10
This is the 3d function in another azure function
  • Can you talk about your specific needs? I don't think you should do this. If there is a problem with function1, function2 will be unable to execute. Maybe all you want is a function like durable function? This is the function chain: https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview?tabs=csharp#chaining – Cindy Pau Jun 04 '20 at 01:32
  • In Azure Functions, the unit of deployment and scale is a "Function App" https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference#function-app – Marie Hoeger Jun 04 '20 at 01:58
  • Here's another example of the folder structure of a Function App that contains multiple Functions (where a Function is the thing that gets triggered) https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node#folder-structure . Is this what you're asking about? – Marie Hoeger Jun 04 '20 at 01:59
  • If so, please see my answer here: https://stackoverflow.com/questions/51735928/manually-trigger-azure-function-time-triggered/51736351#51736351 – Marie Hoeger Jun 04 '20 at 02:00
  • @BowmanZhu Well so, the way I discovered that this was happening, is that I had two different Azure Functions, whereby in each I had a javascript function called "StripeVerification()" and during debugging what ended up happening is that they were calling the other function outside of the current one .. – Adeola Morren Jun 05 '20 at 00:06
  • Hi @MarieHoeger , so yes basically that was kind of what I was asking about. I basically have a few Javascript functions that get run in different Azure functions, Like SendEmail, AddToSgList, VerifyStripeHook etc etc.. So I was thinking that I could abstract those functions by passing in the required info in other azure functions. But I guess I could create a seperate SharedCode folder, with multiple Js files, and import those right? – Adeola Morren Jun 05 '20 at 00:12
  • If I'm understanding you correctly - yes :) I'll post an answer with generic rules around defining functions since hopefully that will be useful to other folks too. – Marie Hoeger Jun 05 '20 at 02:27

1 Answers1

0

Here are the rules around defining JavaScript functions in a Function App:

A function.json file is used to define a trigger, and each function.json file can only define one trigger. There's also a constraint today that a function.json file must be one, and only one, directory past the root. The root directory is the directory with host.json (and likely local.settings.json).

By default, the JavaScript function exported from an index.js file next to a function.json is the code that is used to handle an event from the trigger. However, you can also define where this function comes from using the scriptFile and entryPoint properties.

All entry points to JavaScript functions are loaded when the Function App starts up. This is done by requireing each of the JavaScript functions that will eventually handle trigger events.

Code can, and is expected, to be shared between Functions in this way: Manually trigger Azure Function - Time triggered

Marie Hoeger
  • 1,261
  • 9
  • 11