如何使用 Choice 来测试输入数组是否包含给定的过滤器?

Bre*_*yan 6 amazon-web-services aws-step-functions

给定一个Map状态,其输出是一个类似于以下内容的数组:

[
  {
    "ProcessState": {
      "Status": "SUCCESS"
    }
  },
  {
    "ProcessState": {
      "Status": "SUCCESS"
    }
  },
  {
    "ProcessState": {
      "Status": "FAILURE"
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

我希望能够测试是否有一个 Status = 'FAILURE' 的元素。我尝试使用 a Choicewith a choice 如下:

{
  "Variable": "$..ProcessState[?(Status == FAILURE)]",
  "IsPresent": true,
  "Next": "Items Contained Failure"
}
Run Code Online (Sandbox Code Playgroud)

当尝试这个时我得到Value is not a Reference Path: Illegal '..' ...

我正在考虑尝试使用 aPass作为中间步骤,但我认为如果没有条目匹配,它就会失败,因为它找不到任何内容。

Bre*_*yan 5

Map通过让状态ResultSelector执行过滤已经解决了这个问题。

"ResultSelector": {
  "QueueFailures.$": "$[?(@.ProcessState.Status == 'FAILED')]"
},
"ResultPath": "$.ProcessResult"
Run Code Online (Sandbox Code Playgroud)

状态Choice现在可以只测试第一个失败项是否存在。

"Test Queue Failures": {
  "Type": "Choice",
  "Default": "Mark Decision Run Ready",
  "Choices": [
    {
      "Variable": "$.ProcessResult.QueueFailures[0]",
      "IsPresent": true,
      "Next": "Queue Contains Failures"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

虽然说明了解决方案但省略了大部分业务案例,但以下内容将其放在更完整的上下文中:

{
  // register our exit states
  "Failure Path": { "Type": "Fail"  },
  "Happy Path":   { "Type": "Succeed" },
  
  // map process (much removed)
  "My Map Task": {
    "Type": "Map",
    "Next": "Test Queue Failures",
    "ItemsPath": "$.Queue.Items",
    "Parameters": {
      "Item.$": "$$.Map.Item.Value"
    },

    "ResultSelector": {
      // this selector takes the error info from a batch job task.
      "QueueFailures.$": "$[?(@.ErrorInfo)]",
      "QueueItems.$": "$"
    },
    "ResultPath": "$.ProcessResult",

    "Iterator": {
      "StartAt": "First Map Item Task",
      "States": {
        "First Map Item Task": {
          "Type": "Task",
          "Resource": "arn:aws:states:::batch:submitJob.sync",
          "Next": "Complete Entry",
          // this is the key to storing the error info from the batch job
          "Catch": [
            {
              "ResultPath": "$.ErrorInfo",
              "ErrorEquals": ["States.ALL"],
              "Next": "Fail Entry"
            }
          ]
          // result selector and path omitted
        },
        
        // the following two lambdas allow me to capture state via a lambda

        // happy path for a single item iteration.
        "Complete Entry": {
          "Type": "Task",
          "Resource": "arn:aws:states:::lambda:invoke",
          "End": true,
          "OutputPath": "$.Payload"
        },

        // sad path for a single item iteration
        "Fail Entry": {
          "Type": "Task",
          "Resource": "arn:aws:states:::lambda:invoke",
          "End": true,
          "OutputPath": "$.Payload"
        }
      }
    }
  },
  
  // here we check for our fail state by finding at least 1 array being present.
  "Test Queue Failures": {
    "Type": "Choice",
    "Default": "Happy Path",
    "Choices": [
      {
        "Variable": "$.ProcessResult.QueueFailures[0]",
        "IsPresent": true,
        "Next": "Queue Contains Failures"
      }
    ]
  }

}
Run Code Online (Sandbox Code Playgroud)