Tutorial 5: Concurrently iterate over a collection of items - Amazon Step Functions
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Tutorial 5: Concurrently iterate over a collection of items

In the previous tutorial, you learned how to run separate branches of steps in parallel using the Parallel state. Using the Map state, you can run a set of workflow steps for each item in a dataset. The Map state's iterations run in parallel, which makes it possible to process a dataset quickly.

By including the Map state in your workflows you can perform tasks, such as data processing, using one of the two Map state processing modes: Inline mode and Distributed mode. To configure a Map state, you define an ItemProcessor, which contains JSON objects that specify the Map state processing mode and its definition. In this tutorial, you run the Map state in the default Inline mode, which supports up to 40 concurrent iterations. When you run the Map state in Distributed mode, it supports up to 10,000 parallel child workflow executions.

When your workflow execution enters the Map state, it will iterate over a JSON array specified in the state input. For each array item, its corresponding iteration runs in the context of the workflow that contains the Map state. When all iterations are complete, the Map state will return an array containing the output for each item processed by the ItemProcessor.

In this tutorial, you learn how to use the Map state in Inline mode to fetch the credit score of an applicant by iterating over a set of credit bureaus. To do this, you first fetch the names of all the credit bureaus stored in a Amazon DynamoDB table, and then use the Map state to loop through the credit bureau list to fetch the applicant’s credit score reported by each of these bureaus.

Step 1: Create a DynamoDB table to store the name of all credit bureaus

In this step, you create a table named GetCreditBureau using the DynamoDB console. The table uses the string attribute Name as the Partition key. In this table, you store the name of all the credit bureaus from which you want to fetch the applicant’s credit score.

  1. Sign in to the Amazon Web Services Management Console and open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.

  2. In the navigation pane on the console, choose Tables, and then choose Create table.

  3. Enter the table details as follows:

    1. For the Table name, enter GetCreditBureau.

    2. For the Partition key, enter Name.

    3. Keep the default selections, and choose Create table.

  4. After your table is created, in the Tables list, choose the GetCreditBureau table.

  5. Choose Actions, and then choose Create item.

  6. For Value, enter the name of a credit bureau. For example, CredTrack.

  7. Choose Create item.

  8. Repeat this process and create items for names of other credit bureaus. For example, KapFinn and CapTrust.

Step 2: Update the state machine – Fetch results from the DynamoDB table

In the Step Functions console, you’ll add a Task state and use the Amazon SDK integration to fetch the names of credit bureaus from the DynamoDB table you created in Step 1. You’ll use the output of this step as the input for the Map state you’ll add later in your workflow in this tutorial.

  1. Open the CreditCardWorkflow state machine to update it.

  2. Choose the Get list of credit bureaus state.

  3. For API Parameters, specify the Table name value as GetCreditBureau.

Step 3: Create a Lambda function that returns the credit scores for all credit bureaus

In this step, you create a Lambda function that receives the names of all credit bureaus as input, and returns the credit score of the applicant for each of these credit bureaus. This Lambda function will be invoked from the Map state you’ll add in your workflow in Step 4 of this tutorial.

  1. Create a Node.js 16.x Lambda function and name it get-credit-score.

  2. On the page titled get-credit-score, paste the following code into the Code source area.

    function getScore(arr) { let temp; let i = Math.floor((Math.random() * arr.length)); temp = arr[i]; console.log(i); console.log(temp); return temp; } const arrScores = [700, 820, 640, 460, 726, 850, 694, 721, 556]; exports.handler = (event, context, callback) => { let creditScore = getScore(arrScores); callback(null, "Credit score pulled is: " + creditScore + "."); };
  3. Deploy the Lambda function.

Step 4: Update the state machine – add a Map state to iteratively fetch credit scores

In the Step Functions console, you add a Map state that invokes the get-credit-score Lambda function to check the applicant’s credit score for all the credit bureaus returned by the Get list of credit bureaus state.

  1. Open the CreditCardWorkflow state machine to update it.

  2. Choose the Get scores from all credit bureaus state.

  3. In the Configuration tab, choose Provide a path to items array and then enter $.Items.

  4. Choose Get all scores step inside the Map state.

  5. In the Configuration tab, make sure for Integration type, Optimized is selected.

  6. For Function name, start typing the name of the get-credit-score Lambda function and choose it from the dropdown list that appears.

  7. For Payload, choose No payload.