3
votes

Is it possible to create a loop in aws step function and loop through json input array?

I have a function generateEmails that creates array with n number of objects:

{
  "emails": [
    {
      "to": [
        "[email protected]"
      ]
    },
    {
      "to": [
        "[email protected]"
      ]
    },    {
      "to": [
        "[email protected]"
      ]
    }
  ]
}

and now I want to call next function sendEmail for each object in emails array with something like this:

{
  "email": {
    "to": [
      "[email protected]"
    ]
  }
}

enter image description here

step function code:

{
  "Comment": "A state machine that prepares and sends confirmation email ",
  "StartAt": "generateEmails",
  "States": {
    "generateEmails": {
      "Type": "Task",
      "Resource": "arn:aws:lambda::prepare-confirmation-email",
      "Next": "sendEmail"
    },
    "sendEmail": {
      "Type": "Task",
      "Resource": "arn:aws:lambda::function:template-service",
      "End" : true
    }
  }
}

Is that possible to achieve?

Thanks!

1
found this - justinmchase.com/2017/03/08/iterating-with-aws-step-functions , might be the only workaround - Ward

1 Answers

1
votes

Yesterday the official AWS documentation has been updated with pretty much the same example, as Ward already advised. You need to modify this example and remove the processed data item from original array (output of you generateEmails lambda function) at the end of each iteration.

You may find simple example in my blog post: AWS Step Functions. How to process arrays

At the same time everybody, who interested in parallel array processing may upvote the map-reduce feature at AWS Forums.