Parallel Processing

Add parallel processing step

If the uploaded photo passes both the FaceDetection and CheckFaceDuplicate stage, we can now proceed to index the rider’s face and resize the photo for displaying in the app. Since these two steps don’t depend on one another, they can be run in parallel. We will add a Parallel state in AWS Step Functions to run these steps.

The ARNs of the two AWS Lambda functions that index faces and generate thumbnails can be found in the AWS CloudFormation output IndexFaceFunctionArn and ThumbnailFunctionArn respectively.

  1. Edit your rider-photo-state-machine.json file to add a parallel step (with two sub-steps) to the workflow.

    First, add a new state ParallelProcessing following the CheckFaceDuplicate state. Also make sure:

    • Replace the REPLACE_WITH_IndexFaceFunctionArn with the IndexFaceFunctionArn from the AWS CloudFormation output.
    • Replace the REPLACE_WITH_ThumbnailFunctionArn with the ThumbnailFunctionArn from the AWS CloudFormation output.
    ,
    "ParallelProcessing": {
    	"Type": "Parallel",
    	"Branches": [
    		{
    			"StartAt": "AddFaceToIndex",
    			"States": {
    				"AddFaceToIndex": {
    					"Type": "Task",
    					"Resource": "REPLACE_WITH_IndexFaceFunctionArn",
    					"End": true
    				}
    			}
    		},
    		{
    			"StartAt": "Thumbnail",
    			"States": {
    				"Thumbnail": {
    					"Type": "Task",
    					"Resource": "REPLACE_WITH_ThumbnailFunctionArn",
    					"End": true
    				}
    			}
    		}
    	],
    	"ResultPath": "$.parallelResult",
    	"End": true
    }	
  2. Find the line in the CheckFaceDuplicate state that marks it as the End state of the state machine.

    "End": true,

    and replace it with

    "Next": "ParallelProcessing",

    This tells AWS Step Functions if the CheckFaceDuplicate state runs successfully, go on to run the ParallelProcessing state as the next step in the process.

  3. At this point, your rider-photo-state-machine.json file should look like this (the AWS Lambda ARNs are examples):

    (expand to see)
  4. Go back to the AWS Step Functions Console and click the Edit state machine button to update the RiderPhotoProcessing state machine.

  5. Paste the updated JSON definition and click the refresh button in the preview panel to visualize the changes:

    Update State Machine with parallel step
  6. Click the Save button to save the state machine.

  7. Click the Start execution button to test the new state machine with the test input you’ve used before:

    {
    	"userId": "user_a",
    	"s3Bucket": "REPLACE_WITH_YOUR_BUCKET_NAME",
    	"s3Key": "1_happy_face.jpg"
    }	
  8. If the last step succeeds, you can use the AWS CLI to check the list of faces indexed in your Rekognition collection (replace the REPLACE_WITH_YOUR_CHOSEN_AWS_REGION portion with the region string of your chosen region):

    aws rekognition list-faces \
      --collection-id rider-photos \
      --region REPLACE_WITH_YOUR_CHOSEN_AWS_REGION
    
  9. You might find the delete-faces command useful when testing:

    aws rekognition delete-faces \
      --collection-id rider-photos \
      --face-ids REPLACE_WITH_ID_OF_FACE_TO_DELETE \
      --region REPLACE_WITH_YOUR_CHOSEN_AWS_REGION
    
  10. You can also use the Amazon S3 Console to check the Amazon S3 bucket created by AWS CloudFormation to store the resized thumbnail images. You should find resized thumbnail images in the bucket.

The name of the S3 bucket can be found in the in AWS CloudFormation output ThumbnailS3Bucket. You can also simply search for it in the S3 Console for wildrydes-step-module-resources-thumbnails3bucket

Checking for duplicates

What happens if you start a new workflow with a different userId but the same s3key and s3bucket parameters?