简体   繁体   中英

Break an map loop execution in AWS step functions

I'm trying to build a step function with a loop (Map) inside that can be stopped whenever a specefic Error is thrown, something like this

        "Job": {
          "Type": "Map",
          "InputPath": "$.content",
          "ItemsPath": "$.data",
          "MaxConcurrency": 0,
          "Iterator": {
            "StartAt": "Validate",
            "States": {
              "Validate": {
                "Type": "Task",
                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ship-val",
                "Catch": [
                    {
                      "ErrorEquals": [
                        "ErrorOne"
                      ],
                      "Next": "BreakLoop"
                    },
                    {
                      "ErrorEquals": ["States.ALL"],
                      "Next": "FailUncaughtError"
                    }
                ],
              },
              "FailUncaughtError":{
                "Type": "Fail",
                "Error": "Uncaught error"
              },
              "BreakLoop":{
                "Type": "Fail",
                "Error": "the loop should be stopped"
              }
            }
          },
          "ResultPath": "$.content.data",
          "End": true
        }

I tried to make the Next element of the Catch to a state outside the Map but I couldn't because the Map accept only states within it. Moreover, AFAIK there is no mention for a feature like this in AWS docs

Instead of catching the error inside the Map state, don't catch it and let the Map state to fail. And add a catch to Map state and if the error is equal to what you are looking for continue to the next step:

{
  "StartAt": "Map",
  "States": {
    "Map": {
      "Type": "Map",
      "ItemsPath": "$.array",
      "Iterator": {
        "StartAt": "FaultyLambda",
        "States": {
          "FaultyLambda": {
                  "Type": "Task",
                  "Resource": "arn:aws:states:::lambda:invoke",
                  "Parameters": {
                    "FunctionName": "your function arn",
                    "Payload": {
                      "a": 1
                    }
                  },
                  "End": true
                }
        }
      },
      "Catch": [
        {
          "ErrorEquals": ["ErrorOne"],
          "Next": "BreakLoop"
        }
      ],
      "Next": "BreakLoop"
    },
    "BreakLoop": {
      "Type": "Pass",
      "End": true
    }
  }
}

Any other error will not be catched and failed your entire execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM