Api gateway get output results from step function?

冷暖自知 提交于 2019-11-27 16:14:33

问题


I followed tutorial on creating and invoking step functions

I'm getting output in my GET request of api as

 {
  "executionArn": "arn:aws:states:ap-northeast-1:123456789012:execution:HelloWorld:MyExecution",
  "startDate": 1.486772644911E9
}

But, instead of above response I want my step functions output, which is given by end state as below.

{
   "name":"Hellow World"
}

How to achieve this?


回答1:


AWS Step Functions are asynchronous and do not immediately return their results. API Gateway methods are synchronous and have a maximum timeout of 29 seconds.

To get the function output from a Step Function, you have to add a second method in API Gateway which will call the Step Function with the DescribeExecution action. The API Gateway client will have to call this periodically (poll) until the returned status is no longer "RUNNING".

Here's the DescribeExecution documentation




回答2:


First of all the step functions executes asynchronously and API Gateway is only capable of invoking the step function (Starting a flow) only.

If you are waiting for the results of a step function invocation from a web application, you can use AWS IOT WebSockets for this. The steps are as follows.

  • Setup AWS IOT topic with WebSockets.
  • Configure the API Gateway and Step functions invocation.
  • From the Web Frontend subscribe to the IOT Topic as a WebSocket listener.
  • At the last step (And in error steps) in the Step Functions workflow use AWS SDK to trigger the IOT Topic which will broadcast the results to the Web App running in the browser using WebSockets.

For more details on WebSockets with AWS IOT refer the medium article Receiving AWS IoT messages in your browser using websockets.




回答3:


Expanding on what @MikeD at AWS says, if you're certain that the Step Function won't exceed the 30 second timeout, you could create a lambda that executes the step function and then blocks as it polls for the result. Once it has the result, it can return it.

It is a better idea to have the first call return immediately with the execution id, and then pass that id into a second call to retrieve the result, once it's finished.



来源:https://stackoverflow.com/questions/44041821/api-gateway-get-output-results-from-step-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!