【问题标题】:How to get result of AWS lambda function running with step function如何获取使用 step 函数运行的 AWS lambda 函数的结果
【发布时间】:2018-12-16 20:18:18
【问题描述】:

我正在使用 AWS step 函数来调用这样的 lambda 函数。

 return stepfunctions.startExecution(params).promise().then((result) => {
      console.log(result);
      console.log(result.output);
      return result;
    })

结果是

{ executionArn: 'arn:aws:states:eu-west-2:695510026694:...........:7c197be6-9dca-4bef-966a-ae9ad327bf23',
  startDate: 2018-07-09T07:35:14.930Z }

但我希望将结果作为最终 lambda 函数的输出

我正在通过https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/StepFunctions.html#sendTaskSuccess-property

那里有多个函数,我很困惑哪一个可以用来取回最终 lambda 函数的结果。

stackoverflow Api gateway get output results from step function? 上也有同样的问题,我不想定期调用任何函数并继续检查状态。即使我定期使用 DescribeExecution 函数,我也只会得到执行状态,而不是我想要的结果。是否有任何方法或任何函数返回承诺并在所有 lambda 执行并返回结果后解决

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-step-functions


    【解决方案1】:

    您无法以同步方式从步进函数执行中取回结果。

    不要在完成时轮询步进函数的结果,而是将结果发送到 SNS 主题或 SQS 队列,以便在最终的 lambda 函数中进行进一步处理,或者在步进函数状态机中对整个过程进行建模。

    【讨论】:

    • 谢谢迈克尔这是很有价值的信息,找到了一些我可以订阅的东西以获得有意义的结果,但我仍然发现很难以编程方式将它用于步进功能,所以我决定定期进行观察状态,然后发回成功的结果,我认为这是更容易的方法,当然我不知道哪种方法更好。
    【解决方案2】:

    在做了一些研究并查看了各种教程后,我意识到这个 stackoverflow 答案 Api gateway get output results from step function? 提供了一种更简单的方法来解决问题并从阶跃函数中获得最终结果,是的,我不确定另一种方法以及如何实现任何总是感谢新的答案

    这是我实现相同方法的代码,这可能会对某人有所帮助。

     // in function first start step function execution using startExecution()
    var params = {
            stateMachineArn: 'some correct ARN',
            input: JSON.stringify(body)
          };
    return stepfunctions.startExecution(params).promise().then((result) => {
    
            var paramsStatus = {
              executionArn: result.executionArn
            };
    
            var finalResponse =  new Promise(function(resolve,reject){
          var checkStatusOfStepFunction =  setInterval(function(){
    //on regular interval keep checking status of step function
                stepfunctions.describeExecution(paramsStatus, function(err, data) {
                  console.log('called describeExecution:', data.status);
                  if (err){
                    clearInterval(checkStatusOfStepFunction);
                     reject(err); 
    
                  }
                  else {
                    if(data.status !== 'RUNNING'){
    // once we get status is not running means step function execution is now finished and we get result as data.output
                      clearInterval(checkStatusOfStepFunction);
    
                       resolve(data.output);
                    }  
    
                  }
                }); 
              },200);
            });
    
    
            return finalResponse
    
    
    
          })
    

    【讨论】:

    • 就我而言,只有当状态函数成功时,我才需要解决承诺。话虽如此,当data.status == 'SUCCEEDED',我想解决这个承诺。我在resolve(data.output); 行之前做了一个console.log(data.output)。但是当我从Lambda 调用Step Function 时,有10 次我得到undefined 用于console.log()。云监视日志显示 no such field as data.output 事件虽然 step 函数的状态是 SUCCEEDED。您能否建议一种仅在状态为 SUCCEEDED 时我才能解决承诺的方法?
    【解决方案3】:

    为了能够得到阶跃函数的结果(例如:组合网关和阶跃函数)。您需要:

    1.开始执行
    2。等待您的状态机完成执行(确保等待等于您的状态机的超时 => 等待 = 您的状态机的 TimeoutSeconds)
    3.调用 describeExecution 并从 startExecution 接收 executionArn。

    注意 startExecution 是一个异步函数,它不等待结果。

    在我的例子中,我使用名为 init 的 Lambda 来执行讨论的 3 个步骤:

    代码 lambda 初始化:

    const AWS = require('aws-sdk')
    
    exports.handler = async (event) => {
    
        const stepFunctions = new AWS.StepFunctions();
        const reqBody = event.body || {};
    
        const params = {
            stateMachineArn: process.en.stateMachineArn,
            input: JSON.stringify(reqBody)
        }
    
        return stepFunctions.startExecution(params).promise()
            .then(async data => {
                console.log('==> data: ', data)
                await new Promise(r => setTimeout(r, 6000));
                return stepFunctions.describeExecution({ executionArn: data.executionArn }).promise();
            })
            .then(result => {
               return {
                    statusCode: 200,
                    message: JSON.stringify(result)
                }
            })
            .catch(err => {
                console.error('err: ', err)
                return {
                    statusCode: 500,
                    message: JSON.stringify({ message: 'facing error' })
                }
            })
    }
    

    代码状态机 确保在您的状态机中返回“ResultPath”。

    {
      "Comment": "Annoucement validation",
      "StartAt": "contact-validation",
      "Version": "1.0",
      "TimeoutSeconds": 5,
      "States": {
         "contact-validation": {
              "Type": "Task",
              "Resource": "arn:aws:xxxxxxx:function:scam-detection-dev-contact", 
              "ResultPath": "$.res",
              "Next": "WaitSeconds"
        }, 
         "WaitSeconds": { 
           "Type": "Wait",
            "Seconds": 1,
            "Next": "Result"     
         },
        "Result": {
          "Type": "Pass",
           "ResultPath": "$.res",
          "End": true
        }
      }
    }
    

    【讨论】:

    • 我收到了502 'internal server error',即使所有步骤都已成功完成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2018-01-29
    • 2021-12-03
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多