Process a CSV file from Amazon S3 using a Distributed Map - AWS Step Functions

Process a CSV file from Amazon S3 using a Distributed Map

This sample project demonstrates how you can use the Distributed Map state to iterate over 10,000 rows of a CSV file that is generated using a Lambda function. The CSV file contains shipping information of customer orders and is stored in an Amazon S3 bucket. The Distributed Map iterates over a batch of 10 rows in the CSV file for data analysis.

The Distributed Map contains a Lambda function to detect any delayed orders. The Distributed Map also contains an Inline Map to process the delayed orders in a batch and returns these delayed orders in an array. For each delayed order, the Inline Map sends a message to an Amazon SQS queue. Finally, this sample project stores the Map Run results to another Amazon S3 bucket in your AWS account.

With Distributed Map, you can run up to 10,000 parallel child workflow executions at a time. In this sample project, the maximum concurrency of Distributed Map is set at 1000 that limits it to 1000 parallel child workflow executions.

This sample project creates the state machine, the supporting AWS resources, and configures the related IAM permissions. Explore this sample project to learn about using the Distributed Map for orchestrating large-scale, parallel workloads, or use it as a starting point for your own projects.

AWS CloudFormation template and additional resources

You use a CloudFormation template to deploy this sample project. This template creates the following resources in your AWS account:

  • A Step Functions state machine.

  • Execution role for the state machine. This role grants the permissions that your state machine needs to access other AWS services and resources such as the Lambda function's Invoke action.

  • A Lambda function named CSVGeneratorFunction that generates a CSV file which contains the customer order details.

  • Execution role for the CSV generator Lambda function. This role grants the function permission to access other AWS services.

  • An Amazon S3 input bucket to store the generated CSV file.

  • A delayed order detection Lambda function that analyzes the CSV file data and detects any delayed orders.

  • Execution role for the delayed order Lambda function. This role grants the function permission to access other AWS services.

  • An Amazon S3 output bucket to store the analysis results of the customer orders.

  • An Amazon SQS queue to which Step Functions sends messages for every delayed order. These messages contain the IDs of the customers and their orders.

  • A CloudWatch log group that stores information related to the state machine’s execution history.

Important

Standard charges apply for each service.

Step 1: Create the state machine and provision resources

  1. Open the Step Functions console and choose Create state machine.

  2. Type Distributed Map to process a CSV file in S3 in the search box, and then choose Distributed Map to process a CSV file in S3 from the search results that are returned.

  3. Choose Next to continue.

  4. Choose Run a demo to create a read-only and ready-to-deploy workflow, or choose Build on it to create an editable state machine definition that you can build on and later deploy.

    For information about the resources that will be created for this sample project, see AWS CloudFormation template and additional resources.

    The following image shows the workflow graph for the Distributed Map to process a CSV file in S3 sample project:

    Workflow graph of the Distributed Map to process a CSV file in S3 sample project.
  5. Choose Use template to continue with your selection.

Next steps depend on your previous choice:

  1. Run a demo – You can review the state machine before you create a read-only project with resources deployed by AWS CloudFormation to your AWS account.

    You can view the state machine definition, and when you are ready, choose Deploy and run to deploy the project and create the resources.

    Deploying can take up to 10 minutes to create resources and permissions. You can use the Stack ID link to monitor progress in AWS CloudFormation.

    After deploy completes, you should see your new state machine in the console.

  2. Build on it – You can review and edit the workflow definition. You might need to set values for placeholders in the sample project before attemping to run your custom workflow.

Note

Standard charges might apply for services deployed to your account.

Step 2: Run the state machine

After all the resources are provisioned and deployed, you can run the state machine.

  1. On the State machines page, choose your sample project.

  2. On the sample project page, choose Start execution.

  3. In the Start execution dialog box, do the following:

    1. (Optional) Enter input values in JSON format to run your sample project.

      If you chose to Run a demo, you need not provide any execution input.

      Note

      If the demo project you deployed contains prepopulated execution input data, use that input to run the state machine.

    2. Choose Start execution.

    3. (Optional) The Step Functions console directs you to a page that's titled with your execution ID. This page is known as the Execution Details page. On this page, you can review the execution results as the execution progresses or after it's complete.

      After the execution is complete, choose individual states on the Graph view, and then choose the individual tabs on the Step details pane to view each state's details including input, output, and definition respectively.

      • For details about the execution information you can view on the Execution Details page, see Execution details overview.

      • For more information about viewing a Distributed Map state's execution in the console, see Viewing Map Runs.

    4. (Optional) Review the execution results exported to the Amazon S3 bucket. These results include data, such as execution input and output, ARN, and execution status. For more information, see ResultWriter (Map).