Search code examples
amazon-web-servicescloudstate-machineaws-step-functions

Fail not the Step Function but the one iteration


I have created a Step Function with Iterator and added Catch task for it:

  {
    "StartAt": "Reader",
    "States": {
      "Reader": {
        "Type": "Task",
        "Resource": "arn:aws:lambda:reader",
        "Next": "Processing"
      },
      "Processing": {
        "Type": "Map",
        "ItemsPath": "$",
        "MaxConcurrency": 1,
        "Next": "Success",
        "Iterator": {
          "StartAt": "ProcessFixes",
          "States": {
            "ProcessFixes": {
              "Type": "Task",
              "Resource": "arn:aws:lambda:processing",
              "Next": "Processed",
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "MaxAttempts": 2
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "ProcessingFailed"
                }
              ]
            },
            "ProcessingFailed": {
              "Type": "Fail"
            },
            "Processed": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      },
      "Success": {
        "Type": "Succeed"
      }
    }
  }

Here are the steps:

Steps

So currently when the exception appears it fails the whole Step Function progress. I need to let the one of the iteration been failed and marked as failed. So 'Pass' task does not work for me.

It would be great to have it as Failed in "Map Iteration Details" Tab:

Map Iteration Details


Solution

  • Change ProcessingFailed from Fail to Pass. Add a success: boolean key to the output of both map branches. After the map task finishes, you can handle the success (success: true) and failure (success: false) cases as appropriate.

    "Processed": {
      "Type": "Pass",
      "Result": {
        "success":true,
      },
      "ResultPath": "$.result",
      "Next": "End"
    }
    
    "ProcessingFailed": {
      "Type": "Pass",
      "Result": {
        "success":false,
      },
      "ResultPath": "$.result",
      "Next": "End"
    }