

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

# 從執行中執行啟動新的 AWS Step Functions 狀態機器
<a name="connect-stepfunctions"></a>

Step Functions 整合了自己的 API 做為服務整合。了解如何使用 Step Functions，直接從執行中的任務狀態啟動狀態機器的新執行。在建置新的工作流程時，使用[巢狀工作流程執行](concepts-nested-workflows.md)來降低主要工作流程的複雜性，並重複使用常見的程序。

**Optimized Step Functions 整合的主要功能**  
[執行任務 (.sync)](connect-to-resource.md#connect-sync) 整合模式可供使用。

如需詳細資訊，請參閱下列內容：
+ [從任務開始](concepts-nested-workflows.md)
+ [整合 服務](integrate-services.md)
+ [在 Step Functions 中將參數傳遞至服務 API](connect-parameters.md)

## 最佳化的 Step Functions APIs
<a name="connect-stepfunctions-api"></a>
+ [https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html)

## 工作流程範例
<a name="connect-stepfunctions-api-examples"></a>

以下包含 `Task` 狀態，此狀態會啟動另一個狀態機器的執行並等待其完成。

```
{  
   "Type":"Task",
   "Resource":"arn:aws:states:::states:startExecution.sync:2",
   "Arguments":{  
      "Input":{
        "Comment": "Hello world!"
       },
      "StateMachineArn":"arn:aws:states:{{region}}:{{account-id}}:stateMachine:HelloWorld",
      "Name":"ExecutionName"
   },
   "End":true
}
```

以下包含 `Task` 狀態，此狀態會啟動另一個狀態機器的執行。

```
{  
   "Type":"Task",
   "Resource":"arn:aws:states:::states:startExecution",
   "Arguments":{  
      "Input":{
        "Comment": "Hello world!"
       },
      "StateMachineArn":"arn:aws:states:{{region}}:{{account-id}}:stateMachine:HelloWorld",
      "Name":"ExecutionName"
   },
   "End":true
}
```

以下包含 `Task` 狀態，此狀態會實作[回呼](connect-to-resource.md#connect-wait-token)服務整合模式。

```
{ 
   "Type":"Task",
   "Resource":"arn:aws:states:::states:startExecution.waitForTaskToken",
   "Arguments":{ 
      "Input":{
        "Comment": "Hello world!",
        "token": "{% $states.context.Task.Token %}"
       },
      "StateMachineArn":"arn:aws:states:{{region}}:{{account-id}}:stateMachine:HelloWorld",
      "Name":"ExecutionName"
   },
   "End":true
}
```

若要將巢狀工作流程執行與啟動它的父系執行建立關聯，請傳遞特別命名的參數，其中包含從[內容物件](input-output-contextobject.md)提取的執行 ID。啟動巢狀執行時，請使用名為 `AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID` 的參數。使用 傳遞執行 ID 並參考內容物件中的 ID`$states.context.Execution.Id`。如需詳細資訊，請參閱[存取內容物件](input-output-contextobject.md#contextobject-access)。

```
{  
   "Type":"Task",
   "Resource":"arn:aws:states:::states:startExecution.sync",
   "Arguments":{  
      "Input":{
        "Comment": "Hello world!",
        "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID": "{% $states.context.Execution.Id %}"
       },
      "StateMachineArn":"arn:aws:states:{{region}}:{{account-id}}:stateMachine:HelloWorld",
      "Name":"ExecutionName"
   },
   "End":true
}
```

 巢狀狀態機器會傳回下列內容：


| 資源 | Output | 
| --- | --- | 
| startExecution.sync | String | 
| startExecution.sync:2 | JSON | 

兩者都將等待巢狀狀態機器完成，但它們會傳回不同的 `Output` 格式。例如，如果您建立傳回物件 的 Lambda 函數`{ "MyKey": "MyValue" }`，您會得到下列回應：

對於 startExecution.sync：

```
{
   {{<other fields>}}
   "Output": "{ \"MyKey\": \"MyValue\" }" 
}
```

對於 startExecution.sync:2：

```
{
   {{<other fields>}} 
   "Output": {
      "MyKey": "MyValue"
   }
}
```

### 設定巢狀狀態機器的 IAM 許可
<a name="nested-stepfunctions-iam-permissions"></a>

父系狀態機器會判斷子系狀態機器是否使用輪詢和事件完成執行。輪詢需要 的許可`events:PutRule`，`states:DescribeExecution`而透過 EventBridge 傳送至 Step Functions 的事件需要 `events:PutTargets`、 和 的許可`events:DescribeRule`。如果您的 IAM 角色缺少這些許可，在父系狀態機器得知子系狀態機器的執行完成之前，可能會有延遲。

對於`StartExecution`呼叫單一巢狀工作流程執行的狀態機器，請使用限制該狀態機器許可的 IAM 政策。

## 用於呼叫巢狀 Step Functions 工作流程的 IAM 政策
<a name="stepfunctions-iam"></a>

對於`StartExecution`呼叫單一巢狀工作流程執行的狀態機器，請使用限制該狀態機器許可的 IAM 政策。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "states:StartExecution"
            ],
            "Resource": [
                "arn:aws:states:{{us-east-1}}:{{123456789012}}:stateMachine:{{myStateMachineName}}"
            ]
        }
    ]
}
```

如需詳細資訊，請參閱下列內容：
+ [將 服務與 Step Functions 整合](integrate-services.md)
+ [在 Step Functions 中將參數傳遞至服務 API](connect-parameters.md)
+ [從執行中執行啟動新的 AWS Step Functions 狀態機器](#connect-stepfunctions)

------
#### [ Synchronous ]<a name="sync-async-iam-policies"></a>

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "states:StartExecution"
            ],
            "Resource": [
                "arn:aws:states:{{us-east-1}}:{{123456789012}}:stateMachine:{{stateMachineName}}"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "states:DescribeExecution",
                "states:StopExecution"
            ],
            "Resource": [
               "arn:aws:states:{{us-east-1}}:{{123456789012}}:execution:{{myStateMachineName}}:*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
               "arn:aws:events:{{us-east-1}}:{{123456789012}}:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule"
            ]
        }
    ]
}
```

------
#### [ Asynchronous ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "states:StartExecution"
            ],
            "Resource": [
                "arn:aws:states:{{us-east-1}}:{{123456789012}}:stateMachine:{{myStateMachineName}}"
            ]
        }
    ]
}
```

------

**所需的 ARN 類型**  
在**同步**政策中，請注意 `states:StartExecution`需要狀態機器 ARN，而 `states:DescribeExecution`和 `states:StopExecution`需要執行 ARN。  
如果您錯誤地合併了這三個動作，JSON 將有效，但 IAM 政策將不正確。不正確的政策可能會導致工作流程執行期間停滯的工作流程和/或存取問題。

如需巢狀工作流程執行的詳細資訊，請參閱[從 Step Functions 中的任務狀態啟動工作流程執行](concepts-nested-workflows.md)。