amazon-web-servicespowershellamazon-dynamodbamazon-dynamodb-index

Syntax error when executing Invoke-DDBQuery to fetch dynamodb record using Powershell


I have one dynamodb table and the record looks like below:

{
  "account_id:list": {
    "S": "TEST| OS test"
  },
  "association_id": {
    "S": "test-xxxxx-xxxxx-xxxxx"
  },
  "id": {
    "S": "id-test12345678"
  },
  "version": {
    "N": "1"
  }
}

The "account_id:list" is the Partition key and "association_id" is the Sort key. I have only the value for "association_id" and know what is the starting value of "account_id:list" which is TEST. This information can be queried using only from the Index 'association_id' that is selected from the drop down list of "Select a table or index". Based on this information I am trying to fetch the record using the "association_id" that is available.

When trying to use the "account_id:list" part of KeyConditionExpression like begins_with("account_id:list", :accountid) it gives the below error:

Invoke-DDBQuery : Invalid KeyConditionExpression: Syntax error; token: ":list", near: "account_id:list,"

Command that I used as below:

$Query = @{
    TableName = "tableName"
    KeyConditionExpression = "begins_with(account_id:list, :accountid) AND association_id = :aid"
    ExpressionAttributeValues = @{
    ':accountid' = 'TEST'
    ':aid' = "test-xxxxx-xxxxx-xxxxx"
    } | ConvertTo-DDBItem
}

$Data = @()
$Data = Invoke-DDBQuery @Query -IndexName "association_id" -Region "region" | ConvertFrom-DDBItem

Kindly help how I can get this fixed?


Solution

  • You cannot do a begins_with on a partition key value, it must always be full equality condition otherwise you must do a full table Scan.

    If you know the entire association_id at read time, then it would be better served as your partition key, or alternatively you create a Global Second ary Index where you select it as the partition key.

    $Query = @{
        TableName = "tableName"
        IndexName = "assoc-index"
        KeyConditionExpression = "#aid = :aid AND begins_with(#accountid_list, :accountid)"
        ExpressionAttributeNames = @{
            '#aid' = 'association_id'
            '#accountid_list' = 'account_id:list'
        }
        ExpressionAttributeValues = @{
            ':accountid' = 'TEST'
            ':aid' = "test-xxxxx-xxxxx-xxxxx"
        } | ConvertTo-DDBItem
    }