David Gard
David Gard

Reputation: 12047

Azure Function execution ignoring 'exit' command

I have a Azure Function (PowerShell) that contains some error handling. In the event of an error I carry out a couple of simple actions and I then wish to stop exectution, however this is not working; PowerShell is ignoring the exit 1 command and continuing execution.

try{
    $metrics = Get-AzureMetrics -BearerToken $bearer_token -SubscriptionId $env:HC_SubscriptionId -ResourceGroup $env:HC_ResourceGroup -ResourceType $env:HC_ResourceType -ResourceName $env:HC_ResourceName
} catch {
    $failure = $_.Exception.Message
    Write-Output "[ERROR] Failed to GET metrics from Azure Monitor. $failure"
    Invoke-LogToSplunk -EventHost "bvt-processevnts" -Source $env:HealthCheckFunctionName -Message "Failed to GET metrics from Azure Monitor. $failure" -Severity "ERROR" -SplunkUri $env:SplunkUrl -AccessToken $env:SplunkToken | Out-Null
    exit 1
}

Note that exit 1 works when testing locally, it's only when I run it as an Azure Function that I see this issue. I've also tried Return and Break, but those commands are ignored also.

I've considered replacing exit 1 with $error_occurred = $true and then nesting proceeding commands in an if statement (see below). However, I'd like to avoid this if possible as it's harder to manage in the long run, and less clear to others in the team who may need to view/edit the Azure Function in the future.

if ( ! ( $error_occurred ) ) {
    # Other commands
}

So my question is (hopefully) simple; how can I tell the Azure Function to stop execution?

Upvotes: 0

Views: 2213

Answers (1)

Caleb Seelhoff
Caleb Seelhoff

Reputation: 261

This solution may not fit your needs, but could you try something like this?

Function Get-AzureMetricsOrFail ($bearer_token) {
    try{
        $metrics = Get-AzureMetrics -BearerToken $bearer_token -SubscriptionId $env:HC_SubscriptionId -ResourceGroup $env:HC_ResourceGroup -ResourceType $env:HC_ResourceType -ResourceName $env:HC_ResourceName
    } catch {
        $failure = $_.Exception.Message
        Write-Output "[ERROR] Failed to GET metrics from Azure Monitor. $failure"
        Invoke-LogToSplunk -EventHost "bvt-processevnts" -Source $env:HealthCheckFunctionName -Message "Failed to GET metrics from Azure Monitor. $failure" -Severity "ERROR" -SplunkUri $env:SplunkUrl -AccessToken $env:SplunkToken | Out-Null
    }
    return $metrics
}

$metrics_result = Get-AzureMetricsOrFail -bearer_token 'some_token'

if($metrics_result) {
    'Successfully obtained Azure Metrics'
    # Rest of code
} else {
    'Failed getting Azure Metrics.'
}

Alternatively, maybe try just moving the Exit statement?

try{
    $metrics = Get-AzureMetrics -BearerToken $bearer_token -SubscriptionId $env:HC_SubscriptionId -ResourceGroup $env:HC_ResourceGroup -ResourceType $env:HC_ResourceType -ResourceName $env:HC_ResourceName
} catch {
    $failure = $_.Exception.Message
    Write-Output "[ERROR] Failed to GET metrics from Azure Monitor. $failure"
    Invoke-LogToSplunk -EventHost "bvt-processevnts" -Source $env:HealthCheckFunctionName -Message "Failed to GET metrics from Azure Monitor. $failure" -Severity "ERROR" -SplunkUri $env:SplunkUrl -AccessToken $env:SplunkToken | Out-Null
}
if(!($metrics)) {
    exit
}

Upvotes: 1

Related Questions