首页 > 解决方案 > AWS 状态机 ASL:仅在返回数据时使用结果选择器

问题描述

我正在尝试配置任务状态以查找组织,如果找到组织,结果选择器将选择名称、id 和 createTime。然后,这将被添加回 org 节点 (ResultPath) 下的工单。

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

"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"
        }
    ]
}

标签: amazon-web-servicesaws-step-functions

解决方案


这正是选择状态发挥作用的时候。

对原始 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"
    }
  }
}

在此处输入图像描述


推荐阅读