简体   繁体   中英

Use output variable in downstream step on AWS Step Functions Catch method

I'm trying to implement a Saga pattern using API Gateway and simple POST/DELETE endpoints. I first create a University, then an Address referencing the first, and if this step fails I want to delete the University. I don't know how to pass the id of the University to the catch of the Create Address, so I can invoke the DELETE endpoint of the University, using its DB id. Here's my current flow:

{
  "Comment": "Calling ECS service that calls another service",
  "StartAt": "Create University",
  "States": {
    "Create University": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "xxxxxxxxxxx.amazonaws.com",
        "Method": "POST",
        "Path": "universities",
        "Headers": {
          "Content-Type": [
            "application/json"
          ]
        },
        "RequestBody": {
          "name.$": "$.name",
          "country.$": "$.country"
        },
        "AuthType": "NO_AUTH"
      },
      "ResultSelector": {
        "id.$": "$.ResponseBody.id"
      },
      "Next": "Create Address"
    },
    "Create Address": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "xxxxxxxxxxx.amazonaws.com",
        "Method": "POST",
        "Path.$": "States.Format('/universities/{}/address', $.id)",
        "Headers": {
          "Content-Type": [
            "application/json"
          ]
        },
        "RequestBody": {
          "address.$": "$$.Execution.Input.address"
        },
        "AuthType": "NO_AUTH"
      },
      "ResultPath": "$.id",
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          
          "Next": "Delete University"
        }
      ],
      "End": true
    },
    "Delete Address": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "xxxxxxxxxxx.amazonaws.com",
        "Method": "DELETE",
        "Path.$": "States.Format('/universities/{}/address', $.id)",
        "Headers": {
          "Content-Type": [
            "application/json"
          ]
        },
        "AuthType": "NO_AUTH"
      },
      "Next": "Delete University"
    },
    "Delete University": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "xxxxxxxxxxx.amazonaws.com",
        "Method": "DELETE",
        "Path.$": "States.Format('/universities/{}', $.id)",
        "Headers": {
          "Content-Type": [
            "application/json"
          ]
        },
        "AuthType": "NO_AUTH"
      },
      "End": true
    }
  }
}

I need to enter the Delete University step with the id that the Create University step returned, so I can call the DELETE endpoint correctly. Any ideas?

docs : Use ResultPath in a Catch to include the error with the original input, instead of replacing it.

"Catch": [
  {
    "ErrorEquals": [ "States.ALL"],
    "ResultPath": "$.error",
    "Next": "Delete University"
  }
],

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