3

I'm writing a cloud function and trying to add support for several providers. Currently for Google Cloud and AWS Lambda this has been simple enough, as both allow me to specify a named export of a given file (index.js in root folder) as the entrypoint for function execution.

All was good I thought, but now I want to add Azure support, but it seems to insist on having a folder with the function name with its own index.js which is the entrypoint for execution. Unfortunately this breaks the architecture I have in place (have made it generic to allow one entrypoint for multiple providers with some runtime detection of execution environment to return correct function type for that provider).

Is it possible at all with Azure to do something similar to GCF or Lambda and simply say 'I want a HTTPS triggered function that starts at this export of this file', and it trusts you to do the rest?

Azure documentation has not been much help, neither have I been able to find much of use on Google.

transiti0nary
  • 493
  • 6
  • 25

2 Answers2

2

You do need a folder for each function to map the entry point to the correct script file. But this folder only needs the function.json file to configure this. Your code can be in a different location, for example all functions bundled inside one file - that's what Azure Functions Pack is doing.

Inside the function.json you can set the script file like this:

{
 "disabled": false,
 "bindings": [
  {
   "authLevel": "anonymous",
   "type": "httpTrigger",
   "direction": "in",
   "name": "req",
   "methods": [
    "get"
   ]
  },
  {
   "type": "http",
   "direction": "out",
   "name": "res"
  }
 ],
 "scriptFile": "../.funcpack/index.js",
 "entryPoint": "HttpTrigger1"
}
Thomas
  • 4,030
  • 4
  • 40
  • 79
  • Thanks! This is promising then. So here, is `"entryPoint": "HttpTrigger1"`, the name of a named export in `index.js`? – transiti0nary Oct 18 '18 at 10:51
  • I haven't tried this myself yet & the Azure Functions Pack tool is doing some webpack magic here, but I think that's how it should work. ```module.exports = { "HttpTrigger": __webpack_require__(1), "HttpTrigger2": __webpack_require__(2) } ``` – Thomas Oct 18 '18 at 11:58
1

Just came across a similar issue myself. However I am using TS so this might not be exactly the same, but hey here's my 5c.

Not sure about your case specifically but I had to use the "entryPoint" key on the function.json to indicate the function.

After building your dist folder check what's the name given (most likely its exports.default = httpTrigger httpTrigger is the name for your azure function

in which case, add the export

{
  "entryPoint": "default"
}

to your function.json

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hugo Santos
  • 103
  • 2
  • 7
  • `entryPoint` seems to use whatever is under `exports`. If it's a _default_ export - you should put `"entryPoint": "default"`, if _non-default_, then name of the func will work: `"entryPoint": "whatevernamechosen"` (reference to Thomas's comment: https://stackoverflow.com/questions/52870584/azure-functions-node-js-custom-entrypoint#comment92660370_52872042). – Jon Jul 12 '22 at 09:39