0

I have a .net Azure function project which includes multiple functions such as EventGridTrigger function, BlobTrigger function etc. Few trigger functions in the project needs to be enabled only for specific regions. Is it possible to enable/disable trigger functions based on regions through app settings ?

[FunctionName("CosmosDBFunction")]   //enable only in US-west
public static void Run([CosmosDBTrigger()])
{}

[FunctionName("EventGridFunction")]   //enable only in US-east
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent)
{}

Edit:

I'm trying to disable in "isolated" Azure functions via appsettings.json within the project. The "[Disable]" and AzureWebJobs.$FunctionName.Disabled properties are not working from appsettings.json, though it is working if mentioned in azure portal.

DxG
  • 147
  • 4
  • 17

2 Answers2

2

You can use the Disable attribute, which dynamically disables specific Functions based on app settings.

For example:

[Disable("FUNCTION_DISABLED")]
[FunctionName(nameof(EventGridFunction))]
public static void Run(...

Each regional deployment will then set the FUNCTION_DISABLED setting to true if the Function should be disabled.

  • Thanks for the suggestion. This works when I create in-process azure function. I'm surprised to see it is not working in isolated process azure functions. Have you tried with isolated functions ? – DxG Oct 15 '22 at 21:39
1

One way to disable/Enable Azure functions based on regions by using PowerShell command. I could able to achieve this by using Update-AzFunctionAppSetting command wherein all the functions of the mentioned location will be Disabled - MSFT Docs.

Subscription Level

$GetFunction=Get-AzFunctionApp -SubscriptionId <SUBSCRIPTION_ID>
$ResourceGroup=($GetFunction).ResourceGroupName
$Name = ($GetFunction).Name
For($I=0;$I -lt $ResourceGroup.count;$I++) {
    $FunctionDetails=Get-AzResource -ApiVersion "2022-03-01" -Name $Name[$I] -ResourceGroupName $ResourceGroup[$I] -ResourceType "Microsoft.Web/sites/functions"
    if($GetFunction[$I].Location -eq "<YOUR_REQUIRED_LOCATION>"){
        For($J=0;$J -lt $FunctionDetails.count;$J++){
            $Function=$FunctionDetails[$J].ResourceName
            $FunctionName=$Function.Substring(($Function.IndexOf("/")+1),($Function.length-($Function.IndexOf("/")+1)))
            Update-AzFunctionAppSetting -Name $Name[$I] -ResourceGroupName $ResourceGroup[$I] -AppSetting @{"AzureWebJobs.$FunctionName.Disabled" = "true"}
        }
    }
}

FunctionApp Level

$FunctionDetails=Get-AzResource -ApiVersion "2022-03-01" -Name <FUNCTIONAPP_NAME> -ResourceGroupName <RESOURCE_GROUP> -ResourceType "Microsoft.Web/sites/functions"

$Function=$FunctionDetails.ResourceName | Where-Object {$FunctionDetails.Location -eq "<YOUR_REQUIRED_LOCATION>"}

For($I=0;$I -lt $Function.count;$I++) {
$FunctionExtract=$Function[$I]
$FunctionName=$FunctionExtract.Substring(($FunctionExtract.IndexOf("/")+1),($FunctionExtract.length-($FunctionExtract.IndexOf("/")+1)))
Update-AzFunctionAppSetting -Name <FUNCTIONAPP_NAME> -ResourceGroupName <RESOURCE_GROUP> -AppSetting @{"AzureWebJobs.$FunctionName[$I].Disabled" = "true"}
}

SAMPLE RESULTS:

enter image description here

enter image description here

Note: Replace -AppSetting @{"AzureWebJobs.$FunctionName.Disabled" = "false"} to true to enable it again.

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • Thanks for your comment. I want to do it from the c# code via appsettings. "AzureWebJobs.$FunctionName.Disabled" is working for in-process azure functions, but not for isolated functions – DxG Oct 15 '22 at 22:21
  • I meant the appsettings.json within the code. Not the one in portal. – DxG Oct 16 '22 at 18:36