

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Step Functions 執行AWS Batch工作負載
<a name="connect-batch"></a>

您可以將 Step Functions 與 整合AWS Batch，以在AWS雲端中執行批次運算工作負載。此頁面列出支援的 AWS BatchAPIs，並提供執行批次處理任務的範例`Task`狀態。

若要了解如何在 Step Functions 中整合 AWS服務，請參閱 [整合 服務](integrate-services.md)和 [在 Step Functions 中將參數傳遞至服務 API](connect-parameters.md)。

**Optimized AWS Batch整合的主要功能**  
[執行任務 (.sync)](connect-to-resource.md#connect-sync) 整合模式可供使用。
請注意， [請求回應](connect-to-resource.md#connect-default)或 [使用任務字符等待回呼](connect-to-resource.md#connect-wait-token)整合模式沒有特定的最佳化。

以下顯示提交AWS Batch任務並等待任務完成的範例`Task`狀態。顯示的許多引數都是選用的。

```
"Submit Batch Job": {
    "Type": "Task",
    "Resource": "arn:aws:states:::batch:submitJob.sync",
    "Arguments": {
        "JobName": "BATCH_NAME",
        "JobQueue": "BATCH_QUEUE_ARN",
        "JobDefinition": "BATCH_JOB_DEFINITION_ARN",
        "ArrayProperties": {
        "Size": 10
        },
        "ContainerOverrides": {
        "ResourceRequirements": [
            {
            "Type": "VCPU",
            "Value": "4"
            }
        ]
        },
        "DependsOn": [
        {
            "JobId": "myJobId",
            "Type": "SEQUENTIAL"
        }
        ],
        "PropagateTags": true,
        "Arguments": {
        "Key1": "value1",
        "Key2": 100
        },
        "RetryStrategy": {
        "Attempts": 1
        },
        "Tags": {
        "Tag": "TAG"
        },
        "Timeout": {
        "AttemptDurationSeconds": 10
        }
    }
}
```

## AWS BatchAPIs
<a name="connect-batch-api"></a>
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html)

**中的參數Step Functions以 PascalCase 表示**  
即使原生服務 API 位於 camelCase 中，例如 API 動作 `startSyncExecution`，您也可以在 PascalCase 中指定參數，例如：`StateMachineArn`。

## 用於呼叫 的 IAM 政策AWS Batch
<a name="batch-iam"></a>

下列範例範本顯示 如何根據狀態機器定義中的資源AWS Step Functions產生 IAM 政策。如需詳細資訊，請參閱[Step Functions 如何為整合服務產生 IAM 政策](service-integration-iam-templates.md)及[探索 Step Functions 中的服務整合模式](connect-to-resource.md)。

由於`TerminateJob`會產生 `SubmitJob`和 的任務 ID，因此只能在執行時間知道，因此您無法建立根據特定資源限制存取的政策。

**精細存取的秘訣**  
若要新增對 `SubmitJob`和 的精細存取`TerminateJob`，請考慮使用任務的標籤，並根據您的標籤建立限制存取的政策。此外，任務佇列、定義和消耗性資源可以限制`SubmitJob`使用已知資源。

------
#### [ Run a Job (.sync) ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "batch:SubmitJob",
                "batch:DescribeJobs",
                "batch:TerminateJob"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
               "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventsForBatchJobsRule"
            ]
        }
    ]
}
```

------
#### [ Request Response ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "batch:SubmitJob"
            ],
            "Resource": "*"
        }
    ]
}
```

------