Search code examples
amazon-web-servicesamazon-dynamodbaws-glueaws-step-functions

AWS Step Function get all Range keys with a single primary key in DynamoDB


I'm building AWS Step Function state machines. My goal is to read all Items from a DynamoDB table with a specific value for the Hash key (username) and any (wildcard) Sort keys (order_id). Basically something that would be done from SQL with:

SELECT username,order_id FROM UserOrdersTable
WHERE username = 'daffyduck'

I'm using Step Functions to get configurations for AWS Glue jobs from a DynamoDB table and then spawn a Glue job via step function's Map for each dynamodb item (with parameters read from the database).

 "Read Items from DynamoDB": {
  "Type": "Task",
  "Resource": "arn:aws:states:::dynamodb:getItem",
  "Parameters": {
    "TableName": "UserOrdersTable",
    "Key": {
      "username": {
        "S": "daffyduck"
      },
      "order_id": {
        "S": "*"
      }
    }
  },
  "ResultPath": "$",
  "Next": "Invoke Glue jobs"
}

But I can't bring the state machine to read all order_id's for the user daffyduck in the step function task above. No output is displayed using the code above, apart from http stats. Is there a wildcard for order_id ? Is there another way of getting all order_ids? The query customization seems to be rather limited inside step functions: https://docs.amazonaws.cn/amazondynamodb/latest/APIReference/API_GetItem.html#API_GetItem_RequestSyntax

Basically I'm trying to accomplish what can be done from the command line like so:

$ aws dynamodb query \
--table-name UserOrdersTable \
--key-condition-expression "Username = :username" \
--expression-attribute-values '{
    ":username": { "S": "daffyduck" }
}'

Any ideas? Thanks


Solution

  • I don't think that is possible with Step functions Dynamodb Service yet. currently supports get, put, delete & update Item, not query or scan. For GetItem we need to pass entire KEY (Partition + Range Key)

    For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

    We need to write a Lambda function to query Dynamo and return a map and invoke the lambda function from step.