Home > Net >  Disable/Enable Azure functions based on regions
Disable/Enable Azure functions based on regions

Time:10-12

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)
{}

    

CodePudding user response:

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.

CodePudding user response:

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.

  • Related