Disable Azure function programmatically uisng javascript

Shweta 81 Reputation points
2021-08-13T05:47:07.383+00:00

have a scenario while running the app I have to disable/enable a particular function. I'm using JavaScript SDK. Please suggest.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

Accepted answer
  1. singhh-msft 2,431 Reputation points
    2021-08-13T07:59:19.793+00:00

    @Shweta , thank you for reaching out to us. Let's consider we have an Azure Function App with 2 functions: HttpTrigger1 and BlobTrigger1 and that we want to disable the HttpTrigger1 function. This can be achieved by going to the Configuration tab of our Function App and add a new boolean app setting named using this pattern:

    AzureWebJobs.HttpTrigger1.Disabled  
    

    Now, set it's value to true, and you will see that it is disabled. Consequently, to enable the Function back, you can make AzureWebJobs.HttpTrigger1.Disabled value false. Further, if you want to disable this programmatically, you can use PowerShell or Azure CLI. Below is the script to set App Settings in your App Service:

    $site = Get-AzWebApp -Name foo-app  
    $oldSettings = ($site.SiteConfig.AppSettings | % { $h = @{} } { $h[$_.Name] = $_.Value } { $h })  
      
    $newSettings = @{ AzureWebJobs.HttpTrigger1.Disabled  = true }  
      
    Set-AzWebApp -ResourceGroupName foo-rg -Name foo-app -AppSettings ($oldSettings + $newSettings)  
    

    The best way AFAIK to achieve this is to use PowerShell Function App for running above scripts (since it has in-built support for PowerShell) and calling PowerShell Function App endpoint from your JavaScript code. I would also recommend you to watch Using PowerShell Modules in Azure Functions for more insights about Azure PowerShell functions. Let me know if you have further questions.

    -----------------------------------------------------------------------------------------------------------

    Please "Accept the answer" and upvote if the information helped you. This will help us and others in the community as well.


2 additional answers

Sort by: Most helpful
  1. Praveen Ramachandran 60 Reputation points
    2023-02-16T15:56:14.79+00:00

    I am looking for some similar help. I have to stop the function when exception happens. I am using Java spring to build functions. Any inputs will be apprciated.

    Thanks,

    Praveen

    0 comments No comments

  2. Praveen Ramachandran 60 Reputation points
    2023-02-16T15:56:28.6366667+00:00

    I am looking for some similar help. I have to stop the function when exception happens. I am using Java spring to build functions. Any inputs will be appreciated.

    Thanks,

    Praveen

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.