

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

# 在 Step Functions 中將參數傳遞至服務 API
<a name="connect-parameters"></a>

**管理狀態和轉換資料**  
了解如何[使用變數在狀態與使用 JSONata 轉換資料之間傳遞資料](workflow-variables.md)。 [ JSONata](transforming-data.md)

在 `Task` 狀態中使用 `Parameters` 欄位，以控制將哪些參數傳遞到服務 API。

在 `Parameters`欄位中，您必須在 API 動作中使用陣列參數的複數形式。例如，如果您使用 `DescribeSnapshots` API 動作的[篩選條件](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSnapshots.html#API_DescribeSnapshots_RequestParameters)欄位與 Amazon EC2 整合，您必須將 欄位定義為 `Filters`。如果您不使用複數形式，Step Functions 會傳回下列錯誤：

```
The field Filter is not supported by Step Functions.
```

## 傳遞靜態 JSON 做為參數
<a name="connect-parameters-json"></a>

您可以直接在狀態機器定義中包含 JSON 物件，以當作參數傳遞到資源。

例如，若要設定 `SubmitJob` API 的 `RetryStrategy` 參數 AWS Batch，您可以在 參數中包含下列項目。

```
"RetryStrategy": {
  "attempts": 5
}
```

您也可以隨著靜態 JSON 傳遞多個參數。作為更完整的範例，以下是發佈至名為 之 Amazon SNS 主題的任務規格的 `Resource`和 `Parameters` 欄位`{{myTopic}}`。

```
"Resource": "arn:aws:states:::sns:publish",
  "Parameters": {
     "TopicArn": "arn:aws:sns:us-east-2:{{account-id}}:{{myTopic}}",
     "Message": "test message",
     "MessageAttributes": {
       "my attribute no 1": {
         "DataType": "String",
         "StringValue": "value of my attribute no 1"
       },
       "my attribute no 2": {
         "DataType": "String",
         "StringValue": "value of my attribute no 2"
       }
     }
  },
```

## 使用路徑將狀態輸入作為參數傳遞
<a name="connect-parameters-path"></a>

您可以使用[路徑](amazon-states-language-paths.md)，將部分狀態輸入做為參數傳遞。路徑是以 開頭的字串`$`，用於識別 JSON 文字中的元件。Step Functions 路徑使用 [JsonPath](https://datatracker.ietf.org/wg/jsonpath/about/) 語法。

若要指定參數使用路徑，請使用 結束參數名稱`.$`。例如，如果您的狀態輸入在名為 的節點內包含文字`message`，您可以使用路徑將該文字做為參數傳遞。

請考慮下列狀態輸入：

```
{
  "comment": "A message in the state input",
  "input": {
    "message": "foo",
    "otherInfo": "bar"
  },
  "data": "example"
}
```

若要傳遞名為 `message` 參數的節點值`myMessage`，請指定下列語法：

```
"Parameters": {"myMessage.$": "$.input.message"},
```

Step Functions 接著會以參數`foo`的形式傳遞值。

如需在 Step Functions 中使用參數的詳細資訊，請參閱下列各項：
+ [處理輸入和輸出](concepts-input-output-filtering.md)
+ [在 Step Functions 工作流程中操作參數](input-output-inputpath-params.md)

## 傳遞內容物件節點做為參數
<a name="connect-parameters-context"></a>

除了來自狀態輸入的靜態內容和節點之外，您還可以從內容物件傳遞節點做為參數。內容物件是在狀態機器執行期間存在的動態 JSON 資料。它包含有關您的狀態機器和目前執行的資訊。您可以使用狀態定義 `Parameters` 欄位中的路徑來存取內容物件。



如需內容物件以及如何從 `"Parameters"` 欄位存取該資料的詳細資訊，請參閱下列內容：
+ [從 Step Functions 中的內容物件存取執行資料](input-output-contextobject.md)
+ [存取內容物件](input-output-contextobject.md#contextobject-access)
+ [從內容物件取得權杖](connect-to-resource.md#wait-token-contextobject)