Api网关从步进功能获得输出结果?

Jag*_*row 12 amazon-web-services aws-lambda aws-api-gateway aws-step-functions

我按照教程创建和调用步骤函数

我在api的GET请求中得到了输出

 {
  "executionArn": "arn:aws:states:ap-northeast-1:123456789012:execution:HelloWorld:MyExecution",
  "startDate": 1.486772644911E9
}
Run Code Online (Sandbox Code Playgroud)

但是,而不是上面的响应,我想要我的步骤函数输出,由下面的结束状态给出.

{
   "name":"Hellow World"
}
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

Ede*_*haw 28

请改用 Express Step Functions。这种类型的 Step Functions 可以同步调用。转到您的 API 网关,并在集成请求部分确保您具有 StartSyncExecution 操作:

在此输入图像描述

之后,在同一页面的下方找到映射模板:

在此输入图像描述

并为 application/json Content-Type 包含以下模板:

#set($input = $input.json('$'))
{
   "input": "$util.escapeJavaScript($input)",
   "stateMachineArn": "arn:aws:states:us-east-1:your_aws_account_id:stateMachine:your_step_machine_name"
}
Run Code Online (Sandbox Code Playgroud)

之后,返回方法执行并转到集成响应,然后转到映射模板部分:

在此输入图像描述

并使用以下模板从您的 lambda 中获取自定义响应:

#set ($parsedPayload = $util.parseJson($input.json('$.output')))
$parsedPayload
Run Code Online (Sandbox Code Playgroud)

我的测试Step Function是这样的:

在此输入图像描述

我的 Lambda 函数代码是:

在此输入图像描述

部署您的 API Gateway 阶段。

现在,如果您转到 Postman 并发送带有任何 json 正文的 POST 请求,现在您将得到如下响应:

在此输入图像描述

  • 现在这应该就是答案了。 (5认同)

Mik*_*AWS 19

AWS Step Functions是异步的,不会立即返回其结果.API网关方法是同步的,最大超时为29秒.

要从步骤函数获取函数输出,您必须在API网关中添加第二个方法,该方法将使用DescribeExecution操作调用步骤函数.API网关客户端必须定期调用此方法(轮询),直到返回的状态不再是"RUNNING".

这是DescribeExecution文档

  • 该死.我想我会回到嵌入式lambda调用.如果有一个同步复选框会很好,但我不确定如果没有父lambda处理初始http请求和响应,AWS将如何做到这一点. (2认同)
  • “EXPRESS”状态机不支持“DescribeExecution” (2认同)

Poo*_*del 11

AWS Step Functions 的新同步 Express 工作流就是答案:https : //aws.amazon.com/blogs/compute/new-synchronous-express-workflows-for-aws-step-functions/

Amazon API Gateway 现在支持与 HTTP API 的 Step Functions StartSyncExecution 集成:https : //aws.amazon.com/about-aws/whats-new/2020/12/amazon-api-gateway-supports-integration-with-step-函数-startsyncexecution-http-apis/


Ash*_*han 7

首先,step 函数是异步执行的,API Gateway 只能调用 step 函数(启动流)。

如果您正在等待来自 Web 应用程序的步进函数调用的结果,您可以为此使用 AWS IOT WebSockets。步骤如下。

  • 使用 WebSockets 设置 AWS IOT 主题。
  • 配置 API 网关和 Step 函数调用。
  • 从 Web 前端订阅 IOT 主题作为 WebSocket 侦听器。
  • 在 Step Functions 工作流的最后一步(以及错误步骤)中,使用 AWS SDK 触发 IOT 主题,该主题将使用 WebSockets 将结果广播到在浏览器中运行的 Web 应用程序。

有关使用 AWS IOT 的 WebSockets 的更多详细信息,请参阅使用 websockets 在浏览器接收 AWS IoT 消息的中型文章。