在 AWS Step Functions Catch 方法的下游步骤中使用输出变量

发布于 2025-01-09 10:18:53 字数 2617 浏览 0 评论 0原文

我正在尝试使用 API 网关和简单的 POST/DELETE 端点来实现 Saga 模式。我首先创建一所大学,然后创建一个引用第一个大学的地址,如果此步骤失败,我想删除该大学。我不知道如何将大学的 id 传递给创建地址的捕获,因此我可以使用大学的 DB id 调用大学的 DELETE 端点。这是我当前的流程:

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

我需要使用 Create University 步骤返回的 ID 进入 Delete University 步骤,以便我可以正确调用 DELETE 端点。有什么想法吗?

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.
quot;: "$.name",
          "country.
quot;: "$.country"
        },
        "AuthType": "NO_AUTH"
      },
      "ResultSelector": {
        "id.
quot;: "$.ResponseBody.id"
      },
      "Next": "Create Address"
    },
    "Create Address": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "xxxxxxxxxxx.amazonaws.com",
        "Method": "POST",
        "Path.
quot;: "States.Format('/universities/{}/address', $.id)",
        "Headers": {
          "Content-Type": [
            "application/json"
          ]
        },
        "RequestBody": {
          "address.
quot;: "$.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.
quot;: "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.
quot;: "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-01-16 10:18:53

docs:在 Catch 中使用 ResultPath 将错误包含在原始输入中,而不是替换它。

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

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"
  }
],
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文