範例:在 Step Functions 數工作流程中操作狀態資料 - AWS Step Functions

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

範例:在 Step Functions 數工作流程中操作狀態資料

本主題包含如何JSON使用 InputPath、 ResultPath和 OutputPath 欄位操作狀態輸入和輸出的範例。

狀態或狀態以外的失敗的工作流程任何成功工作流狀態狀態都可以包括輸入和輸出處理欄位InputPath,例如ResultPath、或OutputPath。此外,等待工作流程狀選擇工作流狀態狀態不支援ResultPath欄位。透過這些欄位,您可以使用 a JsonPath來篩選JSON資料在工作流程中移動時。

您也可以使用Parameters欄位,在JSON資料在工作流程中移動時操作資料。如需使用 Parameters 的相關資訊,請參閱 使用 Step Functions 數工作流程中的參數來操作狀態

例如,從 AWS Lambda 建立使用 Lambda 的 Step Functions 狀態機本教程中描述的功能和狀態機。修改狀態機器,使其包含下列 InputPathResultPathOutputPath

{ "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloFunction", "InputPath": "$.lambda", "ResultPath": "$.data.lambdaresult", "OutputPath": "$.data", "End": true } } }

使用以下輸入開始執行。

{ "comment": "An input comment.", "data": { "val1": 23, "val2": 17 }, "extra": "foo", "lambda": { "who": "AWS Step Functions" } }

假設commentextra節點可以捨棄,但您想要包含 Lambda 函數的輸出,並保留data節點中的資訊。

在已更新的狀態機器中,Task 狀態會更改為處理任務輸入。

"InputPath": "$.lambda",

狀態機器定義中的這一行會將任務輸入限制為僅限任務輸入中的 lambda 節點。Lambda 函數只接收JSON物件{"who": "AWS Step Functions"}作為輸入。

"ResultPath": "$.data.lambdaresult",

ResultPath告訴狀態機將 Lambda 函數的結果插入到名為的節點中lambdaresult,作為原始狀態機輸入中data節點的子節點。因為您沒有對原始輸入和使用的結果執行任何其他操作OutputPath,因此狀態的輸出現在會包含 Lambda 函數與原始輸入的結果。

{ "comment": "An input comment.", "data": { "val1": 23, "val2": 17, "lambdaresult": "Hello, AWS Step Functions!" }, "extra": "foo", "lambda": { "who": "AWS Step Functions" } }

但是,我們的目標是只保留data節點,並包含 Lambda 函數的結果。 OutputPath在將其傳遞給狀態輸出JSON之前過濾此組合。

"OutputPath": "$.data",

這只會選取原始輸入中要傳遞到輸出的 data 節點 (包括 ResultPath 所插入的 lambdaresult 子系)。狀態輸出會篩選到下列。

{ "val1": 23, "val2": 17, "lambdaresult": "Hello, AWS Step Functions!" }

在此 Task 狀態中:

  1. InputPath只會將lambda節點從輸入傳送至 Lambda 函數。

  2. ResultPath 會插入結果做為原始輸入中 data 節點的子節點。

  3. OutputPath過濾狀態輸入(現在包括 Lambda 函數的結果),以便僅將data節點傳遞給狀態輸出。

範例 操作原始狀態機輸入,結果和最終輸出 JsonPath

考慮下面的狀態機來驗證保險申請人的身份和地址。

注意

若要檢視完整範例,請參閱如何在 Step Functions 中使用JSON路徑

{ "Comment": "Sample state machine to verify an applicant's ID and address", "StartAt": "Verify info", "States": { "Verify info": { "Type": "Parallel", "End": true, "Branches": [ { "StartAt": "Verify identity", "States": { "Verify identity": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "Payload.$": "$", "FunctionName": "arn:aws:lambda:us-east-2:111122223333:function:check-identity:$LATEST" }, "End": true } } }, { "StartAt": "Verify address", "States": { "Verify address": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "Payload.$": "$", "FunctionName": "arn:aws:lambda:us-east-2:111122223333:function:check-address:$LATEST" }, "End": true } } } ] } } }

如果您使用下列輸入來執行此狀態機器,則執行會失敗,因為執行驗證的 Lambda 函數只需要將需要驗證的資料做為輸入。因此,您必須使用適當的指定包含要驗證之資訊的節點 JsonPath。

{ "data": { "firstname": "Jane", "lastname": "Doe", "identity": { "email": "jdoe@example.com", "ssn": "123-45-6789" }, "address": { "street": "123 Main St", "city": "Columbus", "state": "OH", "zip": "43219" }, "interests": [ { "category": "home", "type": "own", "yearBuilt": 2004 }, { "category": "boat", "type": "snowmobile", "yearBuilt": 2020 }, { "category": "auto", "type": "RV", "yearBuilt": 2015 }, ] } }

若要指定 check-identity Lambda 函數必須使用的節點,請依下列方式使用InputPath欄位:

"InputPath": "$.data.identity"

若要指定 check-address Lambda 函數必須使用的節點,請依下列方式使用InputPath欄位:

"InputPath": "$.data.address"

現在,如果要將驗證結果存儲在原始狀態機輸入中,請使用以下ResultPath字段:

"ResultPath": "$.results"

但是,如果您只需要身份和驗證結果並丟棄原始輸入,請按如下方式使用該OutputPath字段:

"OutputPath": "$.results"

如需詳細資訊,請參閱在 Step Functions 中處理輸入和輸出