Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function App = run single Azure Function locally to debug

In visual studio I have created an Azure Function App with several Function.

When I launch the Function App debugger from the tool bar all Functions are triggered.

Is there a way to trigger a single function from the App within Visual Studio 2017?

like image 276
Creggdev Avatar asked Oct 16 '18 13:10

Creggdev


People also ask

How do I debug Azure function app locally?

Copy the settings file to the root of the Functions project next to the DLL, open a command prompt from there, and run “func start.” The Function will run based off the same trigger it would use if hosted in Azure or it can be manually triggered for testing using a local HTTP endpoint.

How do I debug Azure function locally in Visual Studio code?

In Visual Studio Code, press F5 to launch the debugger and attach to the Azure Functions host. You could also use the Debug > Start Debugging menu command. Output from the Functions Core tools appears in the Terminal panel.

Can we debug Azure function?

On the Azure Portal, access your Azure Function App, from the left context menu select the option Functions present in the Functions section. On the Functions page, select the Function you want to debug.


3 Answers

There is no easy way to achieve this, but it is possible.

  1. Disable functions:

by modifying the function.json file:

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

or by using the [Disable] attribute:

[Disable]
[FunctionName("Function")]
[NoAutomaticTrigger]
public static void Function(string input, TraceWriter log)
{ }
  1. func run using Azure Core Tools (only v1.x)

Run function using command: func run <functionName>

  1. Specify functions in the host.json file

In your host.json file specify functions that should be run:

{ 
   "functions":[ "FunctionToRun" ]
} 
like image 154
Pawel Maga Avatar answered Oct 21 '22 08:10

Pawel Maga


As @Pawel Maga mentioned there are three ways.

I have a little better approach for 3rd option (Specify functions in the host.json file).

Instead of messing with host.json (we might forget to undo while publishing.. i have done it many times :p).

We can override functions array by setting value in local.settings.json.

For example: Set like below code in local.settings.json

{
  "Values": {
    "AzureFunctionsJobHost__functions__0":  "FunctionToRun",
    "AzureFunctionsJobHost__functions__1":  "SecondFunctionToRun",
  }
}

Instead of writing below code in host.json

{ 
   "functions":[ "FunctionToRun", "SecondFunctionToRun" ]
} 
like image 40
Yash Mochi Avatar answered Oct 21 '22 08:10

Yash Mochi


As an update to the above answers: it appears that you can disable functions in the localsettings.json in this way.

{
  "Values": {
    "AzureWebJobs.MyFirstFunction.Disabled": true,
    "AzureWebJobs.MySecondFunction.Disabled": true
  }
}

This is what I'm planning to do for our team as it has the nicest syntax of the "safe" options (approaches that don't have to be undone on publish).

See here: https://learn.microsoft.com/en-us/azure/azure-functions/disable-function?tabs=portal#localsettingsjson

like image 6
Ian Avatar answered Oct 21 '22 09:10

Ian