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

Read data iteratively from DynamoDB inside a Step Functions workflow


I am new to coding and learning about AWS services. I am trying to fetch data iteratively from a DynamoDB table in a Step Functions workflow. For Example, say for a book ID I want to check the names of customers that purchased the book. I am primarily trying to do practice using Map state and try to integrate with DDB.

I created a DDB table with id as partition key and added cust_name1 and cust_name2 as attributes for multiple customers of a book. Now in my Step Functions, I want to use a Map state to query how many people have that book ID. Is this possible? Or is there a better way to use Map state for this scenario?

I am able to do this in a Task state, but trying to figure out how to use Map state for this scenario.

{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB Get Book Purchases",
  "States": {
    "DynamoDB Get Book Purchases": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:getItem",
      "Parameters": {
        "TableName": "get-book-data",
        "Key": {
          "id": {
            "S": "121-11-2436"
          }
        }
      },
      "ResultPath": "$.DynamoDB",
      "End": true
    }
  }
}

Solution

  • Map (in Step Functions) is designed to apply a function on each of the elements of an array or list. Count is an aggregation function and therefore, using it as part of Map is not that useful.

    For your exercise, you can try to allow users to buy multiple books, where the Map functions will check the availability of each of the books in your inventory.