Fan out an Amazon Batch job - 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).

Fan out an Amazon Batch job

This sample project demonstrates how to use Step Functions’s Map state to fan out Amazon Batch jobs. Deploying this sample project will create an Amazon Step Functions state machine, a Lambda function and an Amazon Batch job queue.

In this project, Step Functions uses a state machine to invoke a Lambda function to do simple pre-processing, then invokes multiple Amazon Batch jobs in parallel using the map state.

Create the State Machine and Provision Resources

  1. Open the Fan out an Amazon Batch job sample project.The state machine Code and Visual Workflow are displayed.

    
            Fan out Amazon Batch workflow.
  2. Choose Next.

    The Deploy and run page is displayed, listing the resources that will be created. For this sample project, the resources include:

    • An Amazon Batch job queue

    • A Lambda function

  3. Choose Deploy and run.

    Note

    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.

Start a New Execution

  1. On the New execution page, enter an execution name (optional), and then choose Start Execution.

  2. (Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

  3. Optionally, you can go to the newly created state machine on the Step Functions Dashboard, and then choose New execution.

  4. When an execution is complete, you can select states on the Visual workflow and browse the Input and Output under Step details.

Example State Machine Code

The state machine in this sample project integrates with Amazon Batch and Amazon SNS by passing parameters directly to those resources.

Browse through this example state machine to see how Step Functions controls Amazon Batch and Amazon SNS by connecting to the Amazon Resource Name (ARN) in the Resource field, and by passing Parameters to the service API.

For more information about how Amazon Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.

{ "Comment": "An example of the Amazon States Language for fanning out Amazon Batch job", "StartAt": "Generate batch job input", "TimeoutSeconds": 3600, "States": { "Generate batch job input": { "Type": "Task", "Resource": "arn:aws-cn:states:::lambda:invoke", "OutputPath": "$.Payload", "Parameters": { "FunctionName": "<GENERATE_BATCH_JOB_INPUT_LAMBDA_FUNCTION_NAME>" }, "Next": "Fan out batch jobs" }, "Fan out batch jobs": { "Comment": "Start multiple executions of batch job depending on pre-processed data", "Type": "Map", "End": true, "ItemsPath": "$", "Parameters": { "BatchNumber.$": "$$.Map.Item.Value" }, "Iterator": { "StartAt": "Submit Batch Job", "States": { "Submit Batch Job": { "Type": "Task", "Resource": "arn:aws-cn:states:::batch:submitJob.sync", "Parameters": { "JobName": "BatchJobFanOut", "JobQueue": "<BATCH_QUEUE_ARN>", "JobDefinition": "<BATCH_JOB_DEFINITION_ARN>" }, "End": true } } } } } }

IAM Example

These example Amazon Identity and Access Management (IAM) policies generated by the sample project includes the least privilege necessary to execute the state machine and related resources. We recommend that you include only those permissions that are necessary in your IAM policies.

Example BatchJobFanOutAccessPolicy
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "batch:SubmitJob", "batch:DescribeJobs", "batch:TerminateJob" ], "Resource": "*", "Effect": "Allow" }, { "Action": [ "events:PutTargets", "events:PutRule", "events:DescribeRule" ], "Resource": [ "arn:aws-cn:events:us-west-2:123456789012:rule/StepFunctionsGetEventsForBatchJobsRule" ], "Effect": "Allow" } ] }
Example InvokeGenerateBatchJobMapLambdaPolicy
{ "Statement": [ { "Action": [ "lambda:InvokeFunction" ], "Resource": "arn:aws-cn:lambda:us-west-2:123456789012:function:StepFunctionsSample-BatchJobFa-GenerateBatchJobMap-444455556666", "Effect": "Allow" } ] }

For information about how to configure IAM when using Step Functions with other Amazon services, see IAM Policies for integrated services.