Arjen
Arjen

Reputation: 93

How to update a row in an Azure storage table using an Azure Function out binding

I'm using an Azure Function to process data stored in an Azure Storage table. The Azure Function is written in PowerShell and is triggered by a timer to run each minute. On each run it will take the first 10 unprocessed rows, process each of those rows and then mark the row as processed by setting a field 'processed' to true. However, it seems that by using the output binding I'm not able to update a row.

I tried updating the rows by using the following configuration and code.

Function bindings:

    {
      "tableName": "tobeprocessed",
      "connection": "ProcessStorage",
      "name": "InProcessStorageTableBinding",
      "type": "table",
      "direction": "in"
    },
    {
      "tableName": "tobeprocessed",
      "connection": "ProcessStorage",
      "name": "OutProcessStorageTableBinding",
      "type": "table",
      "direction": "out"
    }

Function code:

param($Timer, $InProcessStorageTableBinding)

# Get first ten unprocessed rows
$InProcessStorageTableBinding | Where-Object { $_.Processed -eq $false } | Select-Object -First 10 | ForEach-Object {

    # Logic for processing the values in the row
    # left out for the sake of brevity

    # Update the row
    $_.Processed = $true
    Push-OutputBinding -Name OutProcessStorageTableBinding -Value $_
}

The message that appears in the logs of the Azure Function on the Azure Portal is:

The specified entity already exists. RequestId:da4224dd-0002-0071-0671-70a732000000 Time:2023-04-16T14:38:05.4671520Z

While searching the internet for possible solutions I found that people are using Get-AzStorageTable and Update-AzTableRow from the PowerShell module AzTable to update rows in Azure Storage tables. But by doing so a lot more configuration is required (certificates, tenant- and app id's, etc) as it seems that by using Update-AzTableRow we cannot use the out binding. And the bindings is such a nice feature of Azure Functions since it takes care of all the authentication and connection setup behind the scenes, so I would really prefer to use the out binding if possible.

Update: As Peter Bons pointed out in the answers below, the docs about Azure Tables output bindings are very clear that updating isn't possible using the bindings.

Would it then maybe be possible to re-use (parts) of the connection that is set up by the bindings to reduce the plumbing needed in order to be able to use (for instance) the PowerShell module AzTable to update a row?

So that we could do something like this:

$table = Get-AzStorageTable –Connection $InProcessStorageTableBinding.Connection

$cloudTable = $table.CloudTable

$rowToProcess = Get-AzTableRow `
    -table $cloudTable `
    -customFilter <filter to retrieve row>

$rowToProcess.Processed = $true

$rowToProcess | Update-AzTableRow -table $cloudTable

Upvotes: 0

Views: 1396

Answers (1)

Peter Bons
Peter Bons

Reputation: 29880

You are out of luck I am afraid. The docs confirm your suspicion:

This output binding only supports creating new entities in a table. If you need to update an existing entity from your function code, instead use an Azure Tables SDK directly.

Upvotes: 1

Related Questions