Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

In this article, you will learn how to disable a single function in Azure Functions 1.x deployed from Visual Studio.

Serverless is awesome and serverless is here to stay.

Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

OK, without getting into a rant about why we should be leveraging serverless architecture, I will address the issue at hand.

Azure Functions 1.x

What is the tough thing about disabling a function app, you ask?

Simply navigate to the function -> Manage and flip the Function State to ‘Disabled’. Easy-peasy!

Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

However, this only works for functions that are authored in the Azure Portal. Now let us face the truth, one does not simply code out using the inbuilt editor.

It takes us all back to the cave-years of coding without intellisense. (Ok, the inbuilt portal does boast of some intellisense, but whatever, I am comfortable with Visual Studio and not getting out of that shell anytime soon!).

For Azure function apps 1.x that are published/deployed from Visual Studio/Visual studio Code, we soon realize that we cannot just pick and disable functions as we require. OUCH!

Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

We mere mortals soon realized this is a drawback and raised a hue and cry regarding the same. See GitHub issues raised for the same below:

  • https://github.com/Azure/azure-functions-host/issues/1331
  • https://github.com/Azure/azure-functions-host/issues/1846

 

But thankfully, big brother soon got onto the case and showed us a flexible way to achieve this without having to recompile and republish the code.

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

 

The Solution

  1. Create a new environment variable in settings.json

    I have named it {MyFunctionName}_DISABLED.

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio
  1. Insert the Disableattribute as shown below.

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio
  1. Publish the function app. Do ensure that the local value is published, as shown below.

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

    It’s all downhill from here. To disable the function,
  1. Navigate to the function app -> Overview -> Function app settings

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

  2. Navigate to Manage application settings as shown,

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio
  1. Edit the application setting created in Step 1. The function is disabled when the application setting is set to true or 1

    Disabling A Single Function In Azure Functions 1.x Deployed From Visual Studio

Using the above method, we can enable and disable the function by simply updating the application setting, without having to recompile or redeploy the app.

As the documentation states,

Changing an app setting causes the function app to restart, so the disabled state change is recognized immediately.

Azure Functions 2.x

For Azure Functions 2.x, you can use the Function State switch on the function's Manage tab, as version 2 introduced a mechanism based on app settings that allow you to disable functions without requiring usage of the disabled attribute discussed above.