Define parallel tasks in a Step Functions workflow
In the previous topic, Add conditional logic, you set up conditions to chose different paths in your workflow. So far, the steps have run sequentially. In this topic, you'll learn how you can run two or more steps concurrently using the Parallel
state.
Both 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 before proceeding to the next step.
You will 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
-
In a new tab or window, open the Lambda console
and create two Node.js Lambda functions titled check-identity
andcheck-address
. For information about creating a Lambda function using the console, see Create a Lambda function in the console in the AWS Lambda Developer Guide. -
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'}` }) } };
-
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'}` }) } };
-
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:lambda:us-east-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
-
Open the Step Functions console
window containing the workflow prototype you created in Create a state machine in Step Functions. -
Choose the Verify identity state, and in the Configuration tab, do the following:
-
For Integration type, keep the default selection of Optimized.
Note
Using Step Functions, you can integrate with other AWS services and orchestrate them in your workflows. For more information about service integrations and their types, see Integrating services with Step Functions
-
For Function name, choose the check-identity Lambda function from the dropdown list.
-
For Payload, choose Enter payload and then replace the example payload with the following as payload:
{ "email": "janedoe@example.com", "ssn": "012-00-0000" }
-
-
Choose the Verify address state, and in the Configuration tab, do the following:
-
For Integration type, keep the default selection of Optimized.
-
For Function name, choose the check-address Lambda function from the dropdown list.
-
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" }
-
-
Choose Next.
Next steps
In the next step, Iterate over items, you’ll learn how to iterate over items.