Tutorial 4: Define multiple tasks to perform in parallel - 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 4: Define multiple tasks to perform in parallel

So far you’ve learned how to run workflows in a sequential manner. However, you can run two or more steps in parallel using the Parallel state. A Parallel state causes the interpreter to execute each branch concurrently.

Both the branches in a Parallel state receive the same input, but each branch processes the parts of input specific for it. Step Functions waits until each branch completes executing before proceeding to the next step.

In this tutorial, you use the Parallel state to concurrently check the identity and address of the applicant.

Step 1: Create the Lambda functions to perform the required checks

This credit card application workflow invokes two Lambda functions inside the Parallel state to check the applicant’s identity and address. These checks are performed simultaneously using the Parallel state. The state machine completes execution only after both the parallel branches have completed executing.

To create the check-identity and check-address Lambda functions
  1. In a new tab or window, open the Lambda console and create two Node.js 16.x Lambda functions titled check-identity and check-address. For information about creating a Lambda function using the console, see Create a Lambda function in the console in the Amazon Lambda Developer Guide.

  2. Open the check-identity function page and replace the existing code in the Code source area with the following code:

    const ssnRegex = /^\d{3}-?\d{2}-?\d{4}$/; const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; class ValidationError extends Error { constructor(message) { super(message); this.name = "CustomValidationError"; } } exports.handler = async (event) => { const { ssn, email } = event; console.log(`SSN: ${ssn} and email: ${email}`); const approved = ssnRegex.test(ssn) && emailRegex.test(email); if (!approved) { throw new ValidationError("Check Identity Validation Failed"); } return { statusCode: 200, body: JSON.stringify({ approved, message: `Identity validation ${approved ? 'passed' : 'failed'}` }) } };
  3. Open the check-address function page and replace the existing code in the Code source area with the following code:

    class ValidationError extends Error { constructor(message) { super(message); this.name = "CustomAddressValidationError"; } } exports.handler = async event => { const { street, city, state, zip } = event; console.log(`Address information: ${street}, ${city}, ${state} - ${zip}`); const approved = [street, city, state, zip].every(i => i?.trim().length > 0); if (!approved) { throw new ValidationError("Check Address Validation Failed"); } return { statusCode: 200, body: JSON.stringify({ approved, message: `Address validation ${ approved ? 'passed' : 'failed'}` }) } };
  4. For both the Lambda functions, from the Function overview section, copy their respective Amazon Resource Names (ARN) and save them in a text file. You’ll need the function ARNs while specifying the service integration for the Verify applicant's identity and address state. The following is an example ARN:

    arn:aws-cn:lambda:us-west-2:123456789012:function:HelloWorld

Step 2: Update the workflow – Add parallel tasks to be performed

In the Step Functions console, you’ll update your workflow to specify service integration with the check-identity and check-address Lambda functions you created in Step 1.

To add parallel tasks in the workflow
  1. Open the Step Functions console window containing the workflow prototype you created in Tutorial 1: Create the prototype for your state machine.

  2. Choose the Verify identity state, and in the Configuration tab, do the following:

    1. For Integration type, keep the default selection of Optimized.

      Note

      Using Step Functions, you can integrate with other Amazon Web Services and orchestrate them in your workflows. For more information about service integrations and their types, see Using Amazon Step Functions with other services

    2. For Function name, choose the check-identity Lambda function from the dropdown list.

    3. For Payload, choose Enter payload and then replace the example payload with the following as payload:

      { "email": "janedoe@example.com", "ssn": "012-00-0000" }
  3. Choose the Verify address state, and in the Configuration tab, do the following:

    1. For Integration type, keep the default selection of Optimized.

    2. For Function name, choose the check-address Lambda function from the dropdown list.

    3. For Payload, choose Enter payload and then replace the example payload with the following as payload:

      { "street": "123 Any St", "city": "Any Town", "state": "AT", "zip": "01000" }
  4. Choose Next.