Azure Functions: How do I launch only a specific Function in local dev environment?
Asked Answered
M

3

5

If I want to launch all available functions in the dev environment, I simply do:

 func host start

Is there a way to choose a subset of the available functions, without having to move the intended-to-be-deactivated ones out of the working directory etc.?

PS I am using Python for the Function itself.

Minority answered 20/5, 2019 at 8:29 Comment(0)
C
8

There are three ways to implement it.

  1. Disable functions:

One is modifying the function.json:

"bindings": [
    ...
],
"disabled": "IS_DISABLED"

Another is use Disable attribute to prevent a function from being triggered.

    [Disable]
 [FunctionName("Function")]
 [NoAutomaticTrigger]
 public static void Function(string input, TraceWriter log)
{
}
  1. With Azure Functions Core Tools, only for version 1.x

    func run <functionName>

  2. host.json:

    {
     "functions": [ "QueueProcessor", "GitHubWebHook" ]
    }
    

Update:

4: as jtlz2 answered, this way is for disable functions locally with local.settings.json.

{
  "Values": {
     "AzureWebJobs.MyFunctionName.Disabled": true
     "AzureWebJobs.MyFunctionName2.Disabled": false
   }
}

**Update:**as @ahmelsayed explains something about there are many options to call only one function, so i update it here.

"Disabled" is meant to be used to dynamically turn a function on or off. The runtime will still load the function, and will display any errors or issues with the function (incorrect settings etc), but will not execute the code. There are many ways to enable/disable a function because some want to keep that in source control and for some it's a devops operation

The functions array in host.json is something I wasn't initially aware of. It was added to the runtime for the convenience of the runtime developers who have a large folder of samples that they wanted to be able to load only a subset of. This completely ignores functions that are not listed. They won't be indexed or loaded in anyway.

Chimpanzee answered 20/5, 2019 at 9:20 Comment(9)
Oops, I posted a (different) answer but to the wrong question(!). Mine adds a fourth way - if you might add it to your answer then yours would be hard to beat? Also, do you know why there are quite so many options..?Minority
@jtlz2, some ways are for the Function on the azure and some for only testing. And the host.json the doc mentions it's only used for running locally.Chimpanzee
Fair enough - and my post was specific about dev. Thanks!Minority
@jtlz2, if this could help you, you could mark it as the answer. Thanks!Chimpanzee
If you are able to add my answer into yours in fact, then I can and will be in a position to accept! :)Minority
Fantastic - thank you so so much, and for your speed reply. Answer accepted!Minority
George please see comment below my answer. Thanks!Minority
@jtlz2, please check the host.json way link I paste, I believe they are the same way.Chimpanzee
George there is some extra info from @ahmelsayed - perhaps you can add it to your answer as well? :)Minority
F
9

I use func start --functions [a space separated list of functions]

This is with azure-functions-core-tools@3

According to:

--port [-p]        Local port to listen on. Default: 7071
--cors             A comma separated list of CORS origins with no spaces. Example: https://functions.azure.com,https://functions-staging.azure.com
--cors-credentials Allow cross-origin authenticated requests (i.e. cookies and the Authentication header)
--timeout [-t]     Timeout for on the functions host to start in seconds. Default: 20 seconds.
--useHttps         Bind to https://localhost:{port} rather than http://localhost:{port}. By default it creates and trusts a certificate.
--cert             for use with --useHttps. The path to a pfx file that contains a private key
--password         to use with --cert. Either the password, or a file that contains the password for the pfx file
--language-worker  Arguments to configure the language worker.
--no-build         Do no build current project before running. For dotnet projects only. Default is set to false.
--enableAuth       Enable full authentication handling pipeline.
--functions        A space seperated list of functions to load.
Friction answered 21/5, 2020 at 4:1 Comment(1)
This is great for local testing, you can switch back and fourth without having to modify a bunch of json.Poisoning
C
8

There are three ways to implement it.

  1. Disable functions:

One is modifying the function.json:

"bindings": [
    ...
],
"disabled": "IS_DISABLED"

Another is use Disable attribute to prevent a function from being triggered.

    [Disable]
 [FunctionName("Function")]
 [NoAutomaticTrigger]
 public static void Function(string input, TraceWriter log)
{
}
  1. With Azure Functions Core Tools, only for version 1.x

    func run <functionName>

  2. host.json:

    {
     "functions": [ "QueueProcessor", "GitHubWebHook" ]
    }
    

Update:

4: as jtlz2 answered, this way is for disable functions locally with local.settings.json.

{
  "Values": {
     "AzureWebJobs.MyFunctionName.Disabled": true
     "AzureWebJobs.MyFunctionName2.Disabled": false
   }
}

**Update:**as @ahmelsayed explains something about there are many options to call only one function, so i update it here.

"Disabled" is meant to be used to dynamically turn a function on or off. The runtime will still load the function, and will display any errors or issues with the function (incorrect settings etc), but will not execute the code. There are many ways to enable/disable a function because some want to keep that in source control and for some it's a devops operation

The functions array in host.json is something I wasn't initially aware of. It was added to the runtime for the convenience of the runtime developers who have a large folder of samples that they wanted to be able to load only a subset of. This completely ignores functions that are not listed. They won't be indexed or loaded in anyway.

Chimpanzee answered 20/5, 2019 at 9:20 Comment(9)
Oops, I posted a (different) answer but to the wrong question(!). Mine adds a fourth way - if you might add it to your answer then yours would be hard to beat? Also, do you know why there are quite so many options..?Minority
@jtlz2, some ways are for the Function on the azure and some for only testing. And the host.json the doc mentions it's only used for running locally.Chimpanzee
Fair enough - and my post was specific about dev. Thanks!Minority
@jtlz2, if this could help you, you could mark it as the answer. Thanks!Chimpanzee
If you are able to add my answer into yours in fact, then I can and will be in a position to accept! :)Minority
Fantastic - thank you so so much, and for your speed reply. Answer accepted!Minority
George please see comment below my answer. Thanks!Minority
@jtlz2, please check the host.json way link I paste, I believe they are the same way.Chimpanzee
George there is some extra info from @ahmelsayed - perhaps you can add it to your answer as well? :)Minority
M
2

It seems there has been some consternation over disabling Functions lately.

As pointed out at https://github.com/Azure/Azure-Functions/issues/736#issuecomment-471072316, one can make use of local.settings.json to achieve this. Simply add to it:

{
  "Values": {
    "AzureWebJobs.MyFunctionName.Disabled": true
    "AzureWebJobs.MyFunctionName2.Disabled": false
  }
}

etc.

I'd be interested to hear if there is a better way, e.g. setting it from the command line when executing func host start.

Minority answered 20/5, 2019 at 9:23 Comment(7)
Another way you can do that by adding the functions you want to run in your host.json like { "functions": [ "MyFunctionName" ] }. That will ONLY load MyFunctionName I was thinking of adding a --functions <list of functions> to func start but I wasn't sure if there was a need for it. Please feel free to open an ask on the core-tools repo github.com/Azure/azure-functions-core-tools/issues and I can take care of itEaglewood
@Eaglewood Amazing, will try to! I wonder if the author of the other answer George Chen can incorporate this into his answer? Question: Why are there quite so many options / any plan to rationalise it? :\Minority
Apologies - George already has that in his answer of course. Your proposal would be amazing - will open a ticket now. Thanks!Minority
Yep, George answer is pretty complete with everything possible now :)Eaglewood
about the existence of many options, they are all meant for slightly different scenarios. It's pretty confusing though because the scenarios are very similar with one or two slight differences. "Disabled" is meant to be used to dynamically turn a function on or off. The runtime will still load the function, and will display any errors or issues with the function (incorrect settings etc), but will not execute the code. There are many ways to enable/disable a function because some want to keep that in source control and for some it's a devops operationEaglewood
The functions array in host.json is something I wasn't initially aware of. It was added to the runtime for the convenience of the runtime developers who have a large folder of samples that they wanted to be able to load only a subset of. This completely ignores functions that are not listed. They won't be indexed or loaded in anyway. I initially asked for it to be removed because it was confusing, but then it enabled some scenarios like this not to mention that removing it would be an unnecessary breaking change for someone.Eaglewood
@Eaglewood Huge thanks for the clarity - perhaps George can add it to his answer?? :)Minority

© 2022 - 2024 — McMap. All rights reserved.