AWS 状态机 ASL:仅在返回数据时使用结果选择器

AWS State Machine ASL: Use the Result Selector only if data is returned

我正在尝试配置任务状态以查找组织,如果找到组织,结果选择器将 select 名称、ID 和创建时间。然后将其添加回组织节点 (ResultPath) 下的工单。

我遇到的问题是,如果未找到组织,则状态机执行将被取消,因为结果选择器正在尝试 select 结果中不存在的节点。有谁知道是否有办法仅在返回数据时使用结果选择器?即使找不到组织,我也希望状态机继续。

"Find Organization": {
    "Type": "Task",
    "Resource": "${aws_lambda_function.test_function.arn}",
    "Next": "Find User Account",
    "Parameters": {
        "name": "FindOrganization",
        "request.$": "$.item"
    },
    "ResultSelector": {
        "name.$": "$.name",
        "id.$": "$.id",
        "createTime.$": "$.createTime"
    },
    "ResultPath": "$.org",
    "Catch": [
        {
            "ErrorEquals": [
                "States.ALL"
            ],
            "ResultPath": "$.error",
            "Next": "Publish SNS Failure"
        }
    ]
}

这正是 Choice State 发挥作用的时候。

对原始 ASL 定义的示例修改是

{
  "Comment": "An example of the Amazon States Language using a choice state.",
  "StartAt": "Find Organization",
  "States": {
    "Find Organization": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Next": "Check If Organization Exists",
      "Parameters": {
        "name": "FindOrganization",
        "request.$": "$.item"
      },
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "Publish SNS Failure"
        }
      ]
    },
    "Check If Organization Exists": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$",
          "IsNull": false,
          "Next": "Organization Exists"
        },
        {
          "Variable": "$",
          "IsNull": true,
          "Next": "Organization Not Exists"
        }
      ],
      "Default": "Organization Exists"
    },
    "Organization Exists": {
      "Type": "Pass",
      "Parameters": {
        "name.$": "$.name",
        "id.$": "$.id",
        "createTime.$": "$.createTime"
      },
      "ResultPath": "$.org",
      "Next": "Find User Account"
    },
    "Organization Not Exists": {
      "Type": "Fail"
    },
    "Find User Account": {
      "Type": "Succeed"
    },
    "Publish SNS Failure": {
      "Type": "Fail"
    }
  }
}