Search code examples
aws-lambdaserverlessaws-step-functions

How to create a step function that iterates over a list of strings


I'm trying to create my first AWS step function. I needs to iterated over a list of string and pass each value to a lambda function. I've gotten started but I'm not understanding how to reference the current element in the list to pass was parameter to the lambda function.

{
  "StartAt": "Handle Loaders",
  "States": {
    "Handle Loaders": {
      "Type": "Map",
      "ItemsPath": "$.InputData"
      "Iterator": {
        "StartAt": "Execute Loader"
        "Execute Loader": {
          "Type": "Task"
          "Resource": !Ref DataLoader,
          "Parameters": {
            "SeedData": <same for every iteration> 
            "Loader": <the current string iterated over>
          }
          "End": true
        }
      }
      "End": true
    }
  }
}

I imagine the input would looks something like this

{
  "InputData: {
    "SeedData": <somedata_values>,
    "Loaders": ["Makes", "Models", "Styles"],
  }
}

Solution

  • I think this is the definition that you are looking for:

    {
      "StartAt": "Handle Loaders",
      "States": {
        "Handle Loaders": {
          "Type": "Map",
          "ItemsPath": "$.InputData.Loaders",
          "Parameters": {
            "loader.$": "$$.Map.Item.Value"
          },
          "Iterator": {
            "StartAt": "Execute Loader",
            "States": {
              "Execute Loader": {
                "Type": "Task",
                "Resource": "<arn of your function>",
                "Parameters": {
                  "SeedData.$": "$$.Execution.Input.InputData.SeedData",
                  "Loader.$": "$.loader"
                },
                "End": true
              }
            }
          },
          "End": true
        }
      }
    }
    

    Here are some useful link that would help you to understand it:

    https://docs.aws.amazon.com/step-functions/latest/dg/input-output-contextobject.html

    https://docs.aws.amazon.com/step-functions/latest/dg/concepts-input-output-filtering.html

    https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-map-state.html

    Also, given that you are starting with step functions I'd recommend you 2 useful tools in the console: