Rahul Soni
Rahul Soni

Reputation: 4968

Disable certain Azure functions using any configuration

While using Azure functions, I have a situation where I want a few functions in "disabled" mode for a function app, while others to be enabled. The thing is, I don't want to do it manually using the functions screen where individual functions could be enabled/disabled easily. There is this article that says Functions 2.x supports this (Functions 1.x is not a choice for me).

https://learn.microsoft.com/en-us/azure/azure-functions/disable-function

It is just that the article is a little vague about what needs to be done. It says and I quote >

In Functions 2.x you disable a function by using an app setting. For example, to disable a function named QueueTrigger, you create an app setting named AzureWebJobs.QueueTrigger.Disabled, and set it to true. To enable the function, set the app setting to false.

I tried this, but it doesn't work as documented. I have a function app called foo and a function called bar. I have tried both:

disabled: true in function.json

as well as:

foo: { 
  bar: {
    disabled: true
  }
}

After making these changes and redeploying there is no effect on the UI. What am I missing?

Upvotes: 5

Views: 4889

Answers (2)

Pieter Heemeryck
Pieter Heemeryck

Reputation: 683

I used the [Disable("myAppSetting")], but it did not disable my function at first. Turns out the app settings from the App Config Azure resource do not have an impact on this behavior. It needs to be put directly in the app settings of the Azure Function itself for it to work. The Azure portal however does show correctly that the function is disabled.

Upvotes: 0

Turbo
Turbo

Reputation: 2561

The recommended approach is by using app settings, which you can do by going to the portal. [Note: They don't mean function.json when they say app settings.]

Option 1: Using App Settings In azure portal, navigate to your function app foo -> Confuguration, and you should see Application Settings tab with a few variables already defined. You need to create a new variable by clicking New application setting button. Set name as AzureWebJobs.bar.Disabled and value as true. Note that the function app name foo doesn't figure in the variable name.

Option 2: Using host.json Because you are looking for disabling a function from code, you can try doing this in host.json. Note that this is intended for local development, and not recommended for prod, but it works. https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json#functions

{
    "functions": [ "function1", "function2" ] // Don't list function "bar" here, and it would get disabled.
}

Note that the portal will not show this correctly, and list "bar" as enabled, but you will get 404 when hitting that function.

Option 3: Using Disable attribute If you are using C# You can also use the [Disable] attribute. This is a Functions 1.x construct, but it works in 2.x as well. Similar to above, the portal UI will not show this correctly.

    [Disable]
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
}

Option 4: By removing FunctionName attribute Only if you are using C#. This might sound counter-intuitive, but if you remove the FunctionName attribute from your function, it won't be treated as such.

    // [FunctionName("Function1")]  // Comment this or delete this line to disable this function
    public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
}

This should work in both runtimes. The function wouldn't show in the azure portal.

Upvotes: 8

Related Questions