Search code examples
aws-step-functions

How to loop over an array in an AWS Step Function?


I'm trying to process a DynamoDB query result, such as the $.DDBResult in a Map;

{
.
.
.,
"LookBackSeconds:"60",                     <-- variable based on request.
"OneMinuteAgo":"2023-08-14T15:59:00.000Z", <-- calculated via Lambda function using $.LookBackSeconds
"DDBResult": {
  "Count": 3,
  "Items": [
    {
      "Status": {
        "S": "ONLINE"
      },
      "Location": {
        "S": "North"
      },
      "LastValidationDateTime": {
        "S": "2023-08-14T14:55:55.144Z"
      }
    },
    {
      "Status": {
        "S": "ONLINE"
      },
      "Location": {
        "S": "South"
      },
      "LastValidationDateTime": {
        "S": "2023-08-14T14:55:54.890Z"
      }
    },
    {
      "Status": {
        "S": "ONLINE"
      },
      "Location": {
        "S": "East"
      },
      "LastValidationDateTime": {
        "S": "2023-08-14T14:55:52.968Z"
      }
    }
  ],
  "ScannedCount": 3
}
}

I know that I can indicate that $.DDBResult.Items is the ItemsPath, but my problem is that in the map I need an addition data point, namely the current time minus some (calculated via a Lambda function using $.LookBackSeconds) to do some processing. My thought was to loop over the $.DDBResult.Items, create a new array based on it's data and add to it $.OneMinuteAgo, but I can't find a way to loop over array to build another.

Does anyone have a thought on a solution?


Solution

  • The optional ItemSelector field lets you override the Map iterations' default (value only) input behaviour. Use it to add fields to the iteration input.

    In your case, ItemSelector could reference array the element value ($$.Map.Item.Value) and the current time from the context object (note the double dollar sign prefix: $$). The seconds offset can be referenced from the input object ($.LookBackSeconds).

    "ItemSelector": {
        "ContextValue.$": "$$.Map.Item.Value",
        "Time.$": "$$.State.EnteredTime",
        "Seconds.$": "$.LookBackSeconds"
    }
    

    N.B. The Map state's ItemSelector field replaces the deprecated Parameters field, which did the same thing.