Use Step Functions and Amazon Batch with error handling - 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).

Use Step Functions and Amazon Batch with error handling

This sample project demonstrates how to use Step Functions to run a batch job with error-handling, with various state types, using Amazon Batch and Amazon SNS. Deploying this sample project will create an Amazon Step Functions state machine, an Amazon Batch job, and an Amazon SNS topic.

In this project, Step Functions uses a state machine to call the Amazon Batch job synchronously. It then waits for the job to succeed or fail, retries and catches errors when a job fails, then sends an Amazon SNS topic with a message about whether the job succeeded or failed.

Create the State Machine and Provision Resources

  1. Open the Error-handling using Amazon Batch and Amazon SNS sample project. The state machine Code and Visual Workflow are displayed.

    
      Manage 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

    • An Amazon SNS topic

  3. Choose Deploy Resources.

    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 notification on an Amazon Batch job completion", "StartAt": "Submit Batch Job", "TimeoutSeconds": 3600, "States": { "Submit Batch Job": { "Type": "Task", "Resource": "arn:aws-cn:states:::batch:submitJob.sync", "Parameters": { "JobName": "BatchJobNotification", "JobQueue": "arn:aws-cn:batch:us-west-2:123456789012:job-queue/BatchJobQueue-123456789abcdef", "JobDefinition": "arn:aws-cn:batch:us-west-2:123456789012:job-definition/BatchJobDefinition-123456789abcdef:1" }, "Next": "Notify Success", "Retry": [ { "ErrorEquals": [ "States.ALL" ], "IntervalSeconds": 30, "MaxAttempts": 2, "BackoffRate": 1.5 } ], "Catch": [ { "ErrorEquals": [ "States.ALL" ], "Next": "Notify Failure" } ] }, "Notify Success": { "Type": "Task", "Resource": "arn:aws-cn:states:::sns:publish", "Parameters": { "Message": "Batch job submitted through Step Functions succeeded", "TopicArn": "arn:aws-cn:sns:us-west-2:123456789012:StepFunctionsSample-BatchJobManagement12345678-9abc-def0-1234-567890abcdef-SNSTopic-A2B3C4D5E6F7G" }, "End": true }, "Notify Failure": { "Type": "Task", "Resource": "arn:aws-cn:states:::sns:publish", "Parameters": { "Message": "Batch job submitted through Step Functions failed", "TopicArn": "arn:aws-cn:sns:us-west-2:123456789012:StepFunctionsSample-BatchJobManagement12345678-9abc-def0-1234-567890abcdef-SNSTopic-A2B3C4D5E6F7G" }, "End": true } } }

IAM Example

This example Amazon Identity and Access Management (IAM) policy 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 BatchJobNotificationAccessPolicy
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "sns:Publish" ], "Resource": [ "arn:aws-cn:sns:us-west-2:123456789012:StepFunctionsSample-BatchJobManagement12345678-9abc-def0-1234-567890abcdef-SNSTopic-A2B3C4D5E6F7G" ], "Effect": "Allow" }, { "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" } ] }

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